function BundleEntityDeleteForm::buildForm

File

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

Class

BundleEntityDeleteForm
A generic entity delete form for entities which are bundles.

Namespace

Drupal\Core\Entity\Form

Code

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);
}

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