You are here

public function SupportTicketForm::form in Support Ticketing System 8

Gets the actual form array to be built.

Overrides ContentEntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

modules/support_ticket/src/SupportTicketForm.php, line 73
Contains \Drupal\support_ticket\SupportTicketForm.

Class

SupportTicketForm
Form controller for the support ticket edit forms.

Namespace

Drupal\support_ticket

Code

public function form(array $form, FormStateInterface $form_state) {

  // Try to restore from temp store, this must be done before calling
  // parent::form().
  $uuid = $this->entity
    ->uuid();
  $store = $this->tempStoreFactory
    ->get('support_ticket_preview');

  // If the user is creating a new support_ticket, the UUID is passed in the request.
  if ($request_uuid = \Drupal::request()->query
    ->get('uuid')) {
    $uuid = $request_uuid;
  }
  if ($preview = $store
    ->get($uuid)) {

    /** @var $preview \Drupal\Core\Form\FormStateInterface */
    $form_state = $preview;

    // Rebuild the form.
    $form_state
      ->setRebuild();
    $this->entity = $preview
      ->getFormObject()
      ->getEntity();
    unset($this->entity->in_preview);

    // Remove the stale temp store entry for existing support tickets.
    if (!$this->entity
      ->isNew()) {
      $store
        ->delete($uuid);
    }
    $this->hasBeenPreviewed = TRUE;
  }

  /** @var \Drupal\support_ticket\SupportTicketInterface $support_ticket */
  $support_ticket = $this->entity;
  if ($this->operation == 'edit') {
    $form['#title'] = $this
      ->t('<em>Edit @support_ticket_type</em> @title', array(
      '@support_ticket_type' => support_ticket_get_type_label($support_ticket),
      '@title' => $support_ticket
        ->label(),
    ));
  }
  $current_user = $this
    ->currentUser();

  // Changed must be sent to the client, for later overwrite error checking.
  $form['changed'] = array(
    '#type' => 'hidden',
    '#default_value' => $support_ticket
      ->getChangedTime(),
  );
  $form['advanced'] = array(
    '#type' => 'vertical_tabs',
    '#attributes' => array(
      'class' => array(
        'entity-meta',
      ),
    ),
    '#weight' => 99,
  );
  $form = parent::form($form, $form_state);

  // Add a revision_log field if the "Create new revision" option is checked,
  // or if the current user has the ability to check that option.
  $form['revision_information'] = array(
    '#type' => 'details',
    '#group' => 'advanced',
    '#title' => t('Revision information'),
    // Open by default when "Create new revision" is checked.
    '#open' => $support_ticket
      ->isNewRevision(),
    '#attributes' => array(
      'class' => array(
        'support-ticket-form-revision-information',
      ),
    ),
    '#attached' => array(
      'library' => array(
        'support_ticket/drupal.support_ticket',
      ),
    ),
    '#weight' => 20,
    '#optional' => TRUE,
  );
  $form['revision'] = array(
    '#type' => 'checkbox',
    '#title' => t('Create new revision'),
    '#default_value' => $support_ticket->support_ticket_type->entity
      ->isNewRevision(),
    '#access' => $current_user
      ->hasPermission('administer support tickets'),
    '#group' => 'revision_information',
  );
  $form['revision_log'] += array(
    '#states' => array(
      'visible' => array(
        ':input[name="revision"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
    '#group' => 'revision_information',
  );

  // Support ticket author information for administrators.
  $form['author'] = array(
    '#type' => 'details',
    '#title' => t('Authoring information'),
    '#group' => 'advanced',
    '#attributes' => array(
      'class' => array(
        'support-ticket-form-author',
      ),
    ),
    '#attached' => array(
      'library' => array(
        'support_ticket/drupal.support_ticket',
      ),
    ),
    '#weight' => 90,
    '#optional' => TRUE,
  );
  if (isset($form['uid'])) {
    $form['uid']['#group'] = 'author';
  }
  if (isset($form['created'])) {
    $form['created']['#group'] = 'author';
  }

  // Support ticket options for administrators.
  $form['options'] = array(
    '#type' => 'details',
    '#title' => t('Locking options'),
    '#group' => 'advanced',
    '#attributes' => array(
      'class' => array(
        'support-ticket-form-options',
      ),
    ),
    '#attached' => array(
      'library' => array(
        'support_ticket/drupal.support_ticket',
      ),
    ),
    '#weight' => 95,
    '#optional' => TRUE,
  );
  if (isset($form['locked'])) {
    $form['locked']['#group'] = 'options';
  }
  $form['#attached']['library'][] = 'support_ticket/form';
  $form['#entity_builders']['update_status'] = [
    $this,
    'updateStatus',
  ];
  return $form;
}