You are here

function ajax_example_node_by_author_node_autocomplete_callback in Examples for Developers 7

Autocomplete callback for nodes by title but limited by author.

Searches for a node by title given the passed-in author username.

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

int $author_uid: The author username to limit the search.

string $string: The string that will be searched.

1 string reference to 'ajax_example_node_by_author_node_autocomplete_callback'
ajax_example_menu in ajax_example/ajax_example.module
Implements hook_menu().

File

ajax_example/ajax_example_autocomplete.inc, line 443
ajax_example_autocomplete.inc

Code

function ajax_example_node_by_author_node_autocomplete_callback($author_uid, $string = "") {
  $matches = array();
  if ($author_uid > 0 && trim($string)) {
    $result = db_select('node')
      ->fields('node', array(
      'nid',
      'title',
    ))
      ->condition('uid', $author_uid)
      ->condition('title', db_like($string) . '%', 'LIKE')
      ->range(0, 10)
      ->execute();
    foreach ($result as $node) {
      $matches[$node->title . " [{$node->nid}]"] = check_plain($node->title);
    }
  }
  drupal_json_output($matches);
}