You are here

public function DeleteMultiple::submitForm in Entity API 8.0

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

src/Form/DeleteMultiple.php, line 174
Contains \Drupal\entity\Form\DeleteMultiple.

Class

DeleteMultiple
Provides an entities deletion confirmation form.

Namespace

Drupal\entity\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $total_count = 0;
  $delete_entities = [];
  $delete_translations = [];
  $storage = $this->entityTypeManager
    ->getStorage($this->entityTypeId);

  /** @var \Drupal\Core\Entity\ContentEntityInterface[] $entities */
  $entities = $storage
    ->loadMultiple(array_keys($this->selection));
  foreach ($this->selection as $id => $langcodes) {
    foreach ($langcodes as $langcode) {
      $entity = $entities[$id]
        ->getTranslation($langcode);
      if ($entity
        ->isDefaultTranslation()) {
        $delete_entities[$id] = $entity;
        unset($delete_translations[$id]);
        $total_count += count($entity
          ->getTranslationLanguages());
      }
      elseif (!isset($delete_entities[$id])) {
        $delete_translations[$id][] = $entity;
      }
    }
  }
  if ($delete_entities) {
    $storage
      ->delete($delete_entities);
    $this
      ->logger('content')
      ->notice('Deleted @count @entity_type items.', [
      '@count' => count($delete_entities),
      '@entity_type' => $this->entityTypeId,
    ]);
  }
  if ($delete_translations) {
    $count = 0;

    /** @var \Drupal\Core\Entity\ContentEntityInterface[][] $delete_translations */
    foreach ($delete_translations as $id => $translations) {
      $entity = $entities[$id]
        ->getUntranslated();
      foreach ($translations as $translation) {
        $entity
          ->removeTranslation($translation
          ->language()
          ->getId());
      }
      $entity
        ->save();
      $count += count($translations);
    }
    if ($count) {
      $total_count += $count;
      $this
        ->logger('content')
        ->notice('Deleted @count @entity_type translations.', [
        '@count' => $count,
        '@entity_type' => $this->entityTypeId,
      ]);
    }
  }
  if ($total_count) {
    drupal_set_message($this
      ->formatPlural($total_count, 'Deleted 1 item.', 'Deleted @count items.'));
  }
  $this->tempStore
    ->delete($this->currentUser
    ->id());
  $form_state
    ->setRedirectUrl($this
    ->getCancelUrl());
}