1 node.pages.inc node_autocomplete($string = '')

Autocomplete callback for nodes by title.

Searches for a node by title, but then identifies it by nid, so the actual returned value can be used later by the form.

The returned $matches array has

  • key: The title, with the identifying nid in brackets, like "Some node title [3325]"
  • value: the title which will is displayed in the autocomplete pulldown.

Note that we must use a key style that can be parsed successfully and unambiguously. For example, if we might have node titles that could have [3325] in them, then we'd have to use a more restrictive token.

Parameters

string $string: The string that will be searched.

File

core/modules/node/node.pages.inc, line 1020
Callbacks for adding, editing, and deleting content and managing revisions.

Code

function node_autocomplete($string = '') {
  $matches = array();
  if ($string) {
    if (is_numeric($string)) {
      $result = db_select('node')
        ->fields('node', array('nid', 'title'))
        ->condition('nid', $string)
        ->addTag('node_access')
        ->execute();
      foreach ($result as $node) {
        $matches[$node->title . " [$node->nid]"] = check_plain($node->title) . ' [' . $node->nid . ']';
      }
    }
    $result = db_select('node')
      ->fields('node', array('nid', 'title'))
      ->condition('title', db_like($string) . '%', 'LIKE')
      ->addTag('node_access')
      ->range(0, 10)
      ->execute();
    foreach ($result as $node) {
      $matches[$node->title . " [$node->nid]"] = check_plain($node->title) . ' [' . $node->nid . ']';
    }
  }

  backdrop_json_output($matches);
}