You are here

function supersized_context_autocomplete_nodes in Supersized 7

Same name and namespace in other branches
  1. 8 supersized_context/supersized_context.module \supersized_context_autocomplete_nodes()

Fetch a list of nodes available for autocomplete.

1 call to supersized_context_autocomplete_nodes()
_supersized_context_autocomplete in supersized_context/supersized_context.module
A helper function for autocomplete.

File

supersized_context/supersized_context.module, line 111
Supersized context module file.

Code

function supersized_context_autocomplete_nodes($string) {
  $matches = array();
  if (empty($string)) {
    return $matches;
  }
  $query = db_select('node', 'n')
    ->addTag('node_access')
    ->fields('n', array(
    'nid',
    'tnid',
    'title',
  ))
    ->range(0, 25);
  $where_args = array();
  global $user;
  if (!user_access('administer nodes', $user)) {
    $query
      ->condition(db_or()
      ->condition('n.status', 1)
      ->condition('n.uid', $user->uid));
  }

  // Run a match to see if they're specifying by nid.
  $preg_matches = array();
  $match = preg_match('/\\[nid: (\\d+)\\]/', $string, $preg_matches);
  if (!$match) {
    $match = preg_match('/^nid: (\\d+)/', $string, $preg_matches);
  }
  if ($match) {

    // If it found a nid via specification, reduce our resultset to just that
    // nid.
    $query
      ->condition('n.nid', $preg_matches[1]);
  }
  else {

    // Build the constant parts of the query.
    $query
      ->where('LOWER(n.title) LIKE LOWER(:string)', array(
      ':string' => '%' . db_like($string) . '%',
    ));
  }
  $query
    ->addTag('i18n_select');
  $result = $query
    ->execute();
  foreach ($result as $node) {
    $matches[$node->nid] = check_plain($node->title) . " [nid: {$node->nid}]";
  }
  return $matches;
}