Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
novicell/custom_forms / src / Form / CustomFormRevisionRevertForm.php
Size: Mime:
<?php

namespace Drupal\custom_forms\Form;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\custom_forms\CustomFormInterface;
use Drupal\custom_forms\Entity\CustomFormType;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Class CustomFormRevisionRevertForm
 *
 * @package Drupal\custom_forms\Form
 */
class CustomFormRevisionRevertForm extends ConfirmFormBase {

  /**
   * The custom form revision.
   *
   * @var \Drupal\custom_forms\CustomFormInterface
   */
  protected $revision;

  /**
   * The sql content entity storage.
   *
   * @var \Drupal\Core\Entity\Sql\SqlContentEntityStorage
   */
  protected $sqlStorage;

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

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

  /**
   * Constructs a new CustomFormRevisionRevertForm.
   *
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date formatter service.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The time service.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function __construct(DateFormatterInterface $date_formatter, TimeInterface $time) {
    $this->sqlStorage = \Drupal::entityTypeManager()->getStorage('custom_form');
    $this->dateFormatter = $date_formatter;
    $this->time = $time;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('date.formatter'),
      $container->get('datetime.time')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'custom_form_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.custom_form.version_history', ['custom_form' => $this->revision->id()]);
  }

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

  /**
   * @inheritDoc
   */
  public function getDescription() {
    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 buildForm(array $form, FormStateInterface $form_state, $custom_form_revision = NULL) {
    $this->revision = $this->sqlStorage->loadRevision($custom_form_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->setRevisionTranslationAffected(TRUE); // for some reason required for log messages to show up?
    $this->revision->setRevisionUserId($this->currentUser()->id());
    $this->revision->setRevisionCreationTime($this->time->getRequestTime());
    $this->revision->setChangedTime($this->time->getRequestTime());

    try {
      $this->revision->save();
      $form_type = CustomFormType::load($this->revision->bundle());
      $this->logger('Custom Forms')->notice('@type: reverted %title revision %revision.', ['@type' => $this->revision->bundle(), '%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]);
      $this->messenger()
        ->addStatus($this->t('@type %title has been reverted to the revision from %revision-date.', [
          '@type' => $form_type->label(),
          '%title' => $this->revision->label(),
          '%revision-date' => $this->dateFormatter->format($original_revision_timestamp),
        ]));
    } catch (EntityStorageException $e) {
      $error_message = $this->t('Failed reverting of revision. Message = %message', [
        '%message' => $e->getMessage()
      ]);
      \Drupal::logger('Custom Forms')->error($error_message);
      $this->messenger()->addError($this->t('Failed reverting of revision, please try again. If it continues please contact an administrator.'));
    }
    $form_state->setRedirect(
      'entity.custom_form.version_history',
      ['custom_form' => $this->revision->id()]
    );
  }

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

    return $revision;
  }

}