You are here

public function SearchEditForm::submitForm in Search API Autocomplete 8

This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state can be updated, this way the subsequently invoked handlers can retrieve a regular entity object to act on. Generally this method should not be overridden unless the entity requires the same preparation for two actions, see \Drupal\comment\CommentForm for an example with the save and preview actions.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides EntityForm::submitForm

File

src/Form/SearchEditForm.php, line 330

Class

SearchEditForm
Provides an edit form for autocomplete search entities.

Namespace

Drupal\search_api_autocomplete\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Remove button and internal Form API values from submitted values.
  $form_state
    ->cleanValues();
  $this->entity = $this
    ->buildEntity($form, $form_state);
  $search = $this->entity;
  $values = $form_state
    ->getValues();

  // Iterate over all suggesters that are enabled.
  $available_suggesters = $this
    ->getAvailableSuggesters();

  /** @var \Drupal\search_api_autocomplete\Suggester\SuggesterInterface[] $enabled_suggesters */
  $enabled_suggesters = array_intersect_key($available_suggesters, array_filter($values['suggesters']['enabled']));
  $suggester_weights = [];
  $suggester_limits = [];
  foreach ($enabled_suggesters as $suggester_id => $suggester) {

    // Submit the form, if there is one.
    if ($suggester instanceof PluginFormInterface) {
      $suggester_form_state = SubformState::createForSubform($form['suggesters']['settings'][$suggester_id], $form, $form_state);
      $suggester
        ->submitConfigurationForm($form['suggesters']['settings'][$suggester_id], $suggester_form_state);
    }
    $suggester_weights[$suggester_id] = (int) $values['suggesters']['weights'][$suggester_id]['weight'];
    if (is_numeric($values['suggesters']['weights'][$suggester_id]['limit'])) {
      $suggester_limits[$suggester_id] = (int) $values['suggesters']['weights'][$suggester_id]['limit'];
    }
  }
  $search
    ->setSuggesters($enabled_suggesters);
  $search
    ->set('suggester_weights', $suggester_weights);
  $search
    ->set('suggester_limits', $suggester_limits);
  $search_plugin = $this->entity
    ->getSearchPlugin();
  if ($search_plugin instanceof PluginFormInterface) {
    $plugin_form = empty($form['search_settings']) ? [] : $form['search_settings'];
    $plugin_form_state = SubFormState::createForSubform($form['search_settings'], $form, $form_state);
    $search_plugin
      ->submitConfigurationForm($plugin_form, $plugin_form_state);
    $search
      ->set('search_settings', $search_plugin
      ->getConfiguration());
  }
  $form_state
    ->setRedirect('search_api_autocomplete.admin_overview', [
    'search_api_index' => $search
      ->getIndexId(),
  ]);
  $this->messenger
    ->addStatus($this
    ->t('The autocompletion settings for the search have been saved.'));
}