You are here

function search_autocomplete_form_alter in Search Autocomplete 8

Same name and namespace in other branches
  1. 5 search_autocomplete.module \search_autocomplete_form_alter()
  2. 6 search_autocomplete.module \search_autocomplete_form_alter()
  3. 2.x search_autocomplete.module \search_autocomplete_form_alter()

Implements hook_form_alter().

Adds autocomplete_configurations to formAPI for search block and search page. This is an example on how to use configurations from code.

File

./search_autocomplete.module, line 260
Provides autocompletion in any field from GUI.

Code

function search_autocomplete_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Prevent a useless query to configurations.
  if ($form_id != $form_id && $form_id != 'search_form') {
    return;
  }

  // Load the default activated configurations.
  $autocomplete_configuration_entities = Drupal::entityQuery('autocompletion_configuration')
    ->condition('status', TRUE)
    ->condition('id', [
    'search_block',
    'search_form_users',
    'search_form_content',
  ], 'IN')
    ->execute();

  // Autocomplete the default search block.
  if ($form_id == 'search_block_form' && isset($autocomplete_configuration_entities['search_block'])) {
    $form['keys']['#autocomplete_configuration'] = 'search_block';
  }
  else {
    if ($form_id == 'search_form') {
      if (Drupal::routeMatch()
        ->getRouteName() == 'search.view_user_search' && isset($autocomplete_configuration_entities['search_form_users'])) {
        $form['basic']['keys']['#autocomplete_configuration'] = 'search_form_users';
      }
      else {
        if (isset($autocomplete_configuration_entities['search_form_users'])) {
          $form['basic']['keys']['#autocomplete_configuration'] = 'search_form_content';
        }
      }
    }
  }
  return $form;
}