Upgraded imagemagick and manually altered pdf to image module to handle changes....
[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       drupal_set_message(t('@type %title has been reverted to the revision from %revision-date.', ['@type' => $this->getBundleLabel($this->revision), '%title' => $this->revision->label(), '%revision-date' => $this->dateFormatter->format($original_revision_timestamp)]));
123     }
124     else {
125       drupal_set_message(t('@type %title has been reverted', ['@type' => $this->getBundleLabel($this->revision), '%title' => $this->revision->label()]));
126     }
127
128     $this->revision->save();
129
130     $this->logger('content')->notice('@type: reverted %title revision %revision.', ['@type' => $this->revision->bundle(), '%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]);
131     $form_state->setRedirect(
132       "entity.{$this->revision->getEntityTypeId()}.version_history",
133       [$this->revision->getEntityTypeId() => $this->revision->id()]
134     );
135   }
136
137   /**
138    * Prepares a revision to be reverted.
139    *
140    * @param \Drupal\Core\Entity\RevisionableInterface $revision
141    *   The revision to be reverted.
142    *
143    * @return \Drupal\Core\Entity\RevisionableInterface
144    *   The prepared revision ready to be stored.
145    */
146   protected function prepareRevision(RevisionableInterface $revision) {
147     $revision->setNewRevision();
148     $revision->isDefaultRevision(TRUE);
149
150     return $revision;
151   }
152
153   /**
154    * Returns a bundle label.
155    *
156    * @param \Drupal\Core\Entity\RevisionableInterface $revision
157    *   The entity revision.
158    *
159    * @return string
160    */
161   protected function getBundleLabel(RevisionableInterface $revision) {
162     /** @var \Drupal\Core\Entity\EntityInterface|\Drupal\Core\Entity\RevisionableInterface $revision */
163     $bundle_info = $this->bundleInformation->getBundleInfo($revision->getEntityTypeId());
164     return $bundle_info[$revision->bundle()]['label'];
165   }
166
167 }