function DeleteMultipleForm::buildForm

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Entity/Form/DeleteMultipleForm.php \Drupal\Core\Entity\Form\DeleteMultipleForm::buildForm()
  2. 10 core/lib/Drupal/Core/Entity/Form/DeleteMultipleForm.php \Drupal\Core\Entity\Form\DeleteMultipleForm::buildForm()
  3. 11.x core/lib/Drupal/Core/Entity/Form/DeleteMultipleForm.php \Drupal\Core\Entity\Form\DeleteMultipleForm::buildForm()

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

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