You are here

HomeboxLayoutRevisionRevertForm.php in Homebox 8

File

src/Form/HomeboxLayoutRevisionRevertForm.php
View source
<?php

namespace Drupal\homebox\Form;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\homebox\Entity\HomeboxLayoutInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a form for reverting a Homebox Layout revision.
 *
 * @ingroup homebox
 */
class HomeboxLayoutRevisionRevertForm extends ConfirmFormBase {

  /**
   * The Homebox Layout revision.
   *
   * @var \Drupal\homebox\Entity\HomeboxLayoutInterface
   */
  protected $revision;

  /**
   * The Homebox Layout storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $homeboxLayoutStorage;

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * Time service.
   *
   * @var \Drupal\Component\Datetime\TimeInterface
   */
  protected $time;

  /**
   * Constructs a new HomeboxLayoutRevisionRevertForm.
   *
   * @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
   *   The Homebox Layout storage.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date formatter service.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The time service.
   */
  public function __construct(EntityStorageInterface $entity_storage, DateFormatterInterface $date_formatter, TimeInterface $time) {
    $this->homeboxLayoutStorage = $entity_storage;
    $this->dateFormatter = $date_formatter;
    $this->time = $time;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {

    /** @var \Drupal\Core\Entity\EntityStorageInterface $entity_storage */
    $entity_storage = $container
      ->get('entity_type.manager')
      ->getStorage('homebox_layout');

    /* @var \Drupal\Component\Datetime\TimeInterface $time */
    $time = $container
      ->get('datetime.time');

    /* @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
    $date_formatter = $container
      ->get('date.formatter');
    return new static($entity_storage, $date_formatter, $time);
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'homebox_layout_revision_revert_confirm';
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return t('Are you sure you want to revert to the revision from %revision-date?', [
      '%revision-date' => $this->dateFormatter
        ->format($this->revision
        ->getRevisionCreationTime()),
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    return new Url('entity.homebox_layout.version_history', [
      'homebox_layout' => $this->revision
        ->id(),
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return t('Revert');
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return '';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $homebox_layout_revision = NULL) {
    $this->revision = $this->homeboxLayoutStorage
      ->loadRevision($homebox_layout_revision);
    $form = parent::buildForm($form, $form_state);
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // The revision timestamp will be updated when the revision is saved. Keep
    // the original one for the confirmation message.
    $original_revision_timestamp = $this->revision
      ->getRevisionCreationTime();
    $this->revision = $this
      ->prepareRevertedRevision($this->revision, $form_state);
    $this->revision
      ->setRevisionLogMessage(t('Copy of the revision from %date.', [
      '%date' => $this->dateFormatter
        ->format($original_revision_timestamp),
    ]));
    $this->revision
      ->save();
    $this
      ->logger('content')
      ->notice('Homebox Layout: reverted %title revision %revision.', [
      '%title' => $this->revision
        ->label(),
      '%revision' => $this->revision
        ->getRevisionId(),
    ]);
    $this
      ->messenger()
      ->addMessage(t('Homebox Layout %title has been reverted to the revision from %revision-date.', [
      '%title' => $this->revision
        ->label(),
      '%revision-date' => $this->dateFormatter
        ->format($original_revision_timestamp),
    ]));
    $form_state
      ->setRedirect('entity.homebox_layout.version_history', [
      'homebox_layout' => $this->revision
        ->id(),
    ]);
  }

  /**
   * Prepares a revision to be reverted.
   *
   * @param \Drupal\homebox\Entity\HomeboxLayoutInterface $revision
   *   The revision to be reverted.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   *
   * @return \Drupal\homebox\Entity\HomeboxLayoutInterface
   *   The prepared revision ready to be stored.
   */
  protected function prepareRevertedRevision(HomeboxLayoutInterface $revision, FormStateInterface $form_state) {
    $revision
      ->setNewRevision();
    $revision
      ->isDefaultRevision(TRUE);
    $revision
      ->setRevisionCreationTime($this->time
      ->getRequestTime());
    return $revision;
  }

}

Classes

Namesort descending Description
HomeboxLayoutRevisionRevertForm Provides a form for reverting a Homebox Layout revision.