You are here

function uuid_link_autocomplete in UUID Link 7

Same name and namespace in other branches
  1. 6 uuid_link.module \uuid_link_autocomplete()

Autocomplete callback for entities.

1 string reference to 'uuid_link_autocomplete'
uuid_link_menu in ./uuid_link.module
Implements hook_menu().

File

./uuid_link.module, line 305
Provides a filter and UI for adding links to entities that are not affected by changes in URL alias.

Code

function uuid_link_autocomplete($type, $string = '') {
  $matches = array();
  $entity_type = entity_get_info($type);
  if (!empty($string) && !empty($entity_type['entity keys']['label'])) {
    $label_key = $entity_type['entity keys']['label'];
    $query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type', $type)
      ->propertyCondition($label_key, $string, 'STARTS_WITH')
      ->propertyOrderBy($label_key)
      ->range(0, 15);

    // Only search for published nodes.
    if ($type == 'node') {
      $query
        ->propertyCondition('status', 1);
    }
    if ($result = $query
      ->execute()) {
      $entities = entity_load($type, array_keys($result[$type]));
      $uuids = entity_get_uuid_by_id($type, array_keys($entities));
      foreach ($entities as $entity_id => $entity) {
        $uuid = $uuids[$entity_id];

        // If content is language specific show language code.
        if (!empty($entity->language) && $entity->language != LANGUAGE_NONE) {
          $label = t('[@language] @label', array(
            '@language' => $entity->language,
            '@label' => entity_label($type, $entity),
          ));
        }
        else {
          $label = check_plain(entity_label($type, $entity));
        }
        $matches[t('@label (@uuid)', array(
          '@label' => $label,
          '@uuid' => $uuid,
        ))] = $label;
      }
    }
  }
  drupal_json_output($matches);
}