You are here

public function AutocompletionConfigurationFormBase::save in Search Autocomplete 8

Same name and namespace in other branches
  1. 2.x src/Form/AutocompletionConfigurationFormBase.php \Drupal\search_autocomplete\Form\AutocompletionConfigurationFormBase::save()

Overrides Drupal\Core\Entity\EntityFormController::save().

Saves the entity. This is called after submit() has built the entity from the form values. Do not override submit() as save() is the preferred method for entity form controllers.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: An associative array containing the current state of the form.

Overrides EntityForm::save

1 call to AutocompletionConfigurationFormBase::save()
AutocompletionConfigurationAddForm::save in src/Form/AutocompletionConfigurationAddForm.php
Overrides Drupal\Core\Entity\EntityFormController::save().
1 method overrides AutocompletionConfigurationFormBase::save()
AutocompletionConfigurationAddForm::save in src/Form/AutocompletionConfigurationAddForm.php
Overrides Drupal\Core\Entity\EntityFormController::save().

File

src/Form/AutocompletionConfigurationFormBase.php, line 168

Class

AutocompletionConfigurationFormBase
Class AutocompletionConfigurationFormBase.

Namespace

Drupal\search_autocomplete\Form

Code

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

  // EntityForm provides us with the entity we're working on.
  $autocompletion_configuration = $this->entity;

  // Drupal already populated the form values in the entity object. Each
  // form field was saved as a public variable in the entity class. PHP
  // allows Drupal to do this even if the method is not defined ahead of
  // time.
  $status = $autocompletion_configuration
    ->save();

  // Grab the URL of the new entity. We'll use it in the message.
  $url = $autocompletion_configuration
    ->toUrl();
  if ($status == SAVED_UPDATED) {

    // If we edited an existing entity...
    drupal_set_message($this
      ->t('Autocompletion Configuration %label has been updated.', [
      '%label' => $autocompletion_configuration
        ->label(),
    ]));
    $this
      ->logger('search_autocomplete')
      ->notice('Autocompletion Configuration %label has been updated.', [
      '%label' => $autocompletion_configuration
        ->label(),
    ]);
  }
  else {

    // If we created a new entity...
    drupal_set_message($this
      ->t('Autocompletion Configuration %label has been added.', [
      '%label' => $autocompletion_configuration
        ->label(),
    ]));
    $this
      ->logger('search_autocomplete')
      ->notice('Autocompletion Configuration %label has been added.', [
      '%label' => $autocompletion_configuration
        ->label(),
    ]);
  }

  // Redirect the user back to the listing route after the save operation.
  $form_state
    ->setRedirect('autocompletion_configuration.list');
}