You are here

public function IndexForm::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/IndexForm.php, line 611

Class

IndexForm
Provides a form for the Index entity.

Namespace

Drupal\search_api\Form

Code

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

  // @todo Redirect to a confirm form if changing server or tracker, since
  //   that isn't such a light operation (equaling a "clear", basically).
  // Only save the index if the form doesn't need to be rebuilt.
  if (!$form_state
    ->isRebuilding()) {
    try {

      /** @var \Drupal\search_api\IndexInterface $index */
      $index = $this
        ->getEntity();
      $index
        ->save();
      $this->messenger
        ->addStatus($this
        ->t('The index was successfully saved.'));
      $button = $form_state
        ->getTriggeringElement();
      if (!empty($button['#redirect_to_url'])) {
        $form_state
          ->setRedirectUrl($index
          ->toUrl($button['#redirect_to_url']));
      }
      else {
        $form_state
          ->setRedirect('entity.search_api_index.canonical', [
          'search_api_index' => $index
            ->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 index could not be saved.'));
    }
  }
}