You are here

function js_process_autocomplete in JS Callback Handler 7.2

Autocomplete #process callback.

1 string reference to 'js_process_autocomplete'
js_element_info_alter in ./js.module
Implements hook_element_info_alter().

File

./js.module, line 352
JavaScript callback handler module.

Code

function js_process_autocomplete($element) {
  if ($element['#autocomplete_path'] && !empty($element['#autocomplete_input']['#url_value']) && isset($element['#js_callback']) && is_array($element['#js_callback'])) {
    $module = key($element['#js_callback']);
    $callback = reset($element['#js_callback']);
    $info = js_get_callback($module, $callback);
    $options = array(
      'absolute' => TRUE,
      'script' => 'js.php',
      'query' => array(
        'js_module' => $module,
        'js_callback' => $callback,
        // Define the path here so it's appended after our query parameters.
        // This is necessary because of how core autocomplete.js appends the
        // test being searched to the end of the URL.
        'q' => $element['#autocomplete_path'],
      ),
    );

    // Generate a token if necessary.
    if ($info['token']) {
      $options['query']['js_token'] = js_get_token($module, $callback);
    }

    // Force autocomplete to use non-clean URLs since this protects against the
    // browser interpreting the path plus search string as an actual file.
    $current_clean_url = isset($GLOBALS['conf']['clean_url']) ? $GLOBALS['conf']['clean_url'] : NULL;
    $GLOBALS['conf']['clean_url'] = 0;

    // Force the script path to 'js.php', in case the server is not
    // configured to find it automatically. Normally it is the responsibility
    // of the site to do this themselves using hook_url_outbound_alter() (see
    // url()) but since this code is forcing non-clean URLs on sites that don't
    // normally use them, it is done here instead.
    // @see https://www.drupal.org/project/js/issues/2873484
    $base_url = url('<front>', $options);

    // Because url() enforces it's own q parameter if a path/path-prefix is
    // given, which happens if language prefixes are enabled, we need to
    // enforce our q parameter.
    $drupal_parsed_url = drupal_parse_url($base_url);

    // Run the autocomplete path through url to ensure it get's the same
    // prefixes. We use drupal_parse_url() as it nicely extracts q right away.
    $autocomplete_path = drupal_parse_url(url($element['#autocomplete_path']));

    // Add the extracted path as the q parameter for the autocomplete url.
    $drupal_parsed_url['query']['q'] = $autocomplete_path['path'];

    // Now create the fully cleaned url.
    $parsed = parse_url($base_url);

    // Inject our custom query.
    $parsed['query'] = drupal_http_build_query($drupal_parsed_url['query']);

    // Now build the uri from the parsed data.
    $element['#autocomplete_input']['#url_value'] = _js_http_build_url($parsed);
    $GLOBALS['conf']['clean_url'] = $current_clean_url;
  }
  return $element;
}