function inline_conditions_autocomplete_callback in Inline Conditions 7
Menu callback: autocomplete the label of an entity.
Assumes "tag mode" (multiple allowed entries). Mostly stolen from entityreference.
Parameters
string $entity_type: The entity type.
bool $product_mode: Boolean indicating whether to limit taxonomy terms to vocabularies used by commerce_product bundles.
bool $single: Indicate a single value autocomple field.
string $string: The label of the entity to query by.
1 string reference to 'inline_conditions_autocomplete_callback'
- inline_conditions_menu in ./
inline_conditions.module - Implements hook_menu().
File
- ./
inline_conditions.module, line 157 - Extends Drupal 7 with a new field type to manage rules conditions directly from a field.
Code
function inline_conditions_autocomplete_callback($entity_type, $product_mode, $single = FALSE, $string = '') {
$matches = array();
$entity_info = entity_get_info($entity_type);
if ($product_mode) {
$product_node_types = commerce_product_reference_node_types();
$product_node_types = array_keys($product_node_types);
}
// Single mode.
if ($single) {
// Get the current string from field.
$tag_last = $string;
}
else {
// The user enters a comma-separated list of tags.
// We only autocomplete the last tag.@
$tags_typed = drupal_explode_tags($string);
$tag_last = drupal_strtolower(array_pop($tags_typed));
if (!empty($tag_last)) {
$prefix = count($tags_typed) ? implode(', ', $tags_typed) . ', ' : '';
}
}
if (!empty($tag_last)) {
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', $entity_type);
$query
->propertyCondition($entity_info['entity keys']['label'], $tag_last, 'CONTAINS');
$query
->addTag($entity_type . '_access');
$query
->range(0, 10);
if ($entity_type == 'node') {
// Limit the query to product display node types.
if ($product_mode) {
$query
->entityCondition('bundle', $product_node_types);
}
// If there are no access control modules enabled, and the user does not
// have permission to bypass node access, limit the nodes to 'published'.
if (!user_access('bypass node access') && !count(module_implements('node_grants'))) {
$query
->propertyCondition('status', NODE_PUBLISHED);
}
}
elseif ($entity_type == 'taxonomy_term' && $product_mode) {
// Gather all vocabularies referenced from term reference fields on
// product display node types.
$vids = array();
$vocabulary_data = taxonomy_vocabulary_get_names();
foreach (field_info_instances('commerce_product') as $instance) {
foreach ($instance as $field_name => $field_properties) {
$field = field_info_field($field_name);
if ($field['type'] == 'taxonomy_term_reference') {
$vocabulary_name = $field['settings']['allowed_values'][0]['vocabulary'];
$vids[] = $vocabulary_data[$vocabulary_name]->vid;
}
}
}
// Limit the query to only those vocabularies.
if ($vids) {
$query
->propertyCondition('vid', array_unique($vids));
}
}
$results = $query
->execute();
if (!empty($results[$entity_type])) {
$entities = entity_load($entity_type, array_keys($results[$entity_type]));
foreach ($entities as $entity_id => $entity) {
$label = entity_label($entity_type, $entity);
$label = check_plain($label);
$key = "{$label} ({$entity_id})";
// Strip things like starting/trailing white spaces,
// line breaks and tags.
$key = preg_replace('/\\s\\s+/', ' ', str_replace("\n", '', trim(decode_entities(strip_tags($key)))));
// Names containing commas or quotes must be wrapped in quotes.
if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) {
$key = '"' . str_replace('"', '""', $key) . '"';
}
$matches[$prefix . $key] = '<div class="reference-autocomplete">' . $label . '</div>';
}
}
}
drupal_json_output($matches);
}