You are here

function mpac_autocomplete in Multi-path autocomplete 7

Same name and namespace in other branches
  1. 8 mpac.pages.inc \mpac_autocomplete()
  2. 6 mpac.module \mpac_autocomplete()

Find nodes, shortcuts and URL aliases based on title.

Return value

List of elements with path as key.

1 string reference to 'mpac_autocomplete'
mpac_menu in ./mpac.module
Implement hook_menu().

File

./mpac.module, line 44
Find node paths on menu item creation via autocomplete.

Code

function mpac_autocomplete() {
  $args = arg();

  // Strip off the first 2 arguments ("mpac" and "autocomplete").
  $args = array_slice($args, 2);

  // Get the type of autocomplete we would like to display results for.
  $type = array_shift($args);

  // Validate type.
  if (!in_array($type, array(
    'alias',
    'menu',
    'shortcut',
  ))) {

    // The given type is not supported.
    return;
  }
  if (count($args) == 0) {

    // No more arguments so there is nothing to do for MPAC.
    return;
  }

  // Prepare the search query (if an argument contains a forward slash it has
  // been splitted into two arguments so join it once again).
  $query = implode('/', $args);
  $matches = array();
  $title = filter_xss_admin($query);

  // Get a list of all nodes where the title matches the given string.
  $matches = _mpac_get_matches_for_nodes($title);
  if ($type == 'menu' && module_exists('path')) {

    // Get a list of all URL aliases where the destination matches the given
    // string.
    $matches = array_merge($matches, _mpac_get_matches_for_aliases($title));
  }
  if ($type == 'shortcut' && module_exists('shortcut')) {

    // Get a list of menu items containing the given string in their title.
    $matches = array_merge($matches, _mpac_get_matches_for_shortcuts($title));
    if (module_exists('path')) {

      // Get matching url aliases.
      $matches = array_merge($matches, _mpac_get_matches_for_aliases($title));
    }
  }

  // Sort result list.
  natsort($matches);

  // Allow other modules to add paths.
  drupal_alter('mpac_autocomplete_paths', $matches, $title);

  // Reduce results to maximum number of items.
  $matches = array_slice($matches, 0, variable_get('mpac_max_items', 20));

  // Print results.
  drupal_json_output($matches);
}