summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCzövek András2011-05-19 12:19:48 (GMT)
committerCzövek András2011-05-19 12:19:48 (GMT)
commitc456598e59b57dba119366b29cebb686583e08e9 (patch)
tree8d2b073eecaff4f4c2965182cca7150d82a27d10
parent58c8b675e26f50cd726c9f26eed83244b17e9640 (diff)
Issue #1159304 by czigor: Fixing references between nodes with different languages.
-rw-r--r--corresponding_node_references.crud.inc149
1 files changed, 70 insertions, 79 deletions
diff --git a/corresponding_node_references.crud.inc b/corresponding_node_references.crud.inc
index dece0bf..0e6e521 100644
--- a/corresponding_node_references.crud.inc
+++ b/corresponding_node_references.crud.inc
@@ -20,7 +20,7 @@ function corresponding_node_references_insert($home_node, $home_field, $away_nod
// Determine the nodereference values after the insert.
if (isset($home_node->$home_field) && is_array($home_node->$home_field)) {
- foreach ($home_node->$home_field as $lang => $fields) {
+ foreach ($home_node->$home_field as $fields) {
foreach ($fields as $reference) {
if (!empty($reference['nid'])) {
// Load the referenced node if it is of the specified away type.
@@ -31,15 +31,15 @@ function corresponding_node_references_insert($home_node, $home_field, $away_nod
// Add the new reference.
// If there are no other references, we need to make sure this
// is delta 0
- if (array_key_exists($lang, $referenced_node->{$away_field}) == FALSE || $referenced_node->{$away_field}[$lang][0]['nid'] == NULL) {
- $referenced_node->{$away_field}[$lang][0]['nid'] = $home_node->nid;
+ if (array_key_exists($referenced_node->language, $referenced_node->{$away_field}) == FALSE || $referenced_node->{$away_field}[$referenced_node->language][0]['nid'] == NULL) {
+ $referenced_node->{$away_field}[$referenced_node->language][0]['nid'] = $home_node->nid;
}
else {
// Add the new reference.
// Check for doubles, could happen when nodes of same type are
// referenced.
$exists = FALSE;
- foreach ($referenced_node->{$away_field}[$lang] as $key => $value) {
+ foreach ($referenced_node->{$away_field}[$referenced_node->language] as $key => $value) {
if ($value['nid'] == $home_node->nid) {
$exists = TRUE;
break;
@@ -47,7 +47,7 @@ function corresponding_node_references_insert($home_node, $home_field, $away_nod
}
if (!$exists) {
- $referenced_node->{$away_field}[$lang][] = array('nid' => $home_node->nid);
+ $referenced_node->{$away_field}[$referenced_node->language][] = array('nid' => $home_node->nid);
}
}
_corresponding_node_references_update($referenced_node);
@@ -76,17 +76,16 @@ function corresponding_node_references_insert($home_node, $home_field, $away_nod
* reference fields that don't match up.
*/
function corresponding_node_references_update($home_node, $home_field, $away_node_type, $away_field, $process_unchanged = FALSE) {
- // Since node is not saved yet, $old_node and $home_node are different!
+ // Since home_node is just beeing saved, $old_node and $home_node are different!
$old_node = node_load($home_node->nid, NULL, FALSE);
$old = $new = array();
// Determine the nodereference values before the update.
if (isset($old_node->$home_field) && is_array($old_node->$home_field)) {
foreach ($old_node->$home_field as $lang => $fields) {
- $old[$lang] = array();
foreach ($fields as $reference) {
if (!empty($reference['nid'])) {
- $old[$lang][] = $reference['nid'];
+ $old[] = $reference['nid'];
}
}
}
@@ -95,18 +94,15 @@ function corresponding_node_references_update($home_node, $home_field, $away_nod
// If we are processing unchanged references, remove all new references
// from the old references.
if ($process_unchanged) {
- foreach ($old as $lang => $data) {
- $old[$lang] = array_diff($old[$lang], $new[$lang]);
- }
+ $old = array_diff($old, $new);
}
// Determine the nodereference values after the update.
if (isset($home_node->$home_field) && is_array($home_node->$home_field)) {
foreach ($home_node->$home_field as $lang => $fields) {
- $new[$lang] = array();
foreach ($fields as $reference) {
if (!empty($reference['nid'])) {
- $new[$lang][] = $reference['nid'];
+ $new[] = $reference['nid'];
}
}
}
@@ -114,19 +110,19 @@ function corresponding_node_references_update($home_node, $home_field, $away_nod
// Handle removed references.
if ( !empty($old) ) {
- foreach ($old as $lang => $data) {
- if ($removed = array_diff($old[$lang], $new[$lang])) {
+ foreach ($old as $data) {
+ if ($removed = array_diff($old, $new)) {
foreach ($removed as $nid) {
// Load the referenced node if it is of the specified away type.
if ($referenced_node = node_load($nid, NULL, FALSE)) {
// Self-references are handled by the node_reference module anyway.
if ($referenced_node->type == $away_node_type && $nid != $home_node->nid) {
- if (isset($referenced_node->{$away_field}[$lang]) && is_array($referenced_node->{$away_field}[$lang])) {
+ if (isset($referenced_node->{$away_field}[$referenced_node->language]) && is_array($referenced_node->{$away_field}[$referenced_node->language])) {
// Iterate through the away node's references.
- foreach ($referenced_node->{$away_field}[$lang] as $key => $value) {
+ foreach ($referenced_node->{$away_field}[$referenced_node->language] as $key => $value) {
// Remove references to the deleted node.
if ($value['nid'] && $value['nid'] == $home_node->nid) {
- unset($referenced_node->{$away_field}[$lang][$key]);
+ unset($referenced_node->{$away_field}[$referenced_node->language][$key]);
_corresponding_node_references_update($referenced_node);
break;
}
@@ -141,72 +137,67 @@ function corresponding_node_references_update($home_node, $home_field, $away_nod
// Handle added references.
// No array diff a reference overload could of happend or a mass update.
if ($added = $new) {
- foreach ($added as $added_lang => $added_langdata) {
- foreach ($added_langdata as $nid) {
-
- // Load the referenced node if it is of the specified away type.
- if ($referenced_node = node_load($nid, NULL, FALSE)) {
- // Self-references are handled by the node_reference module anyway.
- if ($referenced_node->type == $away_node_type && $nid != $home_node->nid) {
- // Detect whether the reference already exists.
- $exists = FALSE;
- if (isset($referenced_node->$away_field) && !empty($referenced_node->$away_field)) {
- foreach ($referenced_node->{$away_field}[$added_lang] as $data) {
- if ($data['nid'] == $home_node->nid) {
- $exists = TRUE;
- break;
- }
+ foreach ($added as $nid) {
+ // Load the referenced node if it is of the specified away type.
+ if ($referenced_node = node_load($nid, NULL, FALSE)) {
+ // Self-references are handled by the node_reference module anyway.
+ if ($referenced_node->type == $away_node_type && $nid != $home_node->nid) {
+ // Detect whether the reference already exists.
+ $exists = FALSE;
+ if (isset($referenced_node->{$away_field}[$referenced_node->language]) && !empty($referenced_node->{$away_field}[$referenced_node->language])) {
+ foreach ($referenced_node->{$away_field}[$referenced_node->language] as $data) {
+ if ($data['nid'] == $home_node->nid) {
+ $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_node->{$away_field}[$added_lang])) {
- foreach ($referenced_node->{$away_field}[$added_lang] as $value) {
- if (!empty($value['nid'])) {
- $values[] = $value;
- }
+ // 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_node->{$away_field}[$referenced_node->language])) {
+ foreach ($referenced_node->{$away_field}[$referenced_node->language] as $value) {
+ if (!empty($value['nid'])) {
+ $values[] = $value;
}
}
- $referenced_node->{$away_field}[$added_lang] = $values;
-
- // Add the new reference. Don't create a duplicate.
- if (!$exists) {
- // Get the allowed values.
- $unlimited = FALSE;
- $field = field_info_field($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_node->{$away_field}[$added_lang]) + 1;
- if (($allowed_references >= $references) || $unlimited) {
- $referenced_node->{$away_field}[$added_lang][] = array('nid' => $home_node->nid);
- _corresponding_node_references_update($referenced_node);
- }
- 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 cck field settings of the instance',
- array(
- '@title' => $referenced_node->title,
- '@url' => url('node/' . $referenced_node->nid),
- '@t_reference' => $t_reference,
- '@t_allowed' => $t_allowed,
- )
- ),
- 'error'
- );
- }
+ }
+ $referenced_node->{$away_field}[$referenced_node->language] = $values;
+ // Add the new reference. Don't create a duplicate.
+ if (!$exists) {
+ // Get the allowed values.
+ $unlimited = FALSE;
+ $field = field_info_field($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_node->{$away_field}[$referenced_node->language]) + 1;
+ if (($allowed_references >= $references) || $unlimited) {
+ $referenced_node->{$away_field}[$referenced_node->language][] = array('nid' => $home_node->nid);
+ _corresponding_node_references_update($referenced_node);
+ }
+ 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 cck field settings of the instance',
+ array(
+ '@title' => $referenced_node->title,
+ '@url' => url('node/' . $referenced_node->nid),
+ '@t_reference' => $t_reference,
+ '@t_allowed' => $t_allowed,
+ )
+ ),
+ 'error'
+ );
}
}
}