You are here

public function DeleteMultipleForm::buildForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/Form/DeleteMultipleForm.php \Drupal\Core\Entity\Form\DeleteMultipleForm::buildForm()

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 ConfirmFormBase::buildForm

File

core/lib/Drupal/Core/Entity/Form/DeleteMultipleForm.php, line 151

Class

DeleteMultipleForm
Provides an entities deletion confirmation form.

Namespace

Drupal\Core\Entity\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL) {
  $this->entityTypeId = $entity_type_id;
  $this->entityType = $this->entityTypeManager
    ->getDefinition($this->entityTypeId);
  $this->selection = $this->tempStore
    ->get($this->currentUser
    ->id() . ':' . $entity_type_id);
  if (empty($this->entityTypeId) || empty($this->selection)) {
    return new RedirectResponse($this
      ->getCancelUrl()
      ->setAbsolute()
      ->toString());
  }
  $items = [];
  $entities = $this->entityTypeManager
    ->getStorage($entity_type_id)
    ->loadMultiple(array_keys($this->selection));
  foreach ($this->selection as $id => $selected_langcodes) {
    $entity = $entities[$id];
    foreach ($selected_langcodes as $langcode) {
      $key = $id . ':' . $langcode;
      if ($entity instanceof TranslatableInterface) {
        $entity = $entity
          ->getTranslation($langcode);
        $default_key = $id . ':' . $entity
          ->getUntranslated()
          ->language()
          ->getId();

        // Build a nested list of translations that will be deleted if the
        // entity has multiple translations.
        $entity_languages = $entity
          ->getTranslationLanguages();
        if (count($entity_languages) > 1 && $entity
          ->isDefaultTranslation()) {
          $names = [];
          foreach ($entity_languages as $translation_langcode => $language) {
            $names[] = $language
              ->getName();
            unset($items[$id . ':' . $translation_langcode]);
          }
          $items[$default_key] = [
            'label' => [
              '#markup' => $this
                ->t('@label (Original translation) - <em>The following @entity_type translations will be deleted:</em>', [
                '@label' => $entity
                  ->label(),
                '@entity_type' => $this->entityType
                  ->getSingularLabel(),
              ]),
            ],
            'deleted_translations' => [
              '#theme' => 'item_list',
              '#items' => $names,
            ],
          ];
        }
        elseif (!isset($items[$default_key])) {
          $items[$key] = $entity
            ->label();
        }
      }
      elseif (!isset($items[$key])) {
        $items[$key] = $entity
          ->label();
      }
    }
  }
  $form['entities'] = [
    '#theme' => 'item_list',
    '#items' => $items,
  ];
  $form = parent::buildForm($form, $form_state);
  return $form;
}