You are here

function entity_embed_autocomplete_entity in Entity Embed 7.2

Same name and namespace in other branches
  1. 7.3 entity_embed.module \entity_embed_autocomplete_entity()
  2. 7 entity_embed.module \entity_embed_autocomplete_entity()

Page callback: Autocomplete for entities.

Parameters

$entity_type: The type of the entity being saved.

$entity_id: The ID of the entity being saved.

Return value

Any matching entities output as JSON.

1 string reference to 'entity_embed_autocomplete_entity'
entity_embed_menu in ./entity_embed.module
Implements hook_menu().

File

./entity_embed.module, line 369
Provides a CKEditor plugin and text filter for embedding and rendering entities.

Code

function entity_embed_autocomplete_entity($filter_format, $embed_button, $string) {
  $matches = array();
  $entity_type_id = $embed_button->entity_type;
  $entity_type_bundles = $embed_button->entity_type_bundles;
  $entity_type = entity_get_info($entity_type_id);

  // Prevent errors if the entity type has no label key.
  if (empty($entity_type['entity keys']['label'])) {
    return drupal_json_output($matches);
  }
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', $entity_type_id)
    ->propertyCondition($entity_type['entity keys']['label'], $string, 'STARTS_WITH')
    ->range(0, 10)
    ->propertyOrderBy($entity_type['entity keys']['label'], 'DESC')
    ->execute();

  // Add optional bundle restrictions.
  if (!empty($entity_type_bundles)) {
    $query
      ->entityCondition('bundle', array_keys($entity_type_bundles));
  }
  $results = $query
    ->execute();
  if (!empty($results)) {
    $ids = array_keys($results[$entity_type_id]);
    $entities = entity_load($entity_type_id, $ids);
    foreach ($entities as $entity) {
      $label = entity_label($entity_type_id, $entity);
      $matches[$label] = check_plain($label);
    }
  }
  return drupal_json_output($matches);
}