You are here

public function DeleteMultiple::submitForm in Support Ticketing System 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/support_ticket/src/Form/DeleteMultiple.php, line 150
Contains \Drupal\support_ticket\Form\DeleteMultiple.

Class

DeleteMultiple
Provides a support ticket deletion confirmation form.

Namespace

Drupal\support_ticket\Form

Code

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

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

    /** @var \Drupal\support_ticket\SupportTicketInterface[] $support_tickets */
    $support_tickets = $this->storage
      ->loadMultiple(array_keys($this->supportTicketInfo));
    foreach ($this->supportTicketInfo as $id => $langcodes) {
      foreach ($langcodes as $langcode) {
        $support_ticket = $support_tickets[$id]
          ->getTranslation($langcode);
        if ($support_ticket
          ->isDefaultTranslation()) {
          $delete_support_tickets[$id] = $support_ticket;
          unset($delete_translations[$id]);
          $total_count += count($support_ticket
            ->getTranslationLanguages());
        }
        elseif (!isset($delete_support_tickets[$id])) {
          $delete_translations[$id][] = $support_ticket;
        }
      }
    }
    if ($delete_support_tickets) {
      $this->storage
        ->delete($delete_support_tickets);
      $this
        ->logger('content')
        ->notice('Deleted @count tickets.', array(
        '@count' => count($delete_support_tickets),
      ));
    }
    if ($delete_translations) {
      $count = 0;
      foreach ($delete_translations as $id => $translations) {
        $support_ticket = $support_tickets[$id]
          ->getUntranslated();
        foreach ($translations as $translation) {
          $support_ticket
            ->removeTranslation($translation
            ->language()
            ->getId());
        }
        $support_ticket
          ->save();
        $count += count($translations);
      }
      if ($count) {
        $total_count += $count;
        $this
          ->logger('content')
          ->notice('Deleted @count ticket translations.', array(
          '@count' => $count,
        ));
      }
    }
    if ($total_count) {
      drupal_set_message($this
        ->formatPlural($total_count, 'Deleted 1 ticket.', 'Deleted @count tickets.'));
    }
    $this->tempStoreFactory
      ->get('support_ticket_multiple_delete_confirm')
      ->delete(\Drupal::currentUser()
      ->id());
  }
  $form_state
    ->setRedirect('system.admin_support');
}