You are here

function mpac_autocomplete in Multi-path autocomplete 6

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

Find nodes and URL aliases based on title.

Parameters

$string: Title of element to find.

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 35
Find node paths on menu item creation via autocomplete.

Code

function mpac_autocomplete($string) {
  $matches = array();
  $title = filter_xss_admin($string);

  // Get a list of all nodes where the title matches the given string.
  $query = db_query("SELECT * FROM {node} n WHERE n.title LIKE '%%%s%%'", $title);
  while ($node = db_fetch_object($query)) {

    // Add node path and title to list.
    if (node_access('view', $node) && $node->status) {
      $matches['node/' . $node->nid] = $node->title;
    }
  }
  if (module_exists('path')) {

    // Get a list of all URL aliases where the destination matches the given
    // string.
    $query = db_query("SELECT * FROM {url_alias} WHERE dst LIKE '%%%s%%'", $title);
    while ($alias = db_fetch_object($query)) {

      // Add alias source and destination to list (and add a special marker to
      // the item).
      $matches[$alias->src] = $alias->dst . t('*');
    }
  }
  drupal_json($matches);
}