You are here

function notifications_tags_autocomplete in Notifications 7

Same name and namespace in other branches
  1. 6.4 notifications_tags/notifications_tags.module \notifications_tags_autocomplete()
  2. 6 notifications_tags/notifications_tags.module \notifications_tags_autocomplete()
  3. 6.2 notifications_tags/notifications_tags.module \notifications_tags_autocomplete()
  4. 6.3 notifications_tags/notifications_tags.module \notifications_tags_autocomplete()

Helper function for autocompletion

It is similar to taxonomy_autocomplete but:

  • Just searches terms in allowed vocabularies
  • Has single/multiple switch in the path

Parameters

$type: 'simple' or 'multiple'

1 string reference to 'notifications_tags_autocomplete'
notifications_tags_menu in notifications_tags/notifications_tags.module
Implementation of hook_menu_()

File

notifications_tags/notifications_tags.module, line 371
Subscriptions to taxonomy terms

Code

function notifications_tags_autocomplete($type, $tags_typed = '') {

  // The user enters a comma-separated list of tags. We only autocomplete the last tag.
  $tags_typed = drupal_explode_tags($tags_typed);
  $tag_last = drupal_strtolower(array_pop($tags_typed));
  $term_matches = array();
  if ($tag_last != '' && ($vids = notifications_tags_vocabulary_enabled())) {
    $query = db_select('taxonomy_term_data', 't');
    $query
      ->addTag('translatable');
    $query
      ->addTag('term_access');

    // Do not select already entered terms.
    if (!empty($tags_typed)) {
      $query
        ->condition('t.name', $tags_typed, 'NOT IN');
    }

    // Select rows that match by term name.
    $tags_return = $query
      ->fields('t', array(
      'tid',
      'name',
    ))
      ->condition('t.vid', $vids)
      ->condition('t.name', '%' . db_like($tag_last) . '%', 'LIKE')
      ->range(0, 10)
      ->execute()
      ->fetchAllKeyed();
    $prefix = count($tags_typed) ? implode(', ', $tags_typed) . ', ' : '';
    $term_matches = array();
    foreach ($tags_return as $tid => $name) {
      $n = $name;

      // Term names containing commas or quotes must be wrapped in quotes.
      if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
        $n = '"' . str_replace('"', '""', $name) . '"';
      }
      else {
        $term_matches[$prefix . $n] = check_plain($name);
      }
    }
  }
  drupal_json_output($term_matches);
}