You are here

function ctools_content_autocomplete_node in Chaos Tool Suite (ctools) 6

Helper function for autocompletion of node titles.

1 string reference to 'ctools_content_autocomplete_node'
ctools_content_menu in includes/content.menu.inc
@file Contains menu item registration for the content tool.

File

includes/content.menu.inc, line 25
Contains menu item registration for the content tool.

Code

function ctools_content_autocomplete_node($string) {
  if ($string != '') {
    $preg_matches = array();
    $match = preg_match('/\\[nid: (\\d+)\\]/', $string, $preg_matches);
    if (!$match) {
      $match = preg_match('/^nid: (\\d+)/', $string, $preg_matches);
    }
    if ($match) {
      $arg = $preg_matches[1];
      $where = "n.nid = %d";
    }
    else {
      $arg = $string;
      $where = "LOWER(n.title) LIKE LOWER('%%%s%%')";
    }
    if (!user_access('administer nodes')) {
      $where .= ' AND n.status = 1';
    }
    $result = db_query_range(db_rewrite_sql("SELECT n.nid, n.title, u.name FROM {node} n INNER JOIN {users} u ON u.uid = n.uid WHERE {$where}"), $arg, 0, 10);
    $matches = array();
    while ($node = db_fetch_object($result)) {
      $matches[$node->title . " [nid: {$node->nid}]"] = '<span class="autocomplete_title">' . check_plain($node->title) . '</span>';
    }
    drupal_json($matches);
  }
}