You are here

public function DeleteMultiple::submitForm in Log 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 185
Contains \Drupal\log\Form\DeleteMultiple.

Class

DeleteMultiple
Provides a log deletion confirmation form.

Namespace

Drupal\log\Form

Code

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

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

    /** @var \Drupal\log\LogInterface[] $logs */
    $logs = $this->storage
      ->loadMultiple(array_keys($this->logInfo));
    foreach ($this->logInfo as $id => $langcodes) {
      foreach ($langcodes as $langcode) {
        $log = $logs[$id]
          ->getTranslation($langcode);
        if ($log
          ->isDefaultTranslation()) {
          $delete_logs[$id] = $log;
          unset($delete_translations[$id]);
          $total_count += count($log
            ->getTranslationLanguages());
        }
        elseif (!isset($delete_logs[$id])) {
          $delete_translations[$id][] = $log;
        }
      }
    }
    if ($delete_logs) {
      $this->storage
        ->delete($delete_logs);
      $this
        ->logger('log')
        ->notice('Deleted @count posts.', array(
        '@count' => count($delete_logs),
      ));
    }
    if ($delete_translations) {
      $count = 0;
      foreach ($delete_translations as $id => $translations) {
        $log = $logs[$id]
          ->getUntranslated();
        foreach ($translations as $translation) {
          $log
            ->removeTranslation($translation
            ->language()
            ->getId());
        }
        $log
          ->save();
        $count += count($translations);
      }
      if ($count) {
        $total_count += $count;
        $this
          ->logger('log')
          ->notice('Deleted @count content translations.', array(
          '@count' => $count,
        ));
      }
    }
    if ($total_count) {
      drupal_set_message($this
        ->formatPlural($total_count, 'Deleted 1 post.', 'Deleted @count posts.'));
    }
    $this->tempStoreFactory
      ->get('log_multiple_delete_confirm')
      ->delete(\Drupal::currentUser()
      ->id());
  }
  $form_state
    ->setRedirectUrl($this
    ->getCancelUrl());
}