X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Fentity%2Fsrc%2FForm%2FRevisionRevertForm.php;fp=web%2Fmodules%2Fcontrib%2Fentity%2Fsrc%2FForm%2FRevisionRevertForm.php;h=c94c53a0c640eb0835543097b1335e4adffee699;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/modules/contrib/entity/src/Form/RevisionRevertForm.php b/web/modules/contrib/entity/src/Form/RevisionRevertForm.php new file mode 100644 index 000000000..c94c53a0c --- /dev/null +++ b/web/modules/contrib/entity/src/Form/RevisionRevertForm.php @@ -0,0 +1,167 @@ +dateFormatter = $date_formatter; + $this->bundleInformation = $bundle_information; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('date.formatter'), + $container->get('entity_type.bundle.info') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'entity_revision_revert_confirm'; + } + + /** + * {@inheritdoc} + */ + public function getQuestion() { + if ($this->revision instanceof RevisionLogInterface) { + return $this->t('Are you sure you want to revert to the revision from %revision-date?', ['%revision-date' => $this->dateFormatter->format($this->revision->getRevisionCreationTime())]); + } + return $this->t('Are you sure you want to revert the revision?'); + } + + /** + * {@inheritdoc} + */ + public function getCancelUrl() { + if ($this->revision->getEntityType()->hasLinkTemplate('version-history')) { + return $this->revision->toUrl('version-history'); + } + return $this->revision->toUrl(); + } + + /** + * {@inheritdoc} + */ + public function getConfirmText() { + return t('Revert'); + } + + /** + * {@inheritdoc} + */ + public function getDescription() { + return ''; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state, $_entity_revision = NULL, Request $request = NULL) { + $this->revision = $_entity_revision; + $form = parent::buildForm($form, $form_state); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + // The revision timestamp will be updated when the revision is saved. Keep + // the original one for the confirmation message. + $this->revision = $this->prepareRevision($this->revision); + if ($this->revision instanceof RevisionLogInterface) { + $original_revision_timestamp = $this->revision->getRevisionCreationTime(); + + $this->revision->setRevisionLogMessage($this->t('Copy of the revision from %date.', ['%date' => $this->dateFormatter->format($original_revision_timestamp)])); + drupal_set_message(t('@type %title has been reverted to the revision from %revision-date.', ['@type' => $this->getBundleLabel($this->revision), '%title' => $this->revision->label(), '%revision-date' => $this->dateFormatter->format($original_revision_timestamp)])); + } + else { + drupal_set_message(t('@type %title has been reverted', ['@type' => $this->getBundleLabel($this->revision), '%title' => $this->revision->label()])); + } + + $this->revision->save(); + + $this->logger('content')->notice('@type: reverted %title revision %revision.', ['@type' => $this->revision->bundle(), '%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]); + $form_state->setRedirect( + "entity.{$this->revision->getEntityTypeId()}.version_history", + [$this->revision->getEntityTypeId() => $this->revision->id()] + ); + } + + /** + * Prepares a revision to be reverted. + * + * @param \Drupal\Core\Entity\RevisionableInterface $revision + * The revision to be reverted. + * + * @return \Drupal\Core\Entity\RevisionableInterface + * The prepared revision ready to be stored. + */ + protected function prepareRevision(RevisionableInterface $revision) { + $revision->setNewRevision(); + $revision->isDefaultRevision(TRUE); + + return $revision; + } + + /** + * Returns a bundle label. + * + * @param \Drupal\Core\Entity\RevisionableInterface $revision + * The entity revision. + * + * @return string + */ + protected function getBundleLabel(RevisionableInterface $revision) { + /** @var \Drupal\Core\Entity\EntityInterface|\Drupal\Core\Entity\RevisionableInterface $revision */ + $bundle_info = $this->bundleInformation->getBundleInfo($revision->getEntityTypeId()); + return $bundle_info[$revision->bundle()]['label']; + } + +}