toolshed-8.x-1.x-dev/src/Entity/Form/RevisionConfirmBase.php

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

namespace Drupal\toolshed\Entity\Form;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\RevisionableInterface;
use Drupal\Core\Entity\RevisionableStorageInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a form for confirming a entity revision revert.
 */
abstract class RevisionConfirmBase extends EntityConfirmFormBase {

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

  /**
   * The datetime time class instance.
   *
   * @var \Drupal\Component\Datetime\TimeInterface
   */
  protected TimeInterface $time;

  /**
   * Constructs a new RevisionConfirmBase form class.
   *
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date formatter service.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The datetime time class instance.
   */
  public function __construct(DateFormatterInterface $date_formatter, TimeInterface $time) {
    $this->dateFormatter = $date_formatter;
    $this->time = $time;
  }

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

  /**
   * {@inheritdoc}
   */
  public function getEntityFromRouteMatch(RouteMatchInterface $route_match, $entity_type_id): EntityInterface {
    $revision = $route_match->getParameter('revision') ?: $route_match->getParameter($entity_type_id . '_revision');

    if (!$revision instanceof RevisionableInterface) {
      $entityStorage = $this->entityTypeManager->getStorage($entity_type_id);
      $entity = $entityStorage instanceof RevisionableStorageInterface
        ? $entityStorage->loadRevision($revision)
        : parent::getEntityFromRouteMatch($route_match, $entity_type_id);
    }
    else {
      $entity = $revision;
    }

    return $entity;
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription(): \Stringable|string {
    /** @var \Drupal\Core\Entity\RevisionLogInterface $entity */
    $entity = $this->getEntity();

    return $this->t('Revert revision: <p>%message</p> This action cannot be undone.', [
      '%message' => $entity->getRevisionLogMessage() ?? '',
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl(): Url {
    return $this->getEntity()->toUrl('revision-history');
  }

  /**
   * {@inheritdoc}
   */
  public function buildEntity(array $form, FormStateInterface $form_state): EntityInterface {
    // Do nothing as we are preparing to delete this entity, there typically
    // are no changes to the value generated by the form. We use the
    // \Drupal\Core\Entity\EntityForm class to allow for consistent entity form
    // routing and generation.
    return $this->entity;
  }

  /**
   * {@inheritdoc}
   *
   * @see \Drupal\toolshed\Entity\Form\RevisionConfirmBase::buildEntity()
   */
  protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state): void {
    // No need to copy values as this form doesn't change any of the content.
  }

}

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

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