API relatedcontent_get_nodes returns wrong array info
jgumpert - November 3, 2008 - 01:45
| Project: | RelatedContent |
| Version: | 5.x-1.7 |
| Component: | Code |
| Category: | bug report |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Jump to:
Description
The function relatedcontent_get_nodes(&$node) called on a node from node.tpl.php returns the following array:
Array
(
[0] => 48
)This is the value of the array key '48' which actually contains all the node id's, which you can see in $node->nodes like the following related content nodes:
Array
(
[48] => Array
(
[2908] => -2
[179] => -1
)
)So to get the right values inside of key 48, I've needed to adjust the logic to:
$nodes = $node->nodes; // Keeps $node immutable.
asort($nodes, SORT_NUMERIC);
$r_nodes = array();
$keys = array_keys($nodes);
foreach($keys as $key){
$r_nodes = $nodes[$key];
}which returns the set of related content node id's:
Array
(
[2908] => -2
[179] => -1
)I haven't yet created a patch because I've also been messing with some other bits of the module code for customized needs.

#1
I had this problem as well. Thanks for posting this. :D One minor edit, the return line also needs to be modified which isn't shown in your snippet. This is what the full function looks like:
<?phpfunction relatedcontent_get_nodes(&$node) {
$nodes = $node->nodes; // Keeps $node immutable.
asort($nodes, SORT_NUMERIC);
$r_nodes = array();
$keys = array_keys($nodes);
foreach($keys as $key) {
$r_nodes = $nodes[$key];
}
return array_keys($r_nodes);
}
?>
Just posting this clarification in case anyone else runs into this.