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 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)]));
125 drupal_set_message(t('@type %title has been reverted', ['@type' => $this->getBundleLabel($this->revision), '%title' => $this->revision->label()]));
128 $this->revision->save();
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()]
138 * Prepares a revision to be reverted.
140 * @param \Drupal\Core\Entity\RevisionableInterface $revision
141 * The revision to be reverted.
143 * @return \Drupal\Core\Entity\RevisionableInterface
144 * The prepared revision ready to be stored.
146 protected function prepareRevision(RevisionableInterface $revision) {
147 $revision->setNewRevision();
148 $revision->isDefaultRevision(TRUE);
154 * Returns a bundle label.
156 * @param \Drupal\Core\Entity\RevisionableInterface $revision
157 * The entity revision.
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'];