function cer_update in Corresponding Entity References 7
$keys = array( 'home_entity_type' => $key[0], 'home_bundle' => $key[1], 'home_field' => $key[2], 'away_entity_type' => $key[3], 'away_bundle' => $key[4], 'away_field' => $key[5], );
File
- ./
cer.crud.inc, line 90 - Include file providing corresponding node reference insert, update, and delete handling.
Code
function cer_update($home_entity, $keys, $process_unchanged = FALSE) {
$types = array(
'home' => $keys['home_entity_type'],
'away' => $keys['away_entity_type'],
);
$ids = _cer_entity_ids($types);
// Since home_entity is just being saved, $old_entity and $home_entity are different!
if ($process_unchanged == FALSE) {
$old_entity = $home_entity->original;
}
else {
$old_entity = $home_entity;
}
//$old_entity = $old_entity[$home_entity->$ids['home']];
$old = $new = array();
// Determine the entityreference values before the update.
if (isset($old_entity->{$keys}['home_field']) && is_array($old_entity->{$keys}['home_field'])) {
foreach ($old_entity->{$keys}['home_field'] as $lang => $fields) {
foreach ($fields as $reference) {
if (!empty($reference['target_id'])) {
$old[] = $reference['target_id'];
}
}
}
}
// 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) {
if (!empty($reference['target_id'])) {
$new[] = $reference['target_id'];
}
}
}
}
if ($old == $new && $process_unchanged == FALSE) {
return;
}
// Handle removed references.
if (!empty($old)) {
foreach ($old as $data) {
if ($removed = array_diff($old, $new)) {
foreach ($removed as $id) {
// Load the referenced node if it is of the specified away type.
if ($referenced_entity = entity_load($keys['away_entity_type'], array(
$id,
), NULL, FALSE)) {
$referenced_entity = $referenced_entity[$id];
$referenced_entity->bundle_type = _cer_entity_get_bundle($referenced_entity, $keys['away_entity_type']);
if ($referenced_entity->bundle_type == $keys['away_bundle']) {
if (isset($referenced_entity->{$keys['away_field']}[LANGUAGE_NONE]) && is_array($referenced_entity->{$keys['away_field']}[LANGUAGE_NONE])) {
// Iterate through the away node's references.
foreach ($referenced_entity->{$keys['away_field']}[LANGUAGE_NONE] as $key => $value) {
// Remove references to the deleted node.
if ($value['target_id'] && $value['target_id'] == $home_entity->{$ids}['home']) {
unset($referenced_entity->{$keys['away_field']}[LANGUAGE_NONE][$key]);
_cer_update($keys['away_entity_type'], $referenced_entity);
break;
}
}
}
}
}
}
}
}
}
// Handle added references.
// No array diff a reference overload could of happend or a mass update.
if ($added = $new) {
foreach ($added as $id) {
// Load the referenced entity if it is of the specified away type.
if ($referenced_entity = entity_load($keys['away_entity_type'], array(
$id,
), NULL, FALSE)) {
$referenced_entity = $referenced_entity[$id];
$referenced_entity->bundle_type = _cer_entity_get_bundle($referenced_entity, $keys['away_entity_type']);
if ($referenced_entity->bundle_type == $keys['away_bundle']) {
// Detect whether the reference already exists.
$exists = FALSE;
if (isset($referenced_entity->{$keys['away_field']}[LANGUAGE_NONE]) && !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;
}
}
}
// Empty places are removed.
// Yes this means the deltas change on the away node when a
// reference is made on the home node.
$values = array();
if (isset($referenced_entity->{$keys['away_field']}[LANGUAGE_NONE])) {
foreach ($referenced_entity->{$keys['away_field']}[LANGUAGE_NONE] as $value) {
if (!empty($value['target_id'])) {
$values[] = $value;
}
}
}
$referenced_entity->{$keys['away_field']}[LANGUAGE_NONE] = $values;
// Add the new reference. Don't create a duplicate.
if (!$exists) {
// Get the allowed values.
$unlimited = FALSE;
$field = field_info_field($keys['away_field']);
if ($field) {
if ($field['cardinality'] == -1) {
$unlimited = TRUE;
$allowed_references = 0;
}
else {
$allowed_references = $field['cardinality'];
}
// Check for reference overloading.
$references = count($referenced_entity->{$keys['away_field']}[LANGUAGE_NONE]) + 1;
if ($allowed_references >= $references || $unlimited) {
$referenced_entity->{$keys['away_field']}[LANGUAGE_NONE][] = array(
'target_id' => $home_entity->{$ids}['home'],
);
_cer_update($keys['away_entity_type'], $referenced_entity);
}
else {
$t_reference = format_plural($references, '1 reference', '@count references');
$t_allowed = format_plural($allowed_references, '1 reference is', '@count references are');
drupal_set_message(t('Reference overloading: @title would of had @t_reference and only @t_allowed permitted. Before adding a reference, you would need to <a href="@url">edit</a> @title to remove an existing reference and resave this node to have make it correspond. Or you could allow this reference instance to have more references, go to the field settings for this instance.', array(
'@title' => $referenced_entity->title,
'@url' => url('node/' . $referenced_entity->{$ids}['away'] . '/edit'),
'@t_reference' => $t_reference,
'@t_allowed' => $t_allowed,
)), 'error');
}
}
}
}
}
}
}
}