You are here

public function SearchApiPageForm::save in Search API Pages 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/SearchApiPageForm.php, line 298

Class

SearchApiPageForm
Class SearchApiPageForm.

Namespace

Drupal\search_api_page\Form

Code

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

  /* @var $search_api_page \Drupal\search_api_page\SearchApiPageInterface */
  $search_api_page = $this->entity;

  // Reset view mode configuration.
  if (!$search_api_page
    ->renderAsViewModes()) {
    $search_api_page
      ->set('view_mode_configuration', []);
  }

  // Check searched fields. In case nothing has been selected, select all
  // the available fields.
  $has_selection = FALSE;
  $searched_fields = $form_state
    ->getValue('searched_fields');
  foreach ($searched_fields as $key => $value) {
    if ($key === $value) {
      $has_selection = TRUE;
      break;
    }
  }
  if (!$has_selection) {
    $key_values = array_keys($form['index_fieldset']['searched_fields']['#options']);
    $searched_fields = array_combine($key_values, $key_values);
    $search_api_page
      ->set('searched_fields', $searched_fields);
  }
  $status = $search_api_page
    ->save();
  switch ($status) {
    case SAVED_NEW:

      // Redirect to edit form so the rest can be configured.
      $form_state
        ->setRedirectUrl($search_api_page
        ->toUrl('edit-form'));
      break;
    default:
      $indexHasChanged = $form_state
        ->getValue('index') !== $form_state
        ->getValue('previous_index');
      if (!$indexHasChanged) {

        // Index is unchanged so we'll redirect to the overview.
        $form_state
          ->setRedirectUrl($search_api_page
          ->toUrl('collection'));
        $this
          ->messenger()
          ->addMessage($this
          ->t('Saved the %label Search page.', [
          '%label' => $search_api_page
            ->label(),
        ]));
      }
      else {

        // Index has changed so we'll redirect to the edit form.
        $form_state
          ->setRedirectUrl($search_api_page
          ->toUrl('edit-form'));
        $this
          ->messenger()
          ->addMessage($this
          ->t('Updated the index for the %label Search page.', [
          '%label' => $search_api_page
            ->label(),
        ]));
      }
  }
  $pathHasChanged = $form_state
    ->getValue('path') != $form_state
    ->getValue('previous_path');
  $cleanUrlHasChanged = $form_state
    ->getValue('clean_url') != $form_state
    ->getValue('previous_clean_url');
  if ($pathHasChanged || $cleanUrlHasChanged) {
    \Drupal::service('router.builder')
      ->rebuild();
  }

  /** @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface $invalidator */
  $invalidator = \Drupal::service('cache_tags.invalidator');
  $invalidator
    ->invalidateTags([
    'search_api_page.style',
  ]);
}