devel_wizard-2.x-dev/templates/spell/entity_type/content/revision_revert_form.php.twig

templates/spell/entity_type/content/revision_revert_form.php.twig
{#
@todo
  Dependency injection for
  - ::$stringTranslation
  - ::$messenger
#}
{%
  include '@devel_wizard/php/devel_wizard.php.file.header.php.twig'
  with {
    'namespace': content.namespace,
  }
%}

use {{ content.interface_fqn }};
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 Symfony\Component\DependencyInjection\ContainerInterface;

class RevisionRevertForm extends ConfirmFormBase {

  protected string $entityTypeId = '{{ content.id }}';

  protected string $bundleEntityTypeId = '{{ config.id }}';

  protected {{ content.interface }} $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 int|string|null ${{ content.id }}_revision
   */
  public function buildForm(array $form, FormStateInterface $form_state, ${{ content.id }}_revision = NULL): array {
    /* @noinspection PhpUnhandledExceptionInspection */
    /** @var \{{ content.namespace }}\StorageInterface $storage */
    $storage = $this->entityTypeManager->getStorage($this->entityTypeId);
    $this->revision = $storage->loadRevision(${{ content.id }}_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({{ content.interface }} $revision, FormStateInterface $form_state): {{ content.interface }} {
    $revision->setNewRevision();
    $revision->isDefaultRevision(TRUE);

    return $revision;
  }
}

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

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