summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCzövek András2011-04-19 09:35:54 (GMT)
committerCzövek András2011-04-19 09:35:54 (GMT)
commit03eeed71a40a36e7913d51035218ec2f27dc9ad0 (patch)
tree601015359884d67c4931157e4fa4bdef55d5b8ab
parent4e0a3970d9df105776b9e11d776339cede598108 (diff)
Issue #1113880 by czigor: Fixed empty cnr fields6.x-4.x
-rw-r--r--corresponding_node_references.crud.inc45
1 files changed, 25 insertions, 20 deletions
diff --git a/corresponding_node_references.crud.inc b/corresponding_node_references.crud.inc
index 497b191..a7a7ceb 100644
--- a/corresponding_node_references.crud.inc
+++ b/corresponding_node_references.crud.inc
@@ -23,7 +23,7 @@ function corresponding_node_references_insert($home_node, $home_field, $away_nod
// Load the referenced node if it is of the specified away type.
if ($referenced_node = node_load(array('nid' => $reference['nid'], 'type' => $away_node_type), NULL, true)) {
// Add the new reference.
-
+
// If there are no other references, we need to make sure this is delta 0
if ($referenced_node->{$away_field}[0]['nid'] == NULL) {
$referenced_node->{$away_field}[0]['nid'] = $home_node->nid;
@@ -38,12 +38,12 @@ function corresponding_node_references_insert($home_node, $home_field, $away_nod
break;
}
}
-
+
if (!$exists) {
$referenced_node->{$away_field}[] = array('nid' => $home_node->nid);
}
}
-
+
_corresponding_node_references_update($referenced_node);
}
}
@@ -67,7 +67,7 @@ function corresponding_node_references_insert($home_node, $home_field, $away_nod
function corresponding_node_references_update($home_node, $home_field, $away_node_type, $away_field, $process_unchanged = FALSE) {
$old_node = node_load($home_node->nid, NULL, false);
$old = $new = array();
-
+
//if ($home_node->$home_field != $old_node->$home_field) {
// Determine the nodereference values before the update.
if (isset($old_node->$home_field) && is_array($old_node->$home_field)) {
@@ -77,12 +77,12 @@ 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) {
$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 $reference) {
@@ -91,7 +91,7 @@ function corresponding_node_references_update($home_node, $home_field, $away_nod
}
}
}
-
+
// Handle removed references.
if ($removed = array_diff($old, $new)) {
foreach ($removed as $nid) {
@@ -127,21 +127,21 @@ function corresponding_node_references_update($home_node, $home_field, $away_nod
}
}
}
-
+
//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 ($referenced_node->{$away_field}) {
- foreach ($referenced_node->{$away_field} as $key => $value) {
+ foreach ($referenced_node->{$away_field} as $key => $value) {
if (!empty($value['nid'])) {
$values[] = $value;
- }
+ }
}
}
$referenced_node->{$away_field} = $values;
-
- // Add the new reference. Don't create a duplicate.
- if (!$exists) {
+
+ // Add the new reference. Don't create a duplicate.
+ if (!$exists) {
//Get the allowed values
$unlimited = false;
$field = content_fields($away_field, $referenced_node->type);
@@ -154,8 +154,8 @@ function corresponding_node_references_update($home_node, $home_field, $away_nod
else {
$allowed_references = $field['multiple'];
}
-
- //check for reference overloading
+
+ //check for reference overloading
$references = count($referenced_node->{$away_field}) + 1;
if (($allowed_references >= $references) || $unlimited) {
$referenced_node->{$away_field}[] = array('nid' => $home_node->nid);
@@ -165,11 +165,11 @@ function corresponding_node_references_update($home_node, $home_field, $away_nod
$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',
+ 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,
+ '@title' => $referenced_node->title,
'@url' => url('node/'. $referenced_node->nid),
'@t_reference' => $t_reference,
'@t_allowed' => $t_allowed,
@@ -204,6 +204,11 @@ function corresponding_node_references_delete($home_node, $home_field, $away_nod
// Remove references to the deleted node.
if ($value['nid'] && $value['nid'] == $home_node->nid) {
unset($referenced_node->{$away_field}[$key]);
+ if (empty($referenced_node->{$away_field})){
+ //We need at least one element of the array to exist, so that
+ //if you delete the node later, this foreach does not throw an error.
+ $referenced_node->{$away_field}[0]['nid'] = $uninitialized;
+ }
_corresponding_node_references_update($referenced_node);
break;
}