class BundleEntityDeleteForm

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

A generic entity delete form for entities which are bundles.

Hierarchy

Expanded class hierarchy of BundleEntityDeleteForm

Related topics

6 files declare their use of BundleEntityDeleteForm
BlockContentType.php in core/modules/block_content/src/Entity/BlockContentType.php
BundleDeleteFormTest.php in core/tests/Drupal/KernelTests/Core/Entity/BundleDeleteFormTest.php
CommentTypeDeleteForm.php in core/modules/comment/src/Form/CommentTypeDeleteForm.php
EntityTestBundle.php in core/modules/system/tests/modules/entity_test/src/Entity/EntityTestBundle.php
MediaType.php in core/modules/media/src/Entity/MediaType.php

... See full list

File

core/lib/Drupal/Core/Entity/Form/BundleEntityDeleteForm.php, line 15

Namespace

Drupal\Core\Entity\Form
View source
class BundleEntityDeleteForm extends EntityDeleteForm {
  
  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $bundle_entity_type = $this->entity
      ->getEntityType();
    $content_entity_type_id = $bundle_entity_type->getBundleOf();
    if ($content_entity_type_id === NULL) {
      throw new \LogicException(sprintf('Entity type %s uses BundleEntityDeleteForm but is not a bundle entity type.', $this->entity
        ->getEntityTypeId()));
    }
    $content_entity_type = $this->entityTypeManager
      ->getDefinition($content_entity_type_id);
    $prevent_deletion = FALSE;
    $num_content_entities = $this->getContentEntityCount();
    if ($num_content_entities > 0) {
      $prevent_deletion = TRUE;
      $form['existing_entities'] = [
        '#type' => 'html_tag',
        '#tag' => 'p',
        '#value' => $this->formatPlural($num_content_entities, '%type is used by 1 @singular-content-label on your site. You may not remove this @bundle-label until you have removed all of the %type @plural-content-label.', '%type is used by @count @plural-content-label on your site. You may not remove this @bundle-label until you have removed all of the %type @plural-content-label.', [
          '%type' => $this->entity
            ->label(),
          '@bundle-label' => $bundle_entity_type->getSingularLabel(),
          '@singular-content-label' => $content_entity_type->getSingularLabel(),
          '@plural-content-label' => $content_entity_type->getPluralLabel(),
        ]),
      ];
    }
    $additional_reasons = $this->getAdditionalReasonsToPreventDeletion();
    if (count($additional_reasons) > 0) {
      $prevent_deletion = TRUE;
      foreach ($additional_reasons as $reason) {
        $form['additional_reasons'][] = [
          '#type' => 'html_tag',
          '#tag' => 'p',
          '#value' => $reason,
        ];
      }
    }
    // If we are blocking deletion, don't show the delete button or the config
    // changes that the parent class adds.
    if ($prevent_deletion) {
      // Change the title, as the user can't delete.
      $form['#title'] = $this->t("Unable to delete @bundle-label %type", [
        '%type' => $this->entity
          ->label(),
        '@bundle-label' => $bundle_entity_type->getSingularLabel(),
      ]);
      // Retrieve and add the form actions array but without the submit button.
      // We need to show the cancel button for non-JS users showing the form
      // on a standalone page instead of a modal.
      $actions = $this->actionsElement($form, $form_state);
      unset($actions['submit']);
      if (!empty($actions)) {
        $form['actions'] = $actions;
      }
      return $form;
    }
    return parent::buildForm($form, $form_state);
  }
  
  /**
   * Gets the count of content entities of this form's bundle.
   *
   * This is handled separately from additional reasons to make it easier for
   * entity types to customize.
   *
   * @return int
   *   The number of entities.
   */
  protected function getContentEntityCount() : int {
    $bundle_entity_type = $this->entity
      ->getEntityType();
    $content_entity_type_id = $bundle_entity_type->getBundleOf();
    $content_entity_type = $this->entityTypeManager
      ->getDefinition($content_entity_type_id);
    $num_content_entities = $this->entityTypeManager
      ->getStorage($content_entity_type_id)
      ->getQuery()
      ->accessCheck(FALSE)
      ->condition($content_entity_type->getKey('bundle'), $this->entity
      ->id())
      ->count()
      ->execute();
    return $num_content_entities;
  }
  
  /**
   * Gets a list of additional reasons preventing deletion of the bundle entity.
   *
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup[]
   *   An array of translated strings, each one giving a separate reason for
   *   preventing deletion. These are shown each in a paragraph on the form. If
   *   the array is empty, and there are no content entities, then deletion is
   *   allowed.
   */
  protected function getAdditionalReasonsToPreventDeletion() : array {
    return [];
  }

}

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