function relation_add_field_load in Relation add 7
Implements hook_field_load().
File
- ./
relation_add.module, line 253 - Relation Add module file.
Code
function relation_add_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
$cache =& drupal_static(__FUNCTION__);
$types = relation_get_types();
foreach ($entities as $id => $entity) {
$cache[$entity_type][$instances[$id]['bundle']]['type_settings'] = array();
$cache[$entity_type][$instances[$id]['bundle']]['relation_types'] = array();
if (isset($cache[$entity_type][$instances[$id]['bundle']]) && !empty($cache[$entity_type][$instances[$id]['bundle']]['type_settings'])) {
$type_settings = $cache[$entity_type][$instances[$id]['bundle']]['type_settings'];
}
else {
if (!empty($instances[$id]['settings']['relation_type'])) {
$type_settings = $instances[$id]['settings']['relation_type'];
$cache[$entity_type][$instances[$id]['bundle']]['type_settings'] = $type_settings;
}
else {
$type_settings = array();
$source_types = relation_get_available_types($entity_type, $instances[$id]['bundle']);
foreach ($source_types as $r_type_id => $r_type) {
$type_settings[] = $r_type_id;
}
$reverse_types = relation_get_available_types($entity_type, $instances[$id]['bundle'], 'target');
foreach ($reverse_types as $r_type_id => $r_type) {
$type_settings[] = $r_type_id . ':reverse';
}
$cache[$entity_type][$instances[$id]['bundle']]['type_settings'] = $type_settings;
}
}
$target_bundles = array();
if (isset($instances[$id]['settings']['relation_target_bundles']) && !empty($instances[$id]['settings']['relation_target_bundles'])) {
foreach ($instances[$id]['settings']['relation_target_bundles'] as $target_bundle) {
$target_bundle_array = explode(':', $target_bundle);
$target_bundles[$target_bundle_array[0]][$target_bundle_array[1]] = $target_bundle_array[1];
}
}
else {
$target_bundles = 'all';
}
if (!empty($type_settings)) {
if (isset($cache[$entity_type][$instances[$id]['bundle']]) && !empty($cache[$entity_type][$instances[$id]['bundle']]['relation_types'])) {
$relation_types = $cache[$entity_type][$instances[$id]['bundle']]['relation_types'];
}
else {
foreach ($type_settings as $type) {
$type_array = explode(':', $type);
$relation_types[$type_array[0]] = $type_array[0];
}
}
$query = relation_query($entity_type, $id);
if ($relation_types) {
$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) {
if (!(is_string($target_bundles) && 'all' == $target_bundles)) {
if ($relation) {
// Filter out relation items that contain
// unwanted endpoint bundle types.
foreach ($relation->endpoints[LANGUAGE_NONE] as $endpoint) {
if (!($endpoint['entity_type'] == $entity_type && $endpoint['entity_id'] == $id)) {
if (isset($target_bundles[$endpoint['entity_type']]) && is_array($target_bundles[$endpoint['entity_type']])) {
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', $endpoint['entity_type'])
->entityCondition('bundle', $target_bundles[$endpoint['entity_type']], 'IN')
->entityCondition('entity_id', $endpoint['entity_id']);
$result = $query
->execute();
if (empty($result)) {
unset($relation);
}
}
else {
unset($relation);
}
}
}
}
}
// Only add items when they are either not directional,
// or have their relation type in the field settings.
if (isset($relation)) {
$directional = $types[$relation->relation_type]->directional;
$relation_reverse = TRUE;
if ($relation->endpoints[LANGUAGE_NONE][0]['entity_id'] == $id && $relation->endpoints[LANGUAGE_NONE][0]['entity_type'] == $entity_type) {
$relation_reverse = FALSE;
}
if (!$directional || $relation_reverse && in_array($relation->relation_type . ':reverse', $type_settings) || !$relation_reverse && in_array($relation->relation_type, $type_settings)) {
$item = (array) $relation;
$item['my_entity_id'] = $id;
$items[$id][] = $item;
}
}
}
}
}
}