function relation_add_field_update in Relation add 7
Implements hook_field_update().
1 call to relation_add_field_update()
- relation_add_field_insert in ./
relation_add.module - Implements hook_field_insert().
File
- ./
relation_add.module, line 1018 - Relation Add module file.
Code
function relation_add_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
foreach ($items as $key => $item) {
if (isset($item['relation_options'])) {
if (isset($item['delete']) && $item['delete']) {
if (isset($item['relation_options']['rid']) && $item['relation_options']['rid']) {
relation_delete($item['relation_options']['rid']);
cache_clear_all('field', 'cache_field', TRUE);
}
continue;
}
$type_array = explode(':', $item['relation_type']);
$type = $type_array[0];
$relation_reverse = isset($type_array[1]) && $type_array[1] == 'reverse';
$entity_label = entity_label($entity_type, $entity);
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
$relation_add = $entity_label . ' [' . $entity_type . ':' . $id . ']';
$entity_strings = array();
if (isset($item['relation_options']['targets'])) {
$targets =& $item['relation_options']['targets'];
for ($i = 2; $i; $i++) {
if (isset($targets['target_' . $i]) && !empty($targets['target_' . $i])) {
$entity_strings[] = $targets['target_' . $i];
}
else {
// Break loop.
$i = FALSE;
}
}
}
if (!isset($item['relation_options']['targets']) || is_array($entity_strings) && count($entity_strings)) {
// Add the current entity to the endpoints array.
if ($relation_reverse) {
// For reverse relations, add the "current entity"
// to the end of the array, else to the start.
array_push($entity_strings, $relation_add);
}
else {
array_unshift($entity_strings, $relation_add);
}
$entity_keys = array();
$i = 0;
foreach ($entity_strings as $r_index => $entity_string) {
$matches = array();
preg_match('/(.*)\\[([\\w\\d]+):(\\d+)\\]/', $entity_string, $matches);
if ($matches) {
$entity_keys[] = array(
'entity_label' => $matches[1],
'entity_type' => $matches[2],
'entity_id' => $matches[3],
'r_index' => $r_index,
);
if ('redhen_' == substr($entity_keys[$i]['entity_type'], 0, 7)) {
unset($entity_keys[$i]['entity_label']);
}
++$i;
}
}
if (isset($item['relation_options']['rid'])) {
if ($relation = relation_load($item['relation_options']['rid'])) {
if ($relation->relation_type == $type) {
$relation->endpoints[LANGUAGE_NONE] = $entity_keys;
$relation->is_new = FALSE;
}
else {
// Different relation type.
relation_delete($item['relation_options']['rid']);
$relation = relation_create($type, $entity_keys);
}
}
else {
// Failed load the relation.
$relation = relation_create($type, $entity_keys);
}
}
else {
$relation = relation_create($type, $entity_keys);
}
$form = $form_state = array();
$relation_instances = field_info_instances('relation', $relation->relation_type);
foreach ($item['relation_options'] as $relation_field_name => $relation_field) {
if (isset($relation_instances[$relation_field_name])) {
$relation_keys = array_keys($relation_field);
$langcode = array_shift($relation_keys);
$relation_field_items = array_shift($relation_field);
$relation_field = field_info_field($relation_field_name);
foreach ($relation_field_items as $delta => $relation_field_item) {
if (!is_numeric($delta)) {
unset($relation_field_items[$delta]);
}
}
field_default_submit('relation', $relation, $relation_field, $relation_instances[$relation_field_name], $langcode, $relation_field_items, $form, $form_state);
$relation->{$relation_field_name}[$langcode] = $relation_field_items;
}
else {
$relation->{$relation_field_name} = $relation_field;
}
}
relation_save($relation);
$items[$key] = (array) $relation;
}
elseif (isset($item['relation_options']['rid'])) {
relation_delete($item['relation_options']['rid']);
unset($items[$key]);
}
}
else {
if (isset($item['relation_type']) && in_array($item['relation_type'], $instance['settings']['relation_type'])) {
$type_array = explode(':', $item['relation_type']);
$type = $type_array[0];
$relation_reverse = isset($type_array[1]) && $type_array[1] == 'reverse';
if (!isset($item['rid']) || empty($item['rid'])) {
$relation_type = relation_type_load($type);
$new_relation = (array) relation_create($type, array());
$item += $new_relation;
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
$add_entpoint = TRUE;
foreach ($item['endpoints'][LANGUAGE_NONE] as $endpoint) {
if ($endpoint['entity_type'] == $entity_type && $endpoint['entity_id'] == $id) {
$add_entpoint = FALSE;
break;
}
}
if ($add_entpoint) {
$new_endpoint = array(
'entity_type' => $entity_type,
'entity_id' => $id,
);
if ($relation_type->directional && !$relation_reverse) {
array_unshift($item['endpoints'][LANGUAGE_NONE], $new_endpoint);
}
else {
$item['endpoints'][LANGUAGE_NONE][] = $new_endpoint;
}
}
}
else {
$item['relation_type'] = $type;
if (isset($item['is_new'])) {
$item['is_new'] = FALSE;
}
}
$relation = (object) $item;
relation_save($relation);
if (isset($relation->is_new)) {
unset($relation->is_new);
}
$items[$key] = (array) $relation;
}
}
}
}