You are here

function apachesolr_autocomplete_do_alter in Apache Solr Autocomplete 7

Same name and namespace in other branches
  1. 6 apachesolr_autocomplete.module \apachesolr_autocomplete_do_alter()
  2. 7.2 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

1 call to apachesolr_autocomplete_do_alter()
apachesolr_autocomplete_form_alter in ./apachesolr_autocomplete.module
Implementation of hook_form_alter().

File

./apachesolr_autocomplete.module, line 71
Alters search forms to suggest terms using Apache Solr using AJAX. Thanks to: robertDouglass who contributed some of the code. sch4lly for contributing to D7 version

Code

function apachesolr_autocomplete_do_alter(&$element, $form, $context_id) {

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

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

  // Custom & jQuery UI widgets
  if (apachesolr_autocomplete_variable_get_widget() != 'drupal') {

    // 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');

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

    // Build a settings for this $autocomplete_element_id
    $settings = array(
      'apachesolr_autocomplete' => array(
        'forms' => array(
          $autocomplete_element_id => array(
            'id' => $autocomplete_element_id,
            // helps identify the element on jQuery
            'path' => url($autocomplete_path),
          ),
        ),
      ),
    );
    drupal_add_js($settings, 'setting');
  }
  else {

    // Drupal stock autocomplete handler.
    $element['#autocomplete_path'] = $autocomplete_path;
  }
}