You are here

public function DeleteMultiple::submitForm in Media entity 8

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 148

Class

DeleteMultiple
Provides a media deletion confirmation form.

Namespace

Drupal\media_entity\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  if ($form_state
    ->getValue('confirm') && !empty($this->entityInfo)) {
    $total_count = 0;
    $delete_entities = [];

    /** @var \Drupal\Core\Entity\ContentEntityInterface[][] $delete_translations */
    $delete_translations = [];

    /** @var \Drupal\media_entity\MediaInterface[] $entities */
    $entities = $this->storage
      ->loadMultiple(array_keys($this->entityInfo));
    foreach ($this->entityInfo 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) {
      $this->storage
        ->delete($delete_entities);
      $this
        ->logger('media_entity')
        ->notice('Deleted @count media entities.', [
        '@count' => count($delete_entities),
      ]);
    }
    if ($delete_translations) {
      $count = 0;
      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('media_entity')
          ->notice('Deleted @count media translations.', [
          '@count' => $count,
        ]);
      }
    }
    if ($total_count) {
      drupal_set_message($this
        ->formatPlural($total_count, 'Deleted 1 media entity.', 'Deleted @count media entities.'));
    }
    $this->tempStoreFactory
      ->get('media_multiple_delete_confirm')
      ->delete(\Drupal::currentUser()
      ->id());
  }
  $form_state
    ->setRedirect('system.admin_content');
}