visualn-8.x-1.x-dev/modules/visualn_dataset/src/Form/VisualNDataSetRevisionRevertForm.php
modules/visualn_dataset/src/Form/VisualNDataSetRevisionRevertForm.php
<?php
namespace Drupal\visualn_dataset\Form;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\visualn_dataset\Entity\VisualNDataSetInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a form for reverting a VisualN Data Set revision.
*
* @ingroup visualn_dataset
*/
class VisualNDataSetRevisionRevertForm extends ConfirmFormBase {
/**
* The VisualN Data Set revision.
*
* @var \Drupal\visualn_dataset\Entity\VisualNDataSetInterface
*/
protected $revision;
/**
* The VisualN Data Set storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $VisualNDataSetStorage;
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* Constructs a new VisualNDataSetRevisionRevertForm.
*
* @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
* The VisualN Data Set storage.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter service.
*/
public function __construct(EntityStorageInterface $entity_storage, DateFormatterInterface $date_formatter) {
$this->VisualNDataSetStorage = $entity_storage;
$this->dateFormatter = $date_formatter;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.manager')->getStorage('visualn_dataset'),
$container->get('date.formatter')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'visualn_dataset_revision_revert_confirm';
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
return 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.visualn_dataset.version_history', ['visualn_dataset' => $this->revision->id()]);
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return t('Revert');
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return '';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $visualn_dataset_revision = NULL) {
$this->revision = $this->VisualNDataSetStorage->loadRevision($visualn_dataset_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.
$original_revision_timestamp = $this->revision->getRevisionCreationTime();
$this->revision = $this->prepareRevertedRevision($this->revision, $form_state);
$this->revision->revision_log = t('Copy of the revision from %date.', ['%date' => $this->dateFormatter->format($original_revision_timestamp)]);
$this->revision->save();
$this->logger('content')->notice('VisualN Data Set: reverted %title revision %revision.', ['%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]);
drupal_set_message(t('VisualN Data Set %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.visualn_dataset.version_history',
['visualn_dataset' => $this->revision->id()]
);
}
/**
* Prepares a revision to be reverted.
*
* @param \Drupal\visualn_dataset\Entity\VisualNDataSetInterface $revision
* The revision to be reverted.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return \Drupal\visualn_dataset\Entity\VisualNDataSetInterface
* The prepared revision ready to be stored.
*/
protected function prepareRevertedRevision(VisualNDataSetInterface $revision, FormStateInterface $form_state) {
$revision->setNewRevision();
$revision->isDefaultRevision(TRUE);
$revision->setRevisionCreationTime(REQUEST_TIME);
return $revision;
}
}
