recurring_events-2.0.x-dev/src/Form/EventInstanceRevisionRevertForm.php
src/Form/EventInstanceRevisionRevertForm.php
<?php
declare(strict_types=1);
namespace Drupal\recurring_events\Form;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\DependencyInjection\AutowireTrait;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\recurring_events\EventInstanceStorageTrait;
use Drupal\recurring_events\EventInterface;
/**
* Provides a form for reverting an eventinstance revision.
*
* @ingroup recurring_events
*/
class EventInstanceRevisionRevertForm extends ConfirmFormBase {
use AutowireTrait;
use EventInstanceStorageTrait;
use StringTranslationTrait;
/**
* The eventinstance revision.
*
* @var \Drupal\recurring_events\EventInterface
*/
protected $revision;
public function __construct(
EntityTypeManagerInterface $entityTypeManager,
protected DateFormatterInterface $dateFormatter,
protected TimeInterface $time,
) {
$this->entityTypeManager = $entityTypeManager;
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'eventinstance_revision_revert_confirm';
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to revert to the revision from %revision-date?', ['%revision-date' => $this->dateFormatter->format($this->revision->getRevisionCreationTime())]);
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('entity.eventinstance.version_history', ['eventinstance' => $this->revision->id()]);
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Revert');
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return '';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $eventinstance_revision = NULL) {
$this->revision = $this->eventInstanceStorage()->loadRevision($eventinstance_revision);
return parent::buildForm($form, $form_state);
}
/**
* {@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.
$original_revision_timestamp = $this->revision->getRevisionCreationTime();
$this->revision = $this->prepareRevertedRevision($this->revision, $form_state);
$this->revision->revision_log = $this->t('Copy of the revision from %date.', ['%date' => $this->dateFormatter->format($original_revision_timestamp)]);
$this->revision->save();
$this->logger('content')->notice('eventinstance: reverted %title revision %revision.', [
'%title' => $this->revision->label(),
'%revision' => $this->revision->getRevisionId(),
]);
$this->messenger()->addMessage($this->t('eventinstance %title has been reverted to the revision from %revision-date.', [
'%title' => $this->revision->label(),
'%revision-date' => $this->dateFormatter->format($original_revision_timestamp),
]));
$form_state->setRedirect(
'entity.eventinstance.version_history',
['eventinstance' => $this->revision->id()]
);
}
/**
* Prepares a revision to be reverted.
*
* @param \Drupal\recurring_events\EventInterface $revision
* The revision to be reverted.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return \Drupal\recurring_events\EventInterface
* The prepared revision ready to be stored.
*/
protected function prepareRevertedRevision(EventInterface $revision, FormStateInterface $form_state) {
$revision->setNewRevision();
$revision->isDefaultRevision(TRUE);
$revision->setRevisionCreationTime($this->time->getRequestTime());
return $revision;
}
}
