You are here

public function ServerForm::save in Search API 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

src/Form/ServerForm.php, line 318

Class

ServerForm
Provides a form for creating and editing search servers.

Namespace

Drupal\search_api\Form

Code

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

  // Only save the server if the form doesn't need to be rebuilt.
  if (!$form_state
    ->isRebuilding()) {
    try {
      $server = $this
        ->getEntity();
      $server
        ->save();
      $this->messenger
        ->addStatus($this
        ->t('The server was successfully saved.'));
      $form_state
        ->setRedirect('entity.search_api_server.canonical', [
        'search_api_server' => $server
          ->id(),
      ]);
    } catch (EntityStorageException $e) {
      $form_state
        ->setRebuild();
      $message = '%type: @message in %function (line %line of %file).';
      $variables = Error::decodeException($e);
      $this
        ->getLogger('search_api')
        ->error($message, $variables);
      $this->messenger
        ->addError($this
        ->t('The server could not be saved.'));
    }
  }
}