You are here

function search_api_autocomplete_admin_search_edit in Search API Autocomplete 7

Form for editing the autocompletion settings for a search.

Parameters

SearchApiAutocompleteSearch $search: The search whose settings should be edited.

See also

search_api_autocomplete_admin_search_edit_validate()

search_api_autocomplete_admin_search_edit_submit()

1 string reference to 'search_api_autocomplete_admin_search_edit'
search_api_autocomplete_menu in ./search_api_autocomplete.module
Implements hook_menu().

File

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

Code

function search_api_autocomplete_admin_search_edit(array $form, array &$form_state, SearchApiAutocompleteSearch $search) {
  drupal_set_title(t('Configure autocompletion for %search', array(
    '%search' => $search->name,
  )), PASS_THROUGH);

  // If this is a re-build (i.e., most likely an AJAX call due to a new
  // suggester being selected), prepare the suggester sub-form state
  // accordingly.
  $selected_suggester_id = $search
    ->getSuggesterId();
  if (!empty($form_state['values']['suggester_id'])) {
    $selected_suggester_id = $form_state['values']['suggester_id'];

    // Don't let submitted values for a different suggester influence another
    // suggester's form.
    if ($selected_suggester_id != $form_state['values']['old_suggester_id']) {
      unset($form_state['values']['options']['suggester_configuration']);
      unset($form_state['input']['options']['suggester_configuration']);
    }
  }
  $form_state['search'] = $search;
  $form_state['type'] = $type = search_api_autocomplete_get_types($search->type);
  if (!$type) {
    drupal_set_message(t('No information about the type of this search was found.'), 'error');
    return array();
  }
  $form['#tree'] = TRUE;
  $form['enabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enabled'),
    '#default_value' => $search->enabled,
  );
  $form['suggester_id'] = array(
    '#type' => 'radios',
    '#title' => t('Suggester plugin'),
    '#description' => t('Choose the suggester implementation to use.'),
    '#options' => array(),
    '#required' => TRUE,
    '#default_value' => $selected_suggester_id,
    '#ajax' => array(
      'callback' => 'search_api_autocomplete_suggester_ajax_callback',
      'wrapper' => 'search-api-suggester-options',
    ),
  );
  foreach (search_api_autocomplete_suggesters_for_index($search
    ->index()) as $suggester_id => $definition) {

    // Load the suggester plugin. If the suggester is unchanged from the one on
    // the saved version of the search, use the saved configuration.
    $configuration = array();
    if ($suggester_id == $search
      ->getSuggesterId() && isset($search->options['suggester_configuration'])) {
      $configuration = $search->options['suggester_configuration'];
    }
    $suggester = search_api_autocomplete_suggester_load($suggester_id, $search, $configuration);
    if (!$suggester) {
      continue;
    }

    // Add the suggester to the suggester options.
    $form['suggester_id']['#options'][$suggester_id] = check_plain($suggester
      ->label());

    // Then, also add the configuration form for the selected suggester.
    if ($suggester_id != $selected_suggester_id) {
      continue;
    }
    $suggester_form_state =& search_api_autocomplete_get_plugin_form_state($form_state);
    $suggester_form = $suggester
      ->buildConfigurationForm(array(), $suggester_form_state);
    if ($suggester_form) {
      $form['options']['suggester_configuration'] = $suggester_form;
      $form['options']['suggester_configuration']['#type'] = 'fieldset';
      $form['options']['suggester_configuration']['#title'] = t('Configure the %suggester suggester plugin', array(
        '%suggester' => $suggester
          ->label(),
      ));
      $form['options']['suggester_configuration']['#description'] = $suggester
        ->getDescription();
      $form['options']['suggester_configuration']['#collapsible'] = TRUE;
    }
    else {
      $form['options']['suggester_configuration']['#type'] = 'item';
    }
    $form['options']['suggester_configuration']['#prefix'] = '<div id="search-api-suggester-options">';
    $form['options']['suggester_configuration']['#suffix'] = '</div>';
  }
  $form['options']['suggester_configuration']['old_suggester_id'] = array(
    '#type' => 'hidden',
    '#value' => $selected_suggester_id,
    '#tree' => FALSE,
  );

  // If there is only a single plugin available, hide the "suggester" option.
  if (count($form['suggester_id']['#options']) == 1) {
    $form['suggester_id'] = array(
      '#type' => 'value',
      '#value' => key($form['suggester_id']['#options']),
    );
  }
  $form['options']['min_length'] = array(
    '#type' => 'textfield',
    '#title' => t('Minimum length of keywords for autocompletion'),
    '#description' => t('If the entered keywords are shorter than this, no autocomplete suggestions will be displayed.'),
    '#default_value' => isset($search->options['min_length']) ? $search->options['min_length'] : 1,
    '#validate' => array(
      'element_validate_integer_positive',
    ),
  );
  $form['options']['results'] = array(
    '#type' => 'checkbox',
    '#title' => t('Display result count estimates'),
    '#description' => t('Display the estimated number of result for each suggestion. ' . 'This option might not have an effect for some servers or types of suggestion.'),
    '#default_value' => !empty($search->options['results']),
  );
  $custom_form = empty($form['options']['custom']) ? array() : $form['options']['custom'];
  if (!empty($type['config form']) && function_exists($type['config form']) && is_array($custom_form = $type['config form']($custom_form, $form_state, $search))) {
    $form['options']['custom'] = $custom_form;
  }
  $form['advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['advanced']['submit_button_selector'] = array(
    '#type' => 'textfield',
    '#title' => t('Search button selector'),
    '#description' => t('<a href="@jquery_url">jQuery selector</a> to identify the search button in the search form. Use the ID attribute of the "Search" submit button to prevent issues when another button is present (e.g., "Reset"). The selector is evaluated relative to the form. The default value is ":submit".', array(
      '@jquery_url' => 'https://api.jquery.com/category/selectors/',
    )),
    '#default_value' => isset($search->options['submit_button_selector']) ? $search->options['submit_button_selector'] : ':submit',
    '#required' => TRUE,
    '#parents' => array(
      'options',
      'submit_button_selector',
    ),
  );
  $form['advanced']['autosubmit'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable auto-submit'),
    '#description' => t('When enabled, the search form will automatically be submitted when a selection is made by pressing "Enter".'),
    '#default_value' => isset($search->options['autosubmit']) ? $search->options['autosubmit'] : TRUE,
    '#parents' => array(
      'options',
      'autosubmit',
    ),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return $form;
}