You are here

public function DeleteMultipleForm::submitForm in Drupal 10

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

Form submission handler.

Parameters

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

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

Overrides FormInterface::submitForm

File

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

Class

DeleteMultipleForm
Provides an entities deletion confirmation form.

Namespace

Drupal\Core\Entity\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $total_count = 0;
  $delete_entities = [];
  $delete_translations = [];
  $inaccessible_entities = [];
  $storage = $this->entityTypeManager
    ->getStorage($this->entityTypeId);
  $entities = $storage
    ->loadMultiple(array_keys($this->selection));
  foreach ($this->selection as $id => $selected_langcodes) {
    $entity = $entities[$id];
    if (!$entity
      ->access('delete', $this->currentUser)) {
      $inaccessible_entities[] = $entity;
      continue;
    }
    foreach ($selected_langcodes as $langcode) {
      if ($entity instanceof TranslatableInterface) {
        $entity = $entity
          ->getTranslation($langcode);

        // If the entity is the default translation then deleting it will
        // delete all the translations.
        if ($entity
          ->isDefaultTranslation()) {
          $delete_entities[$id] = $entity;

          // If there are translations already marked for deletion then remove
          // them as they will be deleted anyway.
          unset($delete_translations[$id]);

          // Update the total count. Since a single delete will delete all
          // translations, we need to add the number of translations to the
          // count.
          $total_count += count($entity
            ->getTranslationLanguages());
        }
        elseif (!isset($delete_entities[$id])) {
          $delete_translations[$id][] = $entity;
        }
      }
      elseif (!isset($delete_entities[$id])) {
        $delete_entities[$id] = $entity;
        $total_count++;
      }
    }
  }
  if ($delete_entities) {
    $storage
      ->delete($delete_entities);
    foreach ($delete_entities as $entity) {
      $this
        ->logger($entity
        ->getEntityType()
        ->getProvider())
        ->notice('The @entity-type %label has been deleted.', [
        '@entity-type' => $entity
          ->getEntityType()
          ->getSingularLabel(),
        '%label' => $entity
          ->label(),
      ]);
    }
  }
  if ($delete_translations) {

    /** @var \Drupal\Core\Entity\TranslatableInterface[][] $delete_translations */
    foreach ($delete_translations as $id => $translations) {
      $entity = $entities[$id]
        ->getUntranslated();
      foreach ($translations as $translation) {
        $entity
          ->removeTranslation($translation
          ->language()
          ->getId());
      }
      $entity
        ->save();
      foreach ($translations as $translation) {
        $this
          ->logger($entity
          ->getEntityType()
          ->getProvider())
          ->notice('The @entity-type %label @language translation has been deleted.', [
          '@entity-type' => $entity
            ->getEntityType()
            ->getSingularLabel(),
          '%label' => $entity
            ->label(),
          '@language' => $translation
            ->language()
            ->getName(),
        ]);
      }
      $total_count += count($translations);
    }
  }
  if ($total_count) {
    $this->messenger
      ->addStatus($this
      ->getDeletedMessage($total_count));
  }
  if ($inaccessible_entities) {
    $this->messenger
      ->addWarning($this
      ->getInaccessibleMessage(count($inaccessible_entities)));
  }
  $this->tempStore
    ->delete($this->currentUser
    ->id());
  $form_state
    ->setRedirectUrl($this
    ->getCancelUrl());
}