You are here

function webform_views_autocomplete in Webform 7.4

Menu callback; Provide a list of Webform nodes for use in autocomplete.

1 string reference to 'webform_views_autocomplete'
webform_menu in ./webform.module
Implements hook_menu().

File

views/webform.views.inc, line 633
Views hooks implemented for the Webform module.

Code

function webform_views_autocomplete($string = '') {
  if ($string) {
    $or = db_or();

    // Strings with nid: in them can be used as direct matches.
    $matches = array();
    if (preg_match('/nid:([0-9]+)/', $string, $matches)) {
      $or
        ->condition('n.nid', (int) $matches[1]);
    }
    else {
      $or
        ->condition('n.title', '%' . db_like($string) . '%', 'LIKE');
      if (is_numeric($string)) {
        $or
          ->condition('n.nid', (int) $string);
      }
    }
    $options = array();
    $query = db_select('node', 'n')
      ->fields('n', array(
      'nid',
      'title',
    ))
      ->condition($or);
    $query
      ->innerJoin('webform', 'w', 'w.nid = n.nid');
    $result = $query
      ->range(0, 10)
      ->execute();
    foreach ($result as $node) {
      $options[$node->title . ' [nid:' . $node->nid . ']'] = check_plain($node->title) . ' [nid:' . $node->nid . ']';
    }
  }
  drupal_json_output($options);
}