function nodequeue_reference_autocomplete in Nodequeue reference 7
Menu callback for the autocomplete results.
3 string references to 'nodequeue_reference_autocomplete'
- nodequeue_reference_field_widget_form in ./
nodequeue_reference.module - Implements hook_field_widget_form().
- nodequeue_reference_field_widget_settings_form in ./
nodequeue_reference.module - Implements hook_field_widget_settings_form().
- nodequeue_reference_menu in ./
nodequeue_reference.module - Implements hook_menu().
File
- ./
nodequeue_reference.module, line 342 - Form field type for node queues.
Code
function nodequeue_reference_autocomplete($entity_type, $bundle, $field_name, $string = '') {
$field = field_info_field($field_name);
$instance = field_info_instance($entity_type, $field_name, $bundle);
$options = array(
'string' => $string,
'match' => $instance['widget']['settings']['autocomplete_match'],
'limit' => 10,
'ids' => array(),
);
$results =& drupal_static(__FUNCTION__, array());
// Create unique id for static cache.
$cid = $field['field_name'] . ':' . $options['match'] . ':' . ($options['string'] !== '' ? $options['string'] : implode('-', $options['ids'])) . ':' . $options['limit'];
if (!isset($results[$cid])) {
$references = FALSE;
$query = db_select('nodequeue_queue', 'q');
$queue_qid_alias = $query
->addField('q', 'qid');
$queue_title_alias = $query
->addField('q', 'title');
if ($options['string'] !== '') {
switch ($options['match']) {
case 'contains':
$query
->condition('title', '%' . $options['string'] . '%', 'LIKE');
break;
case 'starts_with':
$query
->condition('q.title', $options['string'] . '%', 'LIKE');
break;
case 'equals':
default:
// no match type or incorrect match type: use "="
$query
->condition('q.title', $options['string']);
break;
}
}
if ($options['ids']) {
$query
->condition('q.qid', $options['ids'], 'IN');
}
if ($options['limit']) {
$query
->range(0, $options['limit']);
}
$query
->orderBy($queue_title_alias);
$result = $query
->execute()
->fetchAll();
$references = array();
foreach ($result as $queue) {
$references[$queue->qid] = array(
'title' => $queue->title,
'rendered' => check_plain($queue->title),
);
}
// Store the results.
$results[$cid] = !empty($references) ? $references : array();
}
$references = $results[$cid];
$matches = array();
foreach ($references as $id => $row) {
// Markup is fine in autocompletion results (might happen when rendered
// through Views) but we want to remove hyperlinks.
$suggestion = preg_replace('/<a href="([^<]*)">([^<]*)<\\/a>/', '$2', $row['rendered']);
// Add a class wrapper for a few required CSS overrides.
$matches[$row['title'] . " [qid: {$id}]"] = '<div class="reference-autocomplete">' . $suggestion . '</div>';
}
drupal_json_output($matches);
}