function inline_entity_form_autocomplete in Inline Entity Form 7
Returns output for inline entity form autocompletes.
Supports commerce_product_reference and entityreference fields.
1 string reference to 'inline_entity_form_autocomplete'
- inline_entity_form_menu in ./
inline_entity_form.module - Implements hook_menu().
File
- ./
inline_entity_form.module, line 41 - Provides a widget for inline management (creation, modification, removal) of referenced entities. The primary use case is the parent -> children one (for example, order -> line items), where the child entities are never managed outside the…
Code
function inline_entity_form_autocomplete($entity_type, $field_name, $bundle, $string = '') {
$field = field_info_field($field_name);
$instance = field_info_instance($entity_type, $field_name, $bundle);
$settings = inline_entity_form_settings($field, $instance);
$controller = inline_entity_form_get_controller($instance);
// The current entity type is not supported, or the string is empty.
// strlen() is used instead of empty() since '0' is a valid value.
if (!$field || !$instance || !$controller || !strlen($string)) {
return MENU_ACCESS_DENIED;
}
$results = array();
if ($field['type'] == 'commerce_product_reference') {
$match_operator = strtolower($controller
->getSetting('match_operator'));
$products = commerce_product_match_products($field, $instance, $string, $match_operator, array(), 10, TRUE);
// Loop through the products and convert them into autocomplete output.
foreach ($products as $product_id => $data) {
$results[] = t('@label (!entity_id)', array(
'@label' => $data['title'],
'!entity_id' => $product_id,
));
}
}
elseif ($field['type'] == 'entityreference') {
$handler = entityreference_get_selection_handler($field, $instance, $settings['entity_type']);
$entity_labels = $handler
->getReferencableEntities($string, $controller
->getSetting('match_operator'), 10);
foreach ($entity_labels as $bundle => $labels) {
// Loop through each entity type, and autocomplete with its titles.
foreach ($labels as $entity_id => $label) {
// entityreference has already check_plain-ed the title.
$results[] = t('!label (!entity_id)', array(
'!label' => $label,
'!entity_id' => $entity_id,
));
}
}
}
$matches = array();
foreach ($results as $result) {
// Strip things like starting/trailing white spaces, line breaks and tags.
$key = preg_replace('/\\s\\s+/', ' ', str_replace("\n", '', trim(decode_entities(strip_tags($result)))));
$matches[$key] = '<div class="reference-autocomplete">' . $result . '</div>';
}
drupal_json_output($matches);
}