You are here

public function VocabularyForm::save in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/taxonomy/src/VocabularyForm.php \Drupal\taxonomy\VocabularyForm::save()

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

core/modules/taxonomy/src/VocabularyForm.php, line 121
Contains \Drupal\taxonomy\VocabularyForm.

Class

VocabularyForm
Base form for vocabulary edit forms.

Namespace

Drupal\taxonomy

Code

public function save(array $form, FormStateInterface $form_state) {
  $vocabulary = $this->entity;

  // Prevent leading and trailing spaces in vocabulary names.
  $vocabulary
    ->set('name', trim($vocabulary
    ->label()));
  $status = $vocabulary
    ->save();
  $edit_link = $this->entity
    ->link($this
    ->t('Edit'));
  switch ($status) {
    case SAVED_NEW:
      drupal_set_message($this
        ->t('Created new vocabulary %name.', array(
        '%name' => $vocabulary
          ->label(),
      )));
      $this
        ->logger('taxonomy')
        ->notice('Created new vocabulary %name.', array(
        '%name' => $vocabulary
          ->label(),
        'link' => $edit_link,
      ));
      $form_state
        ->setRedirectUrl($vocabulary
        ->urlInfo('overview-form'));
      break;
    case SAVED_UPDATED:
      drupal_set_message($this
        ->t('Updated vocabulary %name.', array(
        '%name' => $vocabulary
          ->label(),
      )));
      $this
        ->logger('taxonomy')
        ->notice('Updated vocabulary %name.', array(
        '%name' => $vocabulary
          ->label(),
        'link' => $edit_link,
      ));
      $form_state
        ->setRedirectUrl($vocabulary
        ->urlInfo('collection'));
      break;
  }
  $form_state
    ->setValue('vid', $vocabulary
    ->id());
  $form_state
    ->set('vid', $vocabulary
    ->id());
}