1 {% extends "base/class.php.twig" %}
4 \Drupal\{{module}}\Form\{{ entity_class }}RevisionRevertForm.
7 {% block namespace_class %}
8 namespace Drupal\{{module}}\Form;
12 use Drupal\Core\Datetime\DateFormatterInterface;
13 use Drupal\Core\Entity\EntityStorageInterface;
14 use Drupal\Core\Form\ConfirmFormBase;
15 use Drupal\Core\Form\FormStateInterface;
17 use Drupal\{{module}}\Entity\{{ entity_class }}Interface;
18 use Symfony\Component\DependencyInjection\ContainerInterface;
21 {% block class_declaration %}
23 * Provides a form for reverting a {{ label }} revision.
27 class {{ entity_class }}RevisionRevertForm extends ConfirmFormBase {% endblock %}
28 {% block class_methods %}
31 * The {{ label }} revision.
33 * @var \Drupal\{{module}}\Entity\{{ entity_class }}Interface
38 * The {{ label }} storage.
40 * @var \Drupal\Core\Entity\EntityStorageInterface
42 protected ${{ entity_class }}Storage;
45 * The date formatter service.
47 * @var \Drupal\Core\Datetime\DateFormatterInterface
49 protected $dateFormatter;
52 * Constructs a new {{ entity_class }}RevisionRevertForm.
54 * @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
55 * The {{ label }} storage.
56 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
57 * The date formatter service.
59 public function __construct(EntityStorageInterface $entity_storage, DateFormatterInterface $date_formatter) {
60 $this->{{ entity_class }}Storage = $entity_storage;
61 $this->dateFormatter = $date_formatter;
67 public static function create(ContainerInterface $container) {
69 $container->get('entity.manager')->getStorage('{{ entity_name }}'),
70 $container->get('date.formatter')
77 public function getFormId() {
78 return '{{ entity_name }}_revision_revert_confirm';
84 public function getQuestion() {
85 return t('Are you sure you want to revert to the revision from %revision-date?', ['%revision-date' => $this->dateFormatter->format($this->revision->getRevisionCreationTime())]);
91 public function getCancelUrl() {
92 return new Url('entity.{{ entity_name }}.version_history', array('{{ entity_name }}' => $this->revision->id()));
98 public function getConfirmText() {
105 public function getDescription() {
112 public function buildForm(array $form, FormStateInterface $form_state, ${{ entity_name }}_revision = NULL) {
113 $this->revision = $this->{{ entity_class }}Storage->loadRevision(${{ entity_name }}_revision);
114 $form = parent::buildForm($form, $form_state);
122 public function submitForm(array &$form, FormStateInterface $form_state) {
123 // The revision timestamp will be updated when the revision is saved. Keep
124 // the original one for the confirmation message.
125 $original_revision_timestamp = $this->revision->getRevisionCreationTime();
127 $this->revision = $this->prepareRevertedRevision($this->revision, $form_state);
128 $this->revision->revision_log = t('Copy of the revision from %date.', ['%date' => $this->dateFormatter->format($original_revision_timestamp)]);
129 $this->revision->save();
131 $this->logger('content')->notice('{{ label }}: reverted %title revision %revision.', ['%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]);
132 drupal_set_message(t('{{ label }} %title has been reverted to the revision from %revision-date.', ['%title' => $this->revision->label(), '%revision-date' => $this->dateFormatter->format($original_revision_timestamp)]));
133 $form_state->setRedirect(
134 'entity.{{ entity_name }}.version_history',
135 array('{{ entity_name }}' => $this->revision->id())
140 * Prepares a revision to be reverted.
142 * @param \Drupal\{{module}}\Entity\{{ entity_class }}Interface $revision
143 * The revision to be reverted.
144 * @param \Drupal\Core\Form\FormStateInterface $form_state
145 * The current state of the form.
147 * @return \Drupal\{{module}}\Entity\{{ entity_class }}Interface
148 * The prepared revision ready to be stored.
150 protected function prepareRevertedRevision({{ entity_class }}Interface $revision, FormStateInterface $form_state) {
151 $revision->setNewRevision();
152 $revision->isDefaultRevision(TRUE);
153 $revision->setRevisionCreationTime(REQUEST_TIME);