You are here

function fontyourface_autocomplete in @font-your-face 7

Returns JSON-encoded autocomplete matches.

1 string reference to 'fontyourface_autocomplete'
fontyourface_menu in ./fontyourface.module
Implements hook_menu().

File

./fontyourface.module, line 767

Code

function fontyourface_autocomplete($tags_typed = '') {
  $tags_typed = drupal_explode_tags($tags_typed);
  $tag_last = drupal_strtolower(array_pop($tags_typed));
  $matches = array();
  if ($tag_last != '') {
    $query = db_select('fontyourface_tag', 't');

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

    // if
    // Select rows that match by tag name.
    $tags_return = $query
      ->fields('t', array(
      'tid',
      'name',
    ))
      ->condition('t.name', '%' . db_like($tag_last) . '%', 'LIKE')
      ->range(0, 10)
      ->execute()
      ->fetchAllKeyed();
    $prefix = count($tags_typed) ? implode(', ', $tags_typed) . ', ' : '';
    $matches = array();
    foreach ($tags_return as $tid => $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) . '"';
      }
      else {
        $matches[$prefix . $n] = check_plain($name);
      }

      // else
    }

    // foreach
  }

  // if
  drupal_json_output($matches);
}