You are here

function relation_dummy_field_field_prepare_view in Relation 7

Same name and namespace in other branches
  1. 8 relation_dummy_field/relation_dummy_field.module \relation_dummy_field_field_prepare_view()

Implements hook_field_prepare_view().

File

relation_dummy_field/relation_dummy_field.module, line 216
A field storing arbitrary relations between entities.

Code

function relation_dummy_field_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
  foreach ($entities as $id => $entity) {
    $relation_types = $instances[$id]['settings']['relation_type'];
    $query = relation_query($entity_type, $id)
      ->range(0, 50);
    if ($relation_types) {

      // TODO:SINGLE_DIR: Fails in cases where we want directional relations for
      // which the entity is only the source, but not the target, or vice versa.
      // But to do this properly would mean multiple queries (up to 3).
      $query
        ->entityCondition('bundle', $relation_types, 'IN');
    }
    $relation_ids = array_keys($query
      ->execute());

    // Who knows why but field does not like if the delta does not start at 0...
    $items[$id] = array();
    foreach (entity_load('relation', $relation_ids) as $relation) {
      $items[$id][] = (array) $relation;
    }
  }
}