You are here

function flexiform_autocomplete_tags in Flexiform 7

Auto-complete callback for flexiform tags.

1 string reference to 'flexiform_autocomplete_tags'
FlexiformUIController::hook_menu in ./flexiform.admin.inc
Overrides hook_menu() defaults.

File

./flexiform.admin.inc, line 2041
Model type editing UI.

Code

function flexiform_autocomplete_tags($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));
  $tag_matches = array();
  if ($tag_last != '') {
    $query = db_select('flexiform_tags', 'ft');

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

    // Select rows that match by tag name.
    $tags_return = $query
      ->distinct()
      ->fields('ft', array(
      'tag',
    ))
      ->condition('ft.tag', '%' . db_like($tag_last) . '%', 'LIKE')
      ->groupBy('ft.tag')
      ->range(0, 10)
      ->execute()
      ->fetchCol('ft.tag');
    $prefix = count($tags_typed) ? drupal_implode_tags($tags_typed) . ', ' : '';
    foreach ($tags_return as $name) {
      $n = $name;

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