You are here

function _token_insert_entity_autocomplete in Token Insert Entity 7

Returns a list of entity suggestions.

1 string reference to '_token_insert_entity_autocomplete'
token_insert_entity_menu in ./token_insert_entity.module
Implements hook_menu()

File

./token_insert_entity.pages.inc, line 60
Page callback implementations for token_insert_entity module.

Code

function _token_insert_entity_autocomplete($string) {
  $matches = array();

  // Define which property to search per entity type.
  $entities = _token_insert_entity_entity_type_titles();

  // Search over each entity type.
  foreach ($entities as $entity_type => $entity_property) {
    $query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type', $entity_type)
      ->propertyCondition($entity_property, $string, 'CONTAINS')
      ->addTag('token_insert_entity_autocomplete');
    $result = $query
      ->execute();
    $ids = !empty($result[$entity_type]) ? array_keys($result[$entity_type]) : array();
    if (count($ids)) {
      $entities = entity_load($entity_type, $ids);
      foreach ($entities as $id => $entity) {
        $bundle = !empty($entity->type) ? $entity->type : $entity_type;
        if (module_exists('entity_view_mode')) {
          $matches[$entity_type . ':' . $bundle . ':' . $id] = $bundle . ': ' . check_plain($entity->{$entity_property});
        }
        else {
          $matches[$entity_type . ':' . $id] = $bundle . ': ' . check_plain($entity->{$entity_property});
        }
      }
    }
  }
  return drupal_json_output($matches);
}