Added Entity and Entity Reference Revisions which got dropped somewhere along the...
[yaffs-website] / web / modules / contrib / entity / src / Form / RevisionRevertForm.php
1 <?php
2
3 namespace Drupal\entity\Form;
4
5 use Drupal\Core\Datetime\DateFormatterInterface;
6 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
7 use Drupal\Core\Entity\RevisionableInterface;
8 use Drupal\Core\Form\ConfirmFormBase;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\Core\Entity\RevisionLogInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12 use Symfony\Component\HttpFoundation\Request;
13
14 class RevisionRevertForm extends ConfirmFormBase {
15
16   /**
17    * The entity revision.
18    *
19    * @var \Drupal\Core\Entity\EntityInterface|\Drupal\Core\Entity\RevisionableInterface|\Drupal\Core\Entity\RevisionLogInterface
20    */
21   protected $revision;
22
23   /**
24    * The date formatter.
25    *
26    * @var \Drupal\Core\Datetime\DateFormatterInterface
27    */
28   protected $dateFormatter;
29
30   /**
31    * The entity bundle information.
32    *
33    * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
34    */
35   protected $bundleInformation;
36
37   /**
38    * Creates a new RevisionRevertForm instance.
39    *
40    * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
41    *   The date formatter.
42    * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_information
43    *   The bundle information.
44    */
45   public function __construct(DateFormatterInterface $date_formatter, EntityTypeBundleInfoInterface $bundle_information) {
46     $this->dateFormatter = $date_formatter;
47     $this->bundleInformation = $bundle_information;
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   public static function create(ContainerInterface $container) {
54     return new static(
55       $container->get('date.formatter'),
56       $container->get('entity_type.bundle.info')
57     );
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public function getFormId() {
64     return 'entity_revision_revert_confirm';
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public function getQuestion() {
71     if ($this->revision instanceof RevisionLogInterface) {
72       return $this->t('Are you sure you want to revert to the revision from %revision-date?', ['%revision-date' => $this->dateFormatter->format($this->revision->getRevisionCreationTime())]);
73     }
74     return $this->t('Are you sure you want to revert the revision?');
75   }
76
77   /**
78    * {@inheritdoc}
79    */
80   public function getCancelUrl() {
81     if ($this->revision->getEntityType()->hasLinkTemplate('version-history')) {
82       return $this->revision->toUrl('version-history');
83     }
84     return $this->revision->toUrl();
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public function getConfirmText() {
91     return t('Revert');
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function getDescription() {
98     return '';
99   }
100
101   /**
102    * {@inheritdoc}
103    */
104   public function buildForm(array $form, FormStateInterface $form_state, $_entity_revision = NULL, Request $request = NULL) {
105     $this->revision = $_entity_revision;
106     $form = parent::buildForm($form, $form_state);
107
108     return $form;
109   }
110
111   /**
112    * {@inheritdoc}
113    */
114   public function submitForm(array &$form, FormStateInterface $form_state) {
115     // The revision timestamp will be updated when the revision is saved. Keep
116     // the original one for the confirmation message.
117     $this->revision = $this->prepareRevision($this->revision);
118     if ($this->revision instanceof RevisionLogInterface) {
119       $original_revision_timestamp = $this->revision->getRevisionCreationTime();
120
121       $this->revision->setRevisionLogMessage($this->t('Copy of the revision from %date.', ['%date' => $this->dateFormatter->format($original_revision_timestamp)]));
122       $this->messenger()->addStatus(t('@type %title has been reverted to the revision from %revision-date.', [
123         '@type' => $this->getBundleLabel($this->revision),
124         '%title' => $this->revision->label(),
125         '%revision-date' => $this->dateFormatter->format($original_revision_timestamp),
126       ]));
127     }
128     else {
129       $this->messenger()->addStatus(t('@type %title has been reverted', [
130         '@type' => $this->getBundleLabel($this->revision),
131         '%title' => $this->revision->label(),
132       ]));
133     }
134
135     $this->revision->save();
136
137     $this->logger('content')->notice('@type: reverted %title revision %revision.', ['@type' => $this->revision->bundle(), '%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]);
138     $form_state->setRedirect(
139       "entity.{$this->revision->getEntityTypeId()}.version_history",
140       [$this->revision->getEntityTypeId() => $this->revision->id()]
141     );
142   }
143
144   /**
145    * Prepares a revision to be reverted.
146    *
147    * @param \Drupal\Core\Entity\RevisionableInterface $revision
148    *   The revision to be reverted.
149    *
150    * @return \Drupal\Core\Entity\RevisionableInterface
151    *   The prepared revision ready to be stored.
152    */
153   protected function prepareRevision(RevisionableInterface $revision) {
154     $revision->setNewRevision();
155     $revision->isDefaultRevision(TRUE);
156
157     return $revision;
158   }
159
160   /**
161    * Returns a bundle label.
162    *
163    * @param \Drupal\Core\Entity\RevisionableInterface $revision
164    *   The entity revision.
165    *
166    * @return string
167    */
168   protected function getBundleLabel(RevisionableInterface $revision) {
169     /** @var \Drupal\Core\Entity\EntityInterface|\Drupal\Core\Entity\RevisionableInterface $revision */
170     $bundle_info = $this->bundleInformation->getBundleInfo($revision->getEntityTypeId());
171     return $bundle_info[$revision->bundle()]['label'];
172   }
173
174 }