You are here

public function DeleteMultiple::buildForm in Support Ticketing System 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfirmFormBase::buildForm

File

modules/support_ticket/src/Form/DeleteMultiple.php, line 98
Contains \Drupal\support_ticket\Form\DeleteMultiple.

Class

DeleteMultiple
Provides a support ticket deletion confirmation form.

Namespace

Drupal\support_ticket\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $this->supportTicketInfo = $this->tempStoreFactory
    ->get('support_ticket_multiple_delete_confirm')
    ->get(\Drupal::currentUser()
    ->id());
  if (empty($this->supportTicketInfo)) {
    return new RedirectResponse($this
      ->getCancelUrl()
      ->setAbsolute()
      ->toString());
  }

  /** @var \Drupal\support_ticket\SupportTicketInterface[] $support_tickets */
  $support_tickets = $this->storage
    ->loadMultiple(array_keys($this->supportTicketInfo));
  $items = [];
  foreach ($this->supportTicketInfo as $id => $langcodes) {
    foreach ($langcodes as $langcode) {
      $support_ticket = $support_tickets[$id]
        ->getTranslation($langcode);
      $key = $id . ':' . $langcode;
      $default_key = $id . ':' . $support_ticket
        ->getUntranslated()
        ->language()
        ->getId();

      // If we have a translated entity we build a nested list of translations
      // that will be deleted.
      $languages = $support_ticket
        ->getTranslationLanguages();
      if (count($languages) > 1 && $support_ticket
        ->isDefaultTranslation()) {
        $names = [];
        foreach ($languages as $translation_langcode => $language) {
          $names[] = $language
            ->getName();
          unset($items[$id . ':' . $translation_langcode]);
        }
        $items[$default_key] = [
          'label' => [
            '#markup' => $this
              ->t('@label (Original translation) - <em>The following ticket translations will be deleted:</em>', [
              '@label' => $support_ticket
                ->label(),
            ]),
          ],
          'deleted_translations' => [
            '#theme' => 'item_list',
            '#items' => $names,
          ],
        ];
      }
      elseif (!isset($items[$default_key])) {
        $items[$key] = $support_ticket
          ->label();
      }
    }
  }
  $form['support_tickets'] = array(
    '#theme' => 'item_list',
    '#items' => $items,
  );
  $form = parent::buildForm($form, $form_state);
  return $form;
}