You are here

function menu_position_taxonomy_autocomplete in Menu Position 7

Same name and namespace in other branches
  1. 7.2 plugins/menu_position.taxonomy.inc \menu_position_taxonomy_autocomplete()

Helper function for auto-completion.

1 string reference to 'menu_position_taxonomy_autocomplete'
menu_position_menu in ./menu_position.module
Implements hook_menu().

File

plugins/menu_position.taxonomy.inc, line 229
Provides the "Taxonomy" rule plugin for the Menu Position module.

Code

function menu_position_taxonomy_autocomplete($vocabulary, $tags_typed = '') {

  // Don't query for terms if no vocabulary is selected.
  if ($vocabulary == 0) {
    drupal_json_output(array());
    return;
  }

  // The user enters a comma-separated list of tags. We only auto-complete the
  // last tag.
  $tags_typed = drupal_explode_tags($tags_typed);
  $tag_last = drupal_strtolower(array_pop($tags_typed));
  $term_matches = array();
  if ($tag_last != '') {
    $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', array(
      $vocabulary,
    ))
      ->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) . '"';
      }
      $term_matches[$prefix . $n] = check_plain($name);
    }
  }
  drupal_json_output($term_matches);
}