marketo_suite-1.0.x-dev/src/Entity/Form/MarketoFormEntityRevisionRevertForm.php

src/Entity/Form/MarketoFormEntityRevisionRevertForm.php
<?php

namespace Drupal\e3_marketo\Entity\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\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\e3_marketo\Entity\MarketoFormEntityInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a form for reverting a Marketo form entity revision.
 *
 * @ingroup e3_marketo
 */
class MarketoFormEntityRevisionRevertForm extends ConfirmFormBase {

  /**
   * The Marketo form entity revision.
   *
   * @var \Drupal\e3_marketo\Entity\MarketoFormEntityInterface
   */
  protected MarketoFormEntityInterface $revision;

  /**
   * Constructs a new MarketoFormEntityRevisionRevertForm.
   *
   * @param \Drupal\Core\Entity\EntityStorageInterface $marketoFormEntityStorage
   *   The Marketo form entity storage.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $dateFormatter
   *   The date formatter service.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   Time.
   */
  public function __construct(
    protected EntityStorageInterface $marketoFormEntityStorage,
    protected DateFormatterInterface $dateFormatter,
    protected TimeInterface $time
  ) {
  }

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

  /**
   * {@inheritdoc}
   */
  public function getFormId() : string {
    return 'marketo_form_revision_revert_confirm';
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() : TranslatableMarkup {
    return $this->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() : Url {
    return new Url('entity.marketo_form.version_history', ['marketo_form' => $this->revision->id()]);
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() : TranslatableMarkup {
    return $this->t('Revert');
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $marketo_form_revision = NULL) : array {
    /** @var \Drupal\e3_marketo\Entity\MarketoFormEntityInterface $revision */
    $revision = $this->marketoFormEntityStorage->loadRevision($marketo_form_revision);
    $this->revision = $revision;
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  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);
    $this->revision->save();

    $this->logger('content')->notice('Marketo form entity: reverted %title revision %revision.', [
      '%title' => $this->revision->label(),
      '%revision' => $this->revision->getRevisionId(),
    ]);

    $this->messenger()->addStatus($this->t('Marketo form entity %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.marketo_form.version_history',
      ['marketo_form' => $this->revision->id()]
    );
  }

  /**
   * Prepares a revision to be reverted.
   *
   * @param \Drupal\e3_marketo\Entity\MarketoFormEntityInterface $revision
   *   The revision to be reverted.
   *
   * @return \Drupal\e3_marketo\Entity\MarketoFormEntityInterface
   *   The prepared revision ready to be stored.
   */
  protected function prepareRevertedRevision(MarketoFormEntityInterface $revision) : MarketoFormEntityInterface {
    $revision->setNewRevision();
    $revision->isDefaultRevision(TRUE);
    $revision->setRevisionCreationTime($this->time->getRequestTime());

    return $revision;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc