Version 1
[yaffs-website] / vendor / drupal / console / templates / module / src / Entity / Form / entity-content-revision-revert.php.twig
1 {% extends "base/class.php.twig" %}
2
3 {% block file_path %}
4 \Drupal\{{module}}\Form\{{ entity_class }}RevisionRevertForm.
5 {% endblock %}
6
7 {% block namespace_class %}
8 namespace Drupal\{{module}}\Form;
9 {% endblock %}
10
11 {% block use_class %}
12 use Drupal\Core\Datetime\DateFormatterInterface;
13 use Drupal\Core\Entity\EntityStorageInterface;
14 use Drupal\Core\Form\ConfirmFormBase;
15 use Drupal\Core\Form\FormStateInterface;
16 use Drupal\Core\Url;
17 use Drupal\{{module}}\Entity\{{ entity_class }}Interface;
18 use Symfony\Component\DependencyInjection\ContainerInterface;
19 {% endblock %}
20
21 {% block class_declaration %}
22 /**
23  * Provides a form for reverting a {{ label }} revision.
24  *
25  * @ingroup {{module}}
26  */
27 class {{ entity_class }}RevisionRevertForm extends ConfirmFormBase {% endblock %}
28 {% block class_methods %}
29
30   /**
31    * The {{ label }} revision.
32    *
33    * @var \Drupal\{{module}}\Entity\{{ entity_class }}Interface
34    */
35   protected $revision;
36
37   /**
38    * The {{ label }} storage.
39    *
40    * @var \Drupal\Core\Entity\EntityStorageInterface
41    */
42   protected ${{ entity_class }}Storage;
43
44   /**
45    * The date formatter service.
46    *
47    * @var \Drupal\Core\Datetime\DateFormatterInterface
48    */
49   protected $dateFormatter;
50
51   /**
52    * Constructs a new {{ entity_class }}RevisionRevertForm.
53    *
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.
58    */
59   public function __construct(EntityStorageInterface $entity_storage, DateFormatterInterface $date_formatter) {
60     $this->{{ entity_class }}Storage = $entity_storage;
61     $this->dateFormatter = $date_formatter;
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public static function create(ContainerInterface $container) {
68     return new static(
69       $container->get('entity.manager')->getStorage('{{ entity_name }}'),
70       $container->get('date.formatter')
71     );
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function getFormId() {
78     return '{{ entity_name }}_revision_revert_confirm';
79   }
80
81   /**
82    * {@inheritdoc}
83    */
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())]);
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public function getCancelUrl() {
92     return new Url('entity.{{ entity_name }}.version_history', array('{{ entity_name }}' => $this->revision->id()));
93   }
94
95   /**
96    * {@inheritdoc}
97    */
98   public function getConfirmText() {
99     return t('Revert');
100   }
101
102   /**
103    * {@inheritdoc}
104    */
105   public function getDescription() {
106     return '';
107   }
108
109   /**
110    * {@inheritdoc}
111    */
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);
115
116     return $form;
117   }
118
119   /**
120    * {@inheritdoc}
121    */
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();
126
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();
130
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())
136     );
137   }
138
139   /**
140    * Prepares a revision to be reverted.
141    *
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.
146    *
147    * @return \Drupal\{{module}}\Entity\{{ entity_class }}Interface
148    *   The prepared revision ready to be stored.
149    */
150   protected function prepareRevertedRevision({{ entity_class }}Interface $revision, FormStateInterface $form_state) {
151     $revision->setNewRevision();
152     $revision->isDefaultRevision(TRUE);
153     $revision->setRevisionCreationTime(REQUEST_TIME);
154
155     return $revision;
156   }
157 {% endblock %}