You are here

function search_api_autocomplete_admin_search_edit_submit in Search API Autocomplete 7

Submit callback for search_api_autocomplete_admin_search_edit().

See also

search_api_autocomplete_admin_search_edit()

search_api_autocomplete_admin_search_edit_validate()

File

./search_api_autocomplete.admin.inc, line 413
Contains page callbacks and related functions for the admin UI.

Code

function search_api_autocomplete_admin_search_edit_submit(array $form, array &$form_state) {
  $values =& $form_state['values'];
  if (!empty($form_state['type']['config form'])) {
    $f = $form_state['type']['config form'] . '_submit';
    if (function_exists($f)) {
      $custom_form = empty($form['options']['custom']) ? array() : $form['options']['custom'];
      $f($custom_form, $form_state, $values['options']['custom']);
    }
  }
  $search = $form_state['search'];
  $search->enabled = $values['enabled'];
  $search->suggester_id = $values['suggester_id'];
  $form_state['redirect'] = 'admin/config/search/search_api/index/' . $search->index_id . '/autocomplete';

  // Take care of custom options that aren't changed in the config form.
  if (!empty($search->options['custom'])) {
    if (!isset($values['options']['custom'])) {
      $values['options']['custom'] = array();
    }
    $values['options']['custom'] += $search->options['custom'];
  }

  // Allow the suggester to decide how to save its configuration. If the user
  // has disabled JS in the browser, or AJAX didn't work for some other reason,
  // a different suggester might be selected than that which created the config
  // form. In that case, we don't call the form submit method, save empty
  // configuration for the plugin and stay on the page.
  if ($values['suggester_id'] == $values['old_suggester_id']) {
    $configuration = array();
    if (!empty($values['options']['suggester_configuration'])) {
      $configuration = $values['options']['suggester_configuration'];
    }
    $suggester = search_api_autocomplete_suggester_load($values['suggester_id'], $search, $configuration);
    $suggester_form = $form['options']['suggester_configuration'];
    unset($suggester_form['old_suggester_id']);
    $suggester_form_state =& search_api_autocomplete_get_plugin_form_state($form_state);
    $suggester
      ->submitConfigurationForm($suggester_form, $suggester_form_state);
    $values['options']['suggester_configuration'] = $suggester
      ->getConfiguration();
  }
  else {
    $values['options']['suggester_configuration'] = array();
    $form_state['redirect'] = NULL;
    drupal_set_message(t('The used suggester plugin has changed. Please review the configuration for the new plugin.'), 'warning');
  }
  $search->options = $values['options'];
  $search
    ->save();
  drupal_set_message(t('The autocompletion settings for the search have been saved.'));
}