You are here

public function SupportTicketTypeForm::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/SupportTicketTypeForm.php, line 216
Contains \Drupal\support_ticket\SupportTicketTypeForm.

Class

SupportTicketTypeForm
Form controller for support ticket type forms.

Namespace

Drupal\support_ticket

Code

public function save(array $form, FormStateInterface $form_state) {
  $type = $this->entity;
  $type
    ->setNewRevision($form_state
    ->getValue(array(
    'options',
    'revision',
  )));
  $type
    ->set('type', trim($type
    ->id()));
  $type
    ->set('name', trim($type
    ->label()));
  $status = $type
    ->save();
  $t_args = array(
    '%name' => $type
      ->label(),
  );
  if ($status == SAVED_UPDATED) {
    drupal_set_message(t('The support ticket type %name has been updated.', $t_args));
  }
  elseif ($status == SAVED_NEW) {
    support_ticket_add_body_field($type);

    // @todo
    drupal_set_message(t('The support ticket type %name has been added.', $t_args));
    $context = array_merge($t_args, array(
      'link' => $type
        ->link($this
        ->t('View'), 'collection'),
    ));
    $this
      ->logger('support_ticket')
      ->notice('Added support ticket type %name.', $context);
  }
  $fields = $this->entityManager
    ->getFieldDefinitions('support_ticket', $type
    ->id());

  // Update title field definition.
  $title_field = $fields['title'];
  $title_label = $form_state
    ->getValue('title_label');
  if ($title_field
    ->getLabel() != $title_label) {
    $title_field
      ->getConfig($type
      ->id())
      ->setLabel($title_label)
      ->save();
  }

  // Update workflow options.
  $support_ticket = $this->entityManager
    ->getStorage('support_ticket')
    ->create(array(
    'support_ticket_type' => $type
      ->id(),
  ));
  foreach (array(
    'status',
    'locked',
  ) as $field_name) {
    $value = (bool) $form_state
      ->getValue([
      'options',
      $field_name,
    ]);
    if ($support_ticket->{$field_name}->value != $value) {
      $fields[$field_name]
        ->getConfig($type
        ->id())
        ->setDefaultValue($value)
        ->save();
    }
  }
  $this->entityManager
    ->clearCachedFieldDefinitions();
  $form_state
    ->setRedirectUrl($type
    ->urlInfo('collection'));
}