class RevisionDeleteForm

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Entity/Form/RevisionDeleteForm.php \Drupal\Core\Entity\Form\RevisionDeleteForm

Provides a form for deleting an entity revision.

@internal

Hierarchy

Expanded class hierarchy of RevisionDeleteForm

2 files declare their use of RevisionDeleteForm
block_content.install in core/modules/block_content/block_content.install
Install, update and uninstall functions for the block_content module.
taxonomy.install in core/modules/taxonomy/taxonomy.install
Install, update and uninstall functions for the taxonomy module.

File

core/lib/Drupal/Core/Entity/Form/RevisionDeleteForm.php, line 26

Namespace

Drupal\Core\Entity\Form
View source
class RevisionDeleteForm extends ConfirmFormBase implements EntityFormInterface {
  
  /**
   * The entity operation.
   *
   * @var string
   */
  protected string $operation;
  
  /**
   * The entity revision.
   *
   * @var \Drupal\Core\Entity\RevisionableInterface
   */
  protected RevisionableInterface $revision;
  
  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected ModuleHandlerInterface $moduleHandler;
  
  /**
   * Creates a new RevisionDeleteForm instance.
   *
   * @param \Drupal\Core\Datetime\DateFormatterInterface $dateFormatter
   *   The date formatter.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   Entity type manager.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundleInformation
   *   The bundle information.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger service.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The time service.
   * @param \Drupal\Core\Session\AccountInterface $currentUser
   *   The current user.
   */
  public function __construct(protected DateFormatterInterface $dateFormatter, protected EntityTypeManagerInterface $entityTypeManager, protected EntityTypeBundleInfoInterface $bundleInformation, MessengerInterface $messenger, protected TimeInterface $time, protected AccountInterface $currentUser) {
    $this->messenger = $messenger;
  }
  
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container->get('date.formatter'), $container->get('entity_type.manager'), $container->get('entity_type.bundle.info'), $container->get('messenger'), $container->get('datetime.time'), $container->get('current_user'));
  }
  
  /**
   * {@inheritdoc}
   */
  public function getBaseFormId() {
    return $this->revision
      ->getEntityTypeId() . '_revision_delete';
  }
  
  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return $this->revision
      ->getEntityTypeId() . '_revision_delete';
  }
  
  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return $this->getEntity() instanceof RevisionLogInterface ? $this->t('Are you sure you want to delete the revision from %revision-date?', [
      '%revision-date' => $this->dateFormatter
        ->format($this->getEntity()
        ->getRevisionCreationTime()),
    ]) : $this->t('Are you sure you want to delete the revision?');
  }
  
  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    return $this->getEntity()
      ->getEntityType()
      ->hasLinkTemplate('version-history') && $this->getEntity()
      ->toUrl('version-history')
      ->access($this->currentUser) ? $this->getEntity()
      ->toUrl('version-history') : $this->getEntity()
      ->toUrl();
  }
  
  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return $this->t('Delete');
  }
  
  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return '';
  }
  
  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $entityTypeId = $this->revision
      ->getEntityTypeId();
    /** @var \Drupal\Core\Entity\RevisionableStorageInterface $entityStorage */
    $entityStorage = $this->entityTypeManager
      ->getStorage($entityTypeId);
    $entityStorage->deleteRevision($this->revision
      ->getRevisionId());
    $bundleLabel = $this->getBundleLabel($this->revision);
    $messengerArgs = [
      '@type' => $bundleLabel ?? $this->revision
        ->getEntityType()
        ->getLabel(),
      '%title' => $this->revision
        ->label(),
    ];
    if ($this->revision instanceof RevisionLogInterface) {
      $messengerArgs['%revision-date'] = $this->dateFormatter
        ->format($this->revision
        ->getRevisionCreationTime());
      $this->messenger
        ->addStatus($this->t('Revision from %revision-date of @type %title has been deleted.', $messengerArgs));
    }
    else {
      $this->messenger
        ->addStatus($this->t('Revision of @type %title has been deleted.', $messengerArgs));
    }
    $this->logger($this->revision
      ->getEntityType()
      ->getProvider())
      ->info('@type: deleted %title revision %revision.', [
      '@type' => $this->revision
        ->bundle(),
      '%title' => $this->revision
        ->label(),
      '%revision' => $this->revision
        ->getRevisionId(),
    ]);
    // When there is one remaining revision or more, redirect to the version
    // history page.
    if ($this->revision
      ->hasLinkTemplate('version-history')) {
      $query = $this->entityTypeManager
        ->getStorage($entityTypeId)
        ->getQuery();
      $remainingRevisions = $query->accessCheck(FALSE)
        ->allRevisions()
        ->condition($this->revision
        ->getEntityType()
        ->getKey('id'), $this->revision
        ->id())
        ->count()
        ->execute();
      $versionHistoryUrl = $this->revision
        ->toUrl('version-history');
      if ($remainingRevisions && $versionHistoryUrl->access($this->currentUser())) {
        $form_state->setRedirectUrl($versionHistoryUrl);
      }
    }
    if (!$form_state->getRedirect()) {
      $canonicalUrl = $this->revision
        ->toUrl();
      if ($canonicalUrl->access($this->currentUser())) {
        $form_state->setRedirectUrl($canonicalUrl);
      }
    }
  }
  
  /**
   * Returns the bundle label of an entity.
   *
   * @param \Drupal\Core\Entity\RevisionableInterface $entity
   *   The entity.
   *
   * @return string|null
   *   The bundle label.
   */
  protected function getBundleLabel(RevisionableInterface $entity) : ?string {
    $bundleInfo = $this->bundleInformation
      ->getBundleInfo($entity->getEntityTypeId());
    return isset($bundleInfo[$entity->bundle()]['label']) ? (string) $bundleInfo[$entity->bundle()]['label'] : NULL;
  }
  
  /**
   * {@inheritdoc}
   */
  public function setOperation($operation) {
    $this->operation = $operation;
    return $this;
  }
  
  /**
   * {@inheritdoc}
   */
  public function getOperation() {
    return $this->operation;
  }
  
  /**
   * {@inheritdoc}
   */
  public function getEntity() {
    return $this->revision;
  }
  
  /**
   * {@inheritdoc}
   */
  public function setEntity(EntityInterface $entity) {
    assert($entity instanceof RevisionableInterface);
    $this->revision = $entity;
    return $this;
  }
  
  /**
   * {@inheritdoc}
   */
  public function getEntityFromRouteMatch(RouteMatchInterface $route_match, $entity_type_id) {
    return $route_match->getParameter($entity_type_id . '_revision');
  }
  
  /**
   * {@inheritdoc}
   */
  public function buildEntity(array $form, FormStateInterface $form_state) {
    return $this->revision;
  }
  
  /**
   * {@inheritdoc}
   *
   * The save() method is not used in RevisionDeleteForm. This
   * overrides the default implementation that saves the entity.
   *
   * Confirmation forms should override submitForm() instead for their logic.
   */
  public function save(array $form, FormStateInterface $form_state) {
    throw new \LogicException('The save() method is not used in RevisionDeleteForm');
  }
  
  /**
   * {@inheritdoc}
   */
  public function setModuleHandler(ModuleHandlerInterface $module_handler) {
    $this->moduleHandler = $module_handler;
    return $this;
  }
  
  /**
   * {@inheritdoc}
   */
  public function setEntityTypeManager(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
    return $this;
  }
  
  /**
   * {@inheritdoc}
   */
  protected function currentUser() {
    return $this->currentUser;
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ConfirmFormBase::buildForm public function Form constructor. Overrides FormInterface::buildForm 26
ConfirmFormBase::getCancelText public function Returns a caption for the link which cancels the action. Overrides ConfirmFormInterface::getCancelText 2
ConfirmFormBase::getFormName public function Returns the internal name used to refer to the confirmation item. Overrides ConfirmFormInterface::getFormName
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 57
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 16
MessengerTrait::messenger public function Gets the messenger. 16
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 2
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
RevisionDeleteForm::$moduleHandler protected property The module handler.
RevisionDeleteForm::$operation protected property The entity operation.
RevisionDeleteForm::$revision protected property The entity revision.
RevisionDeleteForm::buildEntity public function Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface::buildEntity
RevisionDeleteForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
RevisionDeleteForm::currentUser protected function Gets the current user. Overrides FormBase::currentUser
RevisionDeleteForm::getBaseFormId public function Returns a string identifying the base form. Overrides BaseFormIdInterface::getBaseFormId
RevisionDeleteForm::getBundleLabel protected function Returns the bundle label of an entity.
RevisionDeleteForm::getCancelUrl public function Returns the route to go to if the user cancels the action. Overrides ConfirmFormInterface::getCancelUrl
RevisionDeleteForm::getConfirmText public function Returns a caption for the button that confirms the action. Overrides ConfirmFormBase::getConfirmText
RevisionDeleteForm::getDescription public function Returns additional text to display as a description. Overrides ConfirmFormBase::getDescription
RevisionDeleteForm::getEntity public function Gets the form entity. Overrides EntityFormInterface::getEntity
RevisionDeleteForm::getEntityFromRouteMatch public function Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface::getEntityFromRouteMatch
RevisionDeleteForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
RevisionDeleteForm::getOperation public function Gets the operation identifying the form. Overrides EntityFormInterface::getOperation
RevisionDeleteForm::getQuestion public function Returns the question to ask the user. Overrides ConfirmFormInterface::getQuestion
RevisionDeleteForm::save public function The save() method is not used in RevisionDeleteForm. This
overrides the default implementation that saves the entity.
Overrides EntityFormInterface::save
RevisionDeleteForm::setEntity public function Sets the form entity. Overrides EntityFormInterface::setEntity
RevisionDeleteForm::setEntityTypeManager public function Sets the entity type manager for this form. Overrides EntityFormInterface::setEntityTypeManager
RevisionDeleteForm::setModuleHandler public function Sets the module handler for this form. Overrides EntityFormInterface::setModuleHandler
RevisionDeleteForm::setOperation public function Sets the operation for this form. Overrides EntityFormInterface::setOperation
RevisionDeleteForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
RevisionDeleteForm::__construct public function Creates a new RevisionDeleteForm instance.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.