RevisionRevertForm.php in Entity API 8
File
src/Form/RevisionRevertForm.php
View source
<?php
namespace Drupal\entity\Form;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\RevisionableInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\RevisionLogInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class RevisionRevertForm extends ConfirmFormBase {
protected $revision;
protected $dateFormatter;
protected $bundleInformation;
public function __construct(DateFormatterInterface $date_formatter, EntityTypeBundleInfoInterface $bundle_information) {
$this->dateFormatter = $date_formatter;
$this->bundleInformation = $bundle_information;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('date.formatter'), $container
->get('entity_type.bundle.info'));
}
public function getFormId() {
return 'entity_revision_revert_confirm';
}
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?');
}
public function getCancelUrl() {
if ($this->revision
->getEntityType()
->hasLinkTemplate('version-history')) {
return $this->revision
->toUrl('version-history');
}
return $this->revision
->toUrl();
}
public function getConfirmText() {
return $this
->t('Revert');
}
public function getDescription() {
return '';
}
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;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$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),
]));
$this
->messenger()
->addStatus(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 {
$this
->messenger()
->addStatus(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(),
]);
}
protected function prepareRevision(RevisionableInterface $revision) {
$revision
->setNewRevision();
$revision
->isDefaultRevision(TRUE);
return $revision;
}
protected function getBundleLabel(RevisionableInterface $revision) {
$bundle_info = $this->bundleInformation
->getBundleInfo($revision
->getEntityTypeId());
return $bundle_info[$revision
->bundle()]['label'];
}
}