function form_process_autocomplete

Adds autocomplete functionality to elements with a valid #autocomplete_path.

Suppose your autocomplete path in the menu system is 'mymodule_autocomplete.' In your form you have:


'#autocomplete_path' => 'mymodule_autocomplete/' . $some_key . '/' . $some_id,

The user types in "keywords" so the full path called is: 'mymodule_autocomplete/$some_key/$some_id?q=keywords'

Parameters

$element: The form element to process. Properties used:

  • #autocomplete_path: A system path to be used as callback URL by the autocomplete JavaScript library.

Related topics

1 string reference to 'form_process_autocomplete'
system_element_info in drupal/core/modules/system/system.module
Implements hook_element_info().

File

drupal/core/includes/form.inc, line 3906
Functions for form and batch generation and processing.

Code

function form_process_autocomplete($element, &$form_state) {
  if (!empty($element['#autocomplete_path']) && drupal_valid_path($element['#autocomplete_path'])) {
    $element['#attributes']['class'][] = 'form-autocomplete';
    $element['#attached']['library'][] = array(
      'system',
      'drupal.autocomplete',
    );

    // Provide a hidden element for the JavaScript behavior to bind to. Since
    // this element is for client-side functionality only, and we don't want to
    // collect any input from it, use #theme='hidden' instead of #type='hidden'.
    // @todo Refactor autocomplete.js to accept Drupal.settings instead of
    //   requiring extraneous markup.
    $element['autocomplete'] = array(
      '#theme' => 'hidden',
      '#attributes' => array(
        'id' => $element['#id'] . '-autocomplete',
        'value' => url($element['#autocomplete_path'], array(
          'absolute' => TRUE,
        )),
        'class' => array(
          'autocomplete',
        ),
        'disabled' => 'disabled',
      ),
    );
  }
  return $element;
}