You are here

public function CommerceBundleEntityDeleteFormBase::buildForm in Commerce Core 8.2

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides EntityDeleteForm::buildForm

File

src/Form/CommerceBundleEntityDeleteFormBase.php, line 16

Class

CommerceBundleEntityDeleteFormBase
Provides a generic form for deleting bundle entities.

Namespace

Drupal\commerce\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\Core\Config\Entity\ConfigEntityType $bundle_entity_type */
  $bundle_entity_type = $this->entityTypeManager
    ->getDefinition($this->entity
    ->getEntityTypeId());

  /** @var \Drupal\Core\Entity\ContentEntityType $content_entity_type */
  $content_entity_type = $this->entityTypeManager
    ->getDefinition($bundle_entity_type
    ->getBundleOf());
  $usage_count = $this->entityTypeManager
    ->getStorage($content_entity_type
    ->id())
    ->getQuery()
    ->accessCheck(FALSE)
    ->condition($content_entity_type
    ->getKey('bundle'), $this->entity
    ->id())
    ->count()
    ->execute();
  if ($usage_count) {
    $caption = '<p>' . $this
      ->formatPlural($usage_count, '%type is used by 1 %entity on your site. You cannot remove this %entity_type until you have removed all of the %type %entities.', '%type is used by @count %entities on your site. You cannot remove this %entity_type until you have removed all of the %type %entities.', [
      '%type' => $this->entity
        ->label(),
      '%entity' => $content_entity_type
        ->getSingularLabel(),
      '%entities' => $content_entity_type
        ->getPluralLabel(),
      '%entity_type' => $content_entity_type
        ->getBundleLabel(),
    ]) . '</p>';
    $form['#title'] = $this
      ->getQuestion();
    $form['description'] = [
      '#markup' => $caption,
    ];
    return $form;
  }
  return parent::buildForm($form, $form_state);
}