View source
<?php
namespace Drupal\config_entity_revisions;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
class ConfigEntityRevisionsRevertFormBase extends ConfirmFormBase {
protected $entityTypeManager;
protected $dateFormatter;
protected $time;
protected $messenger;
protected $revision;
protected $config_entity;
protected $revision_id;
protected $action;
protected $config;
public function __construct(EntityTypeManager $entity_type_manager, DateFormatterInterface $date_formatter, TimeInterface $time, MessengerInterface $messenger) {
$this->entityTypeManager = $entity_type_manager;
$this->dateFormatter = $date_formatter;
$this->time = $time;
$this->messenger = $messenger;
$match = \Drupal::service('router')
->matchRequest(\Drupal::request());
$this->config_entity = $match['config_entity']
->revisioned_entity();
$this->revision_id = $match['revision_id'];
$this->revision = $this->config_entity
->contentEntityStorage()
->loadRevision($this->revision_id);
$revisionsID = $this->config_entity
->getContentEntityID();
$latest_published = $this->config_entity
->contentEntityStorage()
->getLatestPublishedRevision($revisionsID);
$publish = is_null($latest_published) || $this->revision
->getRevisionId() > $latest_published
->getRevisionId();
$this->action = $publish ? 'publish' : 'revert';
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('date.formatter'), $container
->get('datetime.time'), $container
->get('messenger'));
}
public function getFormId() {
return $this->config_entity
->module_name() . '_revision_revert_confirm';
}
public function getQuestion() {
return $this
->t('Are you sure you want to %action to the revision from %revision-date?', [
'%revision-date' => $this->dateFormatter
->format($this->revision
->getRevisionCreationTime()),
'%action' => $this->action,
]);
}
public function getCancelUrl() {
return new Url('entity.' . $this->config_entity
->getEntityTypeId() . '.revisions', [
$this->config_entity
->getEntityTypeId() => $this->config_entity
->id(),
]);
}
public function getConfirmText() {
return $this
->t(ucfirst($this->action));
}
public function getDescription() {
return '';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
return $form;
}
public function prepareRevertedRevision(ConfigEntityRevisionsEntityInterface $revision) {
$revision
->setNewRevision();
$revision
->isDefaultRevision(TRUE);
$original_revision_timestamp = $revision
->getRevisionCreationTime();
$originalLogMessage = $revision
->getRevisionLogMessage();
$logMessage = $originalLogMessage ? $this
->t('Copy of the revision from %date (%message).', [
'%date' => $this->dateFormatter
->format($original_revision_timestamp),
'%message' => $originalLogMessage,
]) : $this
->t('Copy of the revision from %date.', [
'%date' => $this->dateFormatter
->format($original_revision_timestamp),
]);
$revision
->setRevisionLogMessage($logMessage);
$revision
->setRevisionUserId($this
->currentUser()
->id());
$revision
->setRevisionCreationTime($this->time
->getRequestTime());
$revision
->setChangedTime($this->time
->getRequestTime());
$revision
->set('moderation_state', 'draft');
$revision
->setUnpublished();
return $revision;
}
public function prepareToPublishCurrentRevision(ConfigEntityRevisionsEntityInterface $revision) {
$revision
->set('moderation_state', 'published');
$revision
->setPublished();
$revision
->isDefaultRevision(TRUE);
return $revision;
}
public function applyRevisionChange() {
if ($this->action == 'revert') {
$this->revision = $this
->prepareRevertedRevision($this->revision);
}
else {
$this->revision = $this
->prepareToPublishCurrentRevision($this->revision);
}
$this->revision
->save();
}
public function updateConfigEntity() {
$this->config_entity = \Drupal::getContainer()
->get('serializer')
->deserialize($this->revision
->get('configuration')->value, get_class($this->config_entity), 'json');
$this->config_entity
->enforceIsNew(FALSE);
$this->config_entity
->set('settingsOriginal', $this->config_entity
->get('settings'));
$this->config_entity
->set('revision_id', $this->revision
->getRevisionId());
$this->config_entity
->save();
}
public function logUpdate() {
$this
->logger('content')
->notice('@form: set @form to revision %revision.', [
'@form' => $this->config_entity
->label(),
'%revision' => $this->revision
->getRevisionId(),
]);
}
public function displayUpdate() {
$original_revision_timestamp = $this->revision
->getRevisionCreationTime();
$this->messenger
->addMessage($this
->t('%entity_title %title has been set to the revision from %revision-date.', [
'%entity_title' => $this->config_entity
->title(),
'%title' => $this->config_entity
->label(),
'%revision-date' => $this->dateFormatter
->format($original_revision_timestamp),
]));
}
public function setRedirect(FormStateInterface $form_state) {
$form_state
->setRedirect('entity.' . $this->config_entity
->getEntityTypeId() . '.revisions', [
$this->config_entity
->getEntityTypeId() => $this->config_entity
->id(),
]);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this
->applyRevisionChange();
$this
->updateConfigEntity();
$this
->logUpdate();
$this
->displayUpdate();
$this
->setRedirect($form_state);
}
}