function text_noderef_json in Text or Nodereference 7
Same name and namespace in other branches
- 6 text_noderef.module \text_noderef_json()
Menu callback; Retrieve a pipe delimited string of autocomplete suggestions.
Parameters
$bundle_name: Bundle name whose field widget is being used.
$field_name: Field name whose widget is being used.
$string: The string used as needle.
1 string reference to 'text_noderef_json'
- text_noderef_menu in ./
text_noderef.module - Implements hook_menu().
File
- ./
text_noderef.module, line 84 - Text or nodereference field formatter for a text field and autocomplete widget.
Code
function text_noderef_json($bundle_name, $field_name, $string = '') {
$field = field_info_instance('node', $field_name, $bundle_name);
$matches = array();
// Do not act upon empty search string nor on unrelated fields.
if ($field['widget']['type'] == 'text_noderef_textfield' && $string != '') {
$needle = db_like($string) . '%';
if ($field['widget']['settings']['autocomplete_match'] == 'contains') {
$needle = '%' . $needle;
}
// Step 1/3: Check the available field data.
$query = db_select('node', 'n');
$query
->innerJoin('field_data_' . $field_name, 'fdf', 'n.nid = %alias.entity_id');
$query
->condition('n.status', 1)
->fields('fdf', array(
$field_name . '_value',
))
->condition($field_name . '_value', $needle, 'LIKE')
->groupBy($field_name . '_value')
->orderBy($field_name . '_value')
->range(0, 10)
->addTag('node_access');
$result = $query
->execute();
foreach ($result as $value) {
$value = $value->{$field_name . '_value'};
$matches[$value] = $value;
}
// Step 2/3: Check the node titles.
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'node');
$bundles = array_filter($field['widget']['settings']['bundles']);
if ($bundles) {
$query
->entityCondition('bundle', $bundles, 'IN');
}
$query
->propertyCondition('status', 1)
->propertyCondition('title', $needle, 'LIKE')
->propertyOrderBy('title');
$result = $query
->execute();
$matches += _text_noderef_check_result($string, $field, $result);
// Step 3/3: Merge and sanitize output.
asort($matches, SORT_LOCALE_STRING);
foreach ($matches as &$value) {
// Add a class wrapper for a few required CSS overrides.
$value = '<div class="reference-autocomplete">' . check_plain($value) . '</div>';
}
}
drupal_json_output($matches);
}