You are here

public function BrokenLinkDeleteMultipleForm::submitForm in Broken Link 8.3

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/BrokenLinkDeleteMultipleForm.php, line 145

Class

BrokenLinkDeleteMultipleForm
Provides a brokenLink deletion confirmation form.

Namespace

Drupal\broken_link\Form

Code

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

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

    /** @var \Drupal\brokenLink\NodeInterface[] $brokenLinks */
    $brokenLinks = $this->storage
      ->loadMultiple(array_keys($this->brokenLinkInfo));
    foreach ($this->brokenLinkInfo as $id => $langcodes) {
      foreach ($langcodes as $langcode) {
        $brokenLink = $brokenLinks[$id]
          ->getTranslation($langcode);
        if ($brokenLink
          ->isDefaultTranslation()) {
          $delete_brokenLinks[$id] = $brokenLink;
          unset($delete_translations[$id]);
          $total_count += count($brokenLink
            ->getTranslationLanguages());
        }
        elseif (!isset($delete_brokenLinks[$id])) {
          $delete_translations[$id][] = $brokenLink;
        }
      }
    }
    if ($delete_brokenLinks) {
      $this->storage
        ->delete($delete_brokenLinks);
      $this
        ->logger('content')
        ->notice('Deleted @count posts.', [
        '@count' => count($delete_brokenLinks),
      ]);
    }
    if ($delete_translations) {
      $count = 0;
      foreach ($delete_translations as $id => $translations) {
        $brokenLink = $brokenLinks[$id]
          ->getUntranslated();
        foreach ($translations as $translation) {
          $brokenLink
            ->removeTranslation($translation
            ->language()
            ->getId());
        }
        $brokenLink
          ->save();
        $count += count($translations);
      }
      if ($count) {
        $total_count += $count;
        $this
          ->logger('content')
          ->notice('Deleted @count content translations.', [
          '@count' => $count,
        ]);
      }
    }
    if ($total_count) {
      $this
        ->messenger()
        ->addMessage($this
        ->formatPlural($total_count, 'Deleted 1 post.', 'Deleted @count posts.'), 'warning');
    }
    $this->tempStoreFactory
      ->get('brokenLink_multiple_delete_confirm')
      ->delete(\Drupal::currentUser()
      ->id());
  }
  $form_state
    ->setRedirect('system.admin_content');
}