You are here

public function CacheflushDeleteMultiple::submitForm in CacheFlush 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

modules/cacheflush_ui/src/Form/CacheflushDeleteMultiple.php, line 177

Class

CacheflushDeleteMultiple
Provides a cacheflush deletion confirmation form.

Namespace

Drupal\cacheflush_ui\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  if ($form_state
    ->getValue('confirm') && !empty($this->cacheflushInfo)) {
    $total_count = 0;
    $delete_entities = [];
    $delete_translations = [];
    $entities = $this->storage
      ->loadMultiple(array_keys($this->cacheflushInfo));
    foreach ($this->cacheflushInfo 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('cacheflush')
        ->notice('Deleted @count cacheflush 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('cacheflush')
          ->notice('Deleted @count cacheflush translations.', [
          '@count' => $count,
        ]);
      }
    }
    if ($total_count) {
      $this->messenger
        ->addMessage($this
        ->formatPlural($total_count, 'Deleted 1 cacheflush entity.', 'Deleted @count cacheflush entities.'));
    }
    $this->tempStoreFactory
      ->get('cacheflush_multiple_delete_confirm')
      ->delete($this->currentUser
      ->id());
  }
  $form_state
    ->setRedirect('entity.cacheflush.collection');
}