You are here

function dynamic_entity_reference_autocomplete in Dynamic Entity Reference 7

Dynamic entity reference auto-complete callback.

1 string reference to 'dynamic_entity_reference_autocomplete'
dynamic_entity_reference_menu in ./dynamic_entity_reference.module
Implements hook_menu().

File

./dynamic_entity_reference.pages.inc, line 11
Contains page callbacks for the module.

Code

function dynamic_entity_reference_autocomplete($target_type = NULL, $string = NULL) {
  if ($target_type == NULL) {
    drupal_json_output(array());
  }
  $match_operator = 'CONTAINS';

  // Get the typed string, if exists from the URL.
  $match = drupal_strtolower($string);
  $options = dynamic_entity_reference_get_referenceable_entities($target_type, $match, $match_operator, 10);
  $matches = array();

  // Loop through the entities and convert them into autocomplete output.
  foreach ($options as $values) {
    foreach ($values as $entity_id => $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.
      $matches[$key] = '<div class="reference-autocomplete">' . $label . '</div>';
    }
  }
  drupal_json_output($matches);
}