You are here

function apachesolr_autocomplete_do_alter in Apache Solr Autocomplete 7.2

Same name and namespace in other branches
  1. 6 apachesolr_autocomplete.module \apachesolr_autocomplete_do_alter()
  2. 7 apachesolr_autocomplete.module \apachesolr_autocomplete_do_alter()

Helper function to do the actual altering of search forms.

Parameters

$element: The element to alter. Should be passed by reference so that original form element will be altered. E.g.: apachesolr_autocomplete_do_alter(&$form['xyz'])

$form: The form being altered.

$context_id: A string identifying the form. For example: apachesolr_search_page:core_search

3 calls to apachesolr_autocomplete_do_alter()
apachesolr_autocomplete_form_apachesolr_search_custom_page_search_form_alter in ./apachesolr_autocomplete.module
Implementation of hook_form_FORM_ID_alter().
apachesolr_autocomplete_form_search_block_form_alter in ./apachesolr_autocomplete.module
Implementation of hook_form_FORM_ID_alter().
apachesolr_autocomplete_form_search_form_alter in ./apachesolr_autocomplete.module
Implementation of hook_form_FORM_ID_alter().

File

./apachesolr_autocomplete.module, line 62
Alters search forms to suggest terms using Apache Solr using AJAX.

Code

function apachesolr_autocomplete_do_alter(&$element, $form, $context_id) {
  drupal_set_message("Altering form for context {$context_id}...");

  // TODO: DEBUG
  // The unique element ID for this form's keyword search element.
  $autocomplete_element_id = $form['#id'];

  // Create elements if they do not exist.
  if (!isset($element['#attributes'])) {
    $element['#attributes'] = array();
  }
  if (!isset($element['#attributes']['class'])) {
    $element['#attributes']['class'] = array();
  }
  array_push($element['#attributes']['class'], 'apachesolr-autocomplete');

  // Specify path to autocomplete handler.
  $autocomplete_path = 'apachesolr_autocomplete_callback/' . $context_id;

  // Add data-apachesolr-autocomplete attribute to element.
  $element['#attributes']['data-apachesolr-autocomplete-id'] = array(
    $autocomplete_element_id,
  );

  // Build a settings array.
  $settings = array(
    'id' => $autocomplete_element_id,
    // helps identify the element on jQuery
    'path' => url($autocomplete_path),
  );

  // Add our JS settings to the current page.
  drupal_add_js(array(
    'apachesolr_autocomplete' => array(
      $autocomplete_element_id => $settings,
    ),
  ), 'setting');
}