You are here

function relation_add_autocomplete in Relation add 7

Autocomplete page for listing entities appropriate for a given relation type.

Parameters

string $type: The relation type to search for endpoints for.

string $direction: The direction for which to allow endpoint bundles.

string $field: Entity type, field and entity bundle data.

string $target_bundles: The target bundles in which to search for endpoints.

string $string: The string for which the search through entity labels will be run.

1 string reference to 'relation_add_autocomplete'
relation_add_menu in ./relation_add.module
Implements hook_menu().

File

./relation_add.module, line 50
Relation Add module file.

Code

function relation_add_autocomplete($type = '', $direction = 'target', $field = 'none', $target_bundles = 'all', $string = '') {
  if (empty($type) || empty($direction) || empty($string)) {
    exit;
  }

  // Removing the :reverse suffix if exists.
  $type_array = explode(':', $type);
  $type = $type_array[0];
  $entity_infos = entity_get_info();
  $relation_type = relation_type_load($type);
  $entity_bundles = array();
  $instance = array();
  if ($field !== 'none') {
    list($entity_type, $field_name, $bundle) = explode('-', $field);
    $instance = field_info_instance($entity_type, $field_name, $bundle);
  }

  // Use source bundles unless relation type is directional and we're looking
  // in the forward direction.
  $direction = $relation_type->directional && $direction == 'target' ? 'target_bundles' : 'source_bundles';
  if ($direction == 'target_bundles' && $target_bundles !== 'all') {
    $target_bundles_ar = explode('-', $target_bundles);
    foreach ($target_bundles_ar as $entity_bundle) {
      list($entity_type, $bundle) = explode(':', $entity_bundle, 2);
      $entity_bundles[$entity_type][] = $bundle;
    }
  }
  else {
    foreach ($relation_type->{$direction} as $entity_bundle) {
      list($entity_type, $bundle) = explode(':', $entity_bundle, 2);
      $entity_bundles[$entity_type][] = $bundle;
    }
  }
  if (module_exists('relation_add_views') && isset($instance['widget']['settings']['views']) && !empty($instance['widget']['settings']['views'])) {
    $suggestions = relation_add_views_autocomplet($field, $instance, $type, $direction, $target_bundles, $string);
  }
  else {

    // Get about 12, rounded up.
    $limit = ceil(12 / count(array_keys($entity_bundles)));
    $suggestions = array();
    foreach ($entity_bundles as $entity_type => $bundles) {

      // Get the name of the column in the base table for the entity type.
      if ($entity_type == 'user') {

        // Special case for users.
        $label_key = 'name';
      }
      elseif (isset($entity_infos[$entity_type]['entity keys']['label'])) {
        $label_key = $entity_infos[$entity_type]['entity keys']['label'];
      }
      else {
        if ('redhen_' == substr($entity_type, 0, 7)) {

          // Special case for users.
          $label_key = 'label';
        }
        elseif ('field_collection_item' == $entity_type) {
          $label_key = $entity_infos[$entity_type]['entity keys']['id'];
        }
        else {

          // Can't find a label to search over, give up.
          continue;
        }
      }
      $query = new EntityFieldQuery();
      $query
        ->entityCondition('entity_type', $entity_type);
      if (!empty($instance) && isset($instance['widget']['settings']['relation_endpoint_search_by_id']) && $instance['widget']['settings']['relation_endpoint_search_by_id'] && preg_match("/^[0-9]+\$/", $string)) {

        // We are most likely searching for an entity ID.
        $query
          ->entityCondition('entity_id', (int) $string);
      }
      else {
        if ('redhen_contact' == $entity_type) {
          $query
            ->addTag('redhen_contact_label');
        }
        $query
          ->propertyCondition($label_key, $string, 'CONTAINS');
      }
      $query
        ->range(0, $limit);
      if (!in_array('*', $bundles) && 'taxonomy_term' != $entity_type && 'user' != $entity_type) {
        $query
          ->entityCondition('bundle', $bundles, 'IN');
      }
      elseif (!in_array('*', $bundles) && $entity_type == 'taxonomy_term') {
        $vocabularies = taxonomy_vocabulary_load_multiple(NULL, array(
          'machine_name' => $bundles,
        ));
        $bundles = array_keys($vocabularies);
        $query
          ->propertyCondition('vid', $bundles, 'IN');
      }
      $query
        ->addTag('efq_relation_add_autocomplete');
      if ($results = $query
        ->execute()) {
        foreach (array_keys($results[$entity_type]) as $id) {
          $entities = entity_load($entity_type, array(
            $id,
          ));
          $entity = reset($entities);
          $label = entity_label($entity_type, $entity);
          $bundle = '';
          if (!empty($instance) && $instance['widget']['settings']['relation_endpoint_bundle_display']) {
            list(, , $bundle) = entity_extract_ids($entity_type, $entity);
            if (!empty($bundle)) {
              $bundle = ' - (' . $bundle . ')';
            }
          }
          if (!empty($instance) && $instance['widget']['settings']['relation_endpoint_iso_language_codes'] && isset($entity->language)) {
            $suggestions[$label . $bundle . ' [' . $entity_type . ':' . $id . ']'] = '[' . $entity->language . '] ' . $label . $bundle . ' [' . $entity_type . ':' . $id . ']';
          }
          else {
            $suggestions[$label . $bundle . ' [' . $entity_type . ':' . $id . ']'] = $label . $bundle . ' [' . $entity_type . ':' . $id . ']';
          }
        }
      }
    }
  }
  drupal_json_output($suggestions);
}