You are here

public function SupportTicketForm::save in Support Ticketing System 8

Form submission handler for the 'save' action.

Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.

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

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides EntityForm::save

File

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

Class

SupportTicketForm
Form controller for the support ticket edit forms.

Namespace

Drupal\support_ticket

Code

public function save(array $form, FormStateInterface $form_state) {
  $support_ticket = $this->entity;
  $insert = $support_ticket
    ->isNew();
  $support_ticket
    ->save();
  $support_ticket_link = $support_ticket
    ->link($this
    ->t('View'));
  $context = array(
    '@support_ticket_type' => $support_ticket
      ->getType(),
    '%title' => $support_ticket
      ->label(),
    'link' => $support_ticket_link,
  );
  $t_args = array(
    '@support_ticket_type' => support_ticket_get_type_label($support_ticket),
    '%title' => $support_ticket
      ->label(),
  );
  if ($insert) {
    $this
      ->logger('content')
      ->notice('@support_ticket_type: added %title.', $context);
    drupal_set_message(t('@support_ticket_type %title has been created.', $t_args));
  }
  else {
    $this
      ->logger('content')
      ->notice('@support_ticket_type: updated %title.', $context);
    drupal_set_message(t('@support_ticket_type %title has been updated.', $t_args));
  }
  if ($support_ticket
    ->id()) {
    $form_state
      ->setValue('stid', $support_ticket
      ->id());
    $form_state
      ->set('stid', $support_ticket
      ->id());
    $form_state
      ->setRedirect('entity.support_ticket.canonical', array(
      'support_ticket' => $support_ticket
        ->id(),
    ));

    // Remove the preview entry from the temp store, if any.
    $store = $this->tempStoreFactory
      ->get('support_ticket_preview');
    $store
      ->delete($support_ticket
      ->uuid());
  }
  else {

    // In the unlikely case something went wrong on save, the support_ticket will be
    // rebuilt and support_ticket form redisplayed the same way as in preview.
    drupal_set_message(t('The post could not be saved.'), 'error');
    $form_state
      ->setRebuild();
  }
}