diff --git a/corresponding_entity_references.crud.inc b/corresponding_entity_references.crud.inc index 3736e98..4ad5e83 100644 --- a/corresponding_entity_references.crud.inc +++ b/corresponding_entity_references.crud.inc @@ -52,19 +52,17 @@ function corresponding_entity_references_insert($home_entity, $keys) { // referenced. $exists = FALSE; foreach ($referenced_entity->{$keys['away_field']}[LANGUAGE_NONE] as $key => $value) { - if ($value[$ids['away']] == $home_entity->$ids['home']) { + if ($value['target_id'] == $home_entity->$ids['home']) { $exists = TRUE; break; } } if (!$exists) { - $referenced_entity->{$keys['away_field']}[LANGUAGE_NONE][] = array($ids['home'] => $home_entity->$ids['home']); + $referenced_entity->{$keys['away_field']}[LANGUAGE_NONE][] = array('target_id' => $home_entity->$ids['home']); } } - _corresponding_entity_references_update($keys['away_entity_type'], $referenced_entity); - } } } @@ -114,14 +112,7 @@ function corresponding_entity_references_update($home_entity, $keys, $process_un } } - - // If we are processing unchanged references, remove all new references - // from the old references. - if ($process_unchanged) { - $old = array_diff($old, $new); - } - - // Determine the nodereference values after the update. + // Determine the entityreference values after the update. if (isset($home_entity->$keys['home_field']) && is_array($home_entity->$keys['home_field'])) { foreach ($home_entity->$keys['home_field'] as $lang => $fields) { foreach ($fields as $reference) { @@ -132,8 +123,12 @@ function corresponding_entity_references_update($home_entity, $keys, $process_un } } + if ($old == $new){ + return; + } + // Handle removed references. - if ( !empty($old) ) { + if (!empty($old) ) { foreach ($old as $data) { if ($removed = array_diff($old, $new)) { foreach ($removed as $id) { @@ -174,10 +169,9 @@ function corresponding_entity_references_update($home_entity, $keys, $process_un if ($referenced_entity->bundle_type == $keys['away_bundle'] && $id != $home_entity->$ids['home']) { // Detect whether the reference already exists. $exists = FALSE; - + if (isset($referenced_entity->{$keys['away_field']}[$referenced_entity->language]) && !empty($referenced_entity->{$keys['away_field']}[LANGUAGE_NONE])) { foreach ($referenced_entity->{$keys['away_field']}[LANGUAGE_NONE] as $data) { - if ($data['target_id'] == $home_entity->$ids['home']) { $exists = TRUE; break; @@ -271,7 +265,6 @@ function corresponding_entity_references_delete($home_entity, $keys, $process_un $referenced_entity->bundle_type = _corresponding_entity_references_entity_get_bundle($referenced_entity, $keys['away_entity_type']); if ($referenced_entity->bundle_type == $keys['away_bundle']) { // Iterate through the away entity's references. - foreach ($referenced_entity->{$keys['away_field']}[$lang] as $key => $value) { // Remove references to the deleted node. if ($value['target_id'] && $value['target_id'] == $home_entity->$ids['home']) { @@ -293,6 +286,7 @@ function corresponding_entity_references_delete($home_entity, $keys, $process_un * @param $node the referenced node to be updated. */ function _corresponding_entity_references_update($entity_type, $entity) { + field_attach_presave($entity_type, $entity); field_attach_update($entity_type, $entity); }