You are here

function _ref_fieldl_autocomplete in (Entity)Reference Field Synchronization 7

Autocomplete function for entities. Searches in label field of the entity.

Parameters

$instance_id: The instance ID for the given instance. This is ues to check if user came a form that displayed the widget in the last 3 hours. If so access is allowed. Stored in $_SESSION['ref_field'][$token].

$field_name: Name for the current field

$entity_type: Entity type which this field is attached to

$budnle_name: Bundle name which this field is attached to

$string: Text to search for

Return value

associative array suitable for autocomplete fields where:

See also

ref_field_encode().

1 string reference to '_ref_fieldl_autocomplete'
ref_field_menu in ./ref_field.module
Implements hook_menu().

File

./ref_field.module, line 557

Code

function _ref_fieldl_autocomplete($instance_id, $field_name, $entity_type, $bundle_name, $string = '') {

  // Unset old tokens
  if (isset($_SESSION['ref_field'])) {
    foreach ($_SESSION['ref_field'] as $id => $instance_time) {
      if ($_SESSION['ref_field'][$id] < time() - REFFIELDTIMEOUT) {
        unset($_SESSION['ref_field'][$id]);
      }
    }
  }

  // If we got here but token does not exists, probably means the token was old
  if (isset($_SESSION['ref_field'][$instance_id])) {
    $field = field_info_field($field_name);

    // Type of entity to autocomplete
    $ref_entity_type = $field['settings']['entity'];

    // Bundles to filter by
    $ref_bundles = $field['settings']['bundles'];

    // Initialize array to store autocomplete matches
    $matches = array();
    $ref_entity_info = entity_get_info($ref_entity_type);
    if ($string && $ref_entity_info) {
      $ref_base_table = $ref_entity_info['base table'];
      $ref_id_column = $ref_entity_info['entity keys']['id'];
      $ref_label_column = FALSE;
      if (isset($ref_entity_info['entity keys']['label'])) {
        $ref_label_column = $ref_entity_info['entity keys']['label'];
      }
      elseif ($ref_entity_type == 'user') {
        $ref_label_column = 'name';
      }

      // Autocomplete is only compatible with entities that have labels column
      // @see hook_entity_info().
      // @see entity_label().
      if ($ref_label_column) {
        $query = new EntityFieldQuery();
        $query
          ->entityCondition('entity_type', $ref_entity_type);
        if ($ref_bundles) {
          switch ($ref_entity_type) {
            case 'taxonomy_term':

              // Special case for taxonomy_term
              // http://api.drupal.org/api/drupal/includes--entity.inc/function/EntityFieldQuery%3A%3AentityCondition/7
              $query
                ->propertyCondition('vid', $ref_bundles, 'IN');
              break;
            default:
              $query
                ->entityCondition('bundle', $ref_bundles, 'IN');
              break;
          }
        }
        $query
          ->propertyCondition($ref_label_column, db_like($string), 'CONTAINS');

        // Should we limit?
        $results = $query
          ->range(0, 10)
          ->execute();
        $ref_entities = entity_load($ref_entity_type, array_keys($results[$ref_entity_type]));
        foreach ($ref_entities as $ref_entity) {
          $matches[ref_field_encode($ref_entity_type, $ref_entity)] = entity_label($ref_entity_type, $ref_entity);
        }
      }
      else {
        $matches[t('error')] = t('Autocomplete is not compatible<br />with %entity_type entities,<br />please contact your administrator.', array(
          '%entity_type' => $ref_entity_info['label'],
        ));
      }
    }
  }
  else {

    // Show a please refresh message if user didn't access the widget in the
    // last x hours
    // Short lines to make sure it fits in short input fields
    $matches[t('error')] = t('Please refresh the form to<br />get access to the autocomplete.<br />If error persist, you may not have<br />access to this autocomplete\'s content.');
  }
  drupal_json_output($matches);
}