ispim-1.0.x-dev/src/PreviewImage/RevisionRevertForm.php

src/PreviewImage/RevisionRevertForm.php
<?php

declare(strict_types=1);

namespace Drupal\ispim\PreviewImage;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\ispim\Entity\IspimPreviewImageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class RevisionRevertForm extends ConfirmFormBase {

  protected string $entityTypeId = 'ispim_preview_image';

  protected string $bundleEntityTypeId = '';

  protected IspimPreviewImageInterface $revision;

  protected EntityTypeManagerInterface $entityTypeManager;

  protected DateFormatterInterface $dateFormatter;

  protected TimeInterface $time;

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

  public function __construct(
    EntityTypeManagerInterface $entityTypeManager,
    DateFormatterInterface $dateFormatter,
    TimeInterface $time,
  ) {
    $this->entityTypeManager = $entityTypeManager;
    $this->dateFormatter = $dateFormatter;
    $this->time = $time;
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return "{$this->entityTypeId}_revision_revert_form";
  }

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

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    $entityTypeId = $this->revision->getEntityTypeId();

    return new Url(
      "entity.$entityTypeId.revision_history",
      [
        $entityTypeId => $this->revision->id(),
      ],
    );
  }

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

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    // @phpstan-ignore-next-line
    return '';
  }

  /**
   * {@inheritdoc}
   *
   * @param array<string, mixed> $form
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   * @param int|string|null $ispim_preview_image_revision
   *
   * @return array<string, mixed>
   */
  public function buildForm(array $form, FormStateInterface $form_state, $ispim_preview_image_revision = NULL): array {
    /* @noinspection PhpUnhandledExceptionInspection */
    /** @var \Drupal\ispim\PreviewImage\StorageInterface $storage */
    $storage = $this->entityTypeManager->getStorage($this->entityTypeId);
    $this->revision = $storage->loadRevision($ispim_preview_image_revision);

    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   *
   * @phpstan-param array<string, mixed> $form
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  public function submitForm(array &$form, FormStateInterface $form_state): void {
    $originalRevisionTimestamp = $this->revision->getRevisionCreationTime();
    $entityTypeId = $this->revision->getEntityTypeId();
    /* @noinspection PhpUnhandledExceptionInspection */
    $bundle = $this
      ->entityTypeManager
      ->getStorage($this->bundleEntityTypeId)
      ->load($this->revision->bundle());
    $this->revision = $this->prepareRevertedRevision($this->revision, $form_state);

    $args = [
      '@entity.revision.id' => $this->revision->getRevisionId(),
      '@entity.revision.date' => $this->dateFormatter->format($originalRevisionTimestamp),
      '@entity.bundle.id' => $bundle->id(),
      '%entity.bundle.label' => $bundle->label(),
      '%entity.label' => $this->revision->label(),
    ];

    $this
      ->revision
      ->setRevisionLogMessage((string) $this->t(
        'Copy of the revision from @entity.revision.date.',
        $args,
      ))
      ->setRevisionUserId($this->currentUser()->id())
      ->setRevisionCreationTime($this->time->getRequestTime())
      ->setChangedTime($this->time->getRequestTime())
      ->save();

    $this
      ->logger($entityTypeId)
      ->notice(
        '@entity.bundle.id: reverted %entity.label revision @entity.revision.id.',
        $args,
      );

    $this
      ->messenger()
      ->addStatus($this->t(
        '@entity.bundle.id %entity.label has been reverted to the revision from @entity.revision.date.',
        $args,
      ));

    $form_state->setRedirect(
      "entity.$entityTypeId.revision_history",
      [
        $entityTypeId => $this->revision->id(),
      ],
    );
  }

  protected function prepareRevertedRevision(IspimPreviewImageInterface $revision, FormStateInterface $form_state): IspimPreviewImageInterface {
    $revision->setNewRevision();
    $revision->isDefaultRevision(TRUE);

    return $revision;
  }

}

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

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