You are here

public function DeleteMultiple::submitForm in MERCI (Manage Equipment Reservations, Checkout and Inventory) 8.2

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/merci_line_item/src/Form/DeleteMultiple.php, line 145

Class

DeleteMultiple
Provides a node deletion confirmation form.

Namespace

Drupal\merci_line_item\Form

Code

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

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

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