LibraryItemRevisionRevertForm.php in Paragraphs 8
File
modules/paragraphs_library/src/Form/LibraryItemRevisionRevertForm.php
View source
<?php
namespace Drupal\paragraphs_library\Form;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Url;
use Drupal\paragraphs_library\LibraryItemInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LibraryItemRevisionRevertForm extends ConfirmFormBase {
protected $revision;
protected $entityTypeManager;
protected $dateFormatter;
protected $time;
protected $messenger;
public function __construct(EntityTypeManagerInterface $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;
}
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 'library_item_revision_revert';
}
public function getQuestion() {
return $this
->t('Are you sure you want to revert revision from %revision-date?', [
'%revision-date' => $this->dateFormatter
->format($this->revision
->getChangedTime()),
]);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$original_revision_timestamp = $this->revision
->getChangedTime();
$this->revision = $this
->prepareRevertedRevision($this->revision);
$this->revision->revision_log = $this
->t('Copy of the revision from %date.', [
'%date' => $this->dateFormatter
->format($original_revision_timestamp),
]);
$this->revision
->setChangedTime($this->time
->getRequestTime());
$this->revision
->save();
$this->messenger
->addMessage($this
->t('%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.paragraphs_library_item.version_history', [
'paragraphs_library_item' => $this->revision
->id(),
]);
}
protected function prepareRevertedRevision(LibraryItemInterface $library_item_revision) {
$library_item_revision
->setNewRevision();
$library_item_revision
->isDefaultRevision(TRUE);
return $library_item_revision;
}
public function buildForm(array $form, FormStateInterface $form_state, $paragraphs_library_item_revision = NULL) {
$this->revision = $this->entityTypeManager
->getStorage('paragraphs_library_item')
->loadRevision($paragraphs_library_item_revision);
return parent::buildForm($form, $form_state);
}
public function getCancelUrl() {
return new Url('entity.paragraphs_library_item.version_history', [
'paragraphs_library_item' => $this->revision
->id(),
]);
}
public function getConfirmText() {
return $this
->t('Revert');
}
}