3 namespace Drupal\entity\Form;
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;
14 class RevisionRevertForm extends ConfirmFormBase {
17 * The entity revision.
19 * @var \Drupal\Core\Entity\EntityInterface|\Drupal\Core\Entity\RevisionableInterface|\Drupal\Core\Entity\RevisionLogInterface
26 * @var \Drupal\Core\Datetime\DateFormatterInterface
28 protected $dateFormatter;
31 * The entity bundle information.
33 * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
35 protected $bundleInformation;
38 * Creates a new RevisionRevertForm instance.
40 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
42 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_information
43 * The bundle information.
45 public function __construct(DateFormatterInterface $date_formatter, EntityTypeBundleInfoInterface $bundle_information) {
46 $this->dateFormatter = $date_formatter;
47 $this->bundleInformation = $bundle_information;
53 public static function create(ContainerInterface $container) {
55 $container->get('date.formatter'),
56 $container->get('entity_type.bundle.info')
63 public function getFormId() {
64 return 'entity_revision_revert_confirm';
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())]);
74 return $this->t('Are you sure you want to revert the revision?');
80 public function getCancelUrl() {
81 if ($this->revision->getEntityType()->hasLinkTemplate('version-history')) {
82 return $this->revision->toUrl('version-history');
84 return $this->revision->toUrl();
90 public function getConfirmText() {
97 public function getDescription() {
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);
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();
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),
129 $this->messenger()->addStatus(t('@type %title has been reverted', [
130 '@type' => $this->getBundleLabel($this->revision),
131 '%title' => $this->revision->label(),
135 $this->revision->save();
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()]
145 * Prepares a revision to be reverted.
147 * @param \Drupal\Core\Entity\RevisionableInterface $revision
148 * The revision to be reverted.
150 * @return \Drupal\Core\Entity\RevisionableInterface
151 * The prepared revision ready to be stored.
153 protected function prepareRevision(RevisionableInterface $revision) {
154 $revision->setNewRevision();
155 $revision->isDefaultRevision(TRUE);
161 * Returns a bundle label.
163 * @param \Drupal\Core\Entity\RevisionableInterface $revision
164 * The entity revision.
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'];