Download & Extend

API relatedcontent_get_nodes returns wrong array info

Project:RelatedContent
Version:5.x-1.7
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:active

Issue Summary

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.

Comments

#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:

<?php
function 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.

#2

I find I have to do this to get the node ids:

$nodes = $node->nodes;
asort($nodes, SORT_NUMERIC);

$r_nodes = array();

foreach ($nodes as $cell) {
foreach ($cell as $key => $value) {
$r_nodes[] = $key;
}
}

#3

Can someone make a patch of this? I would like to use this module! If nto can you tell me what exact lines to replace? Sorry Im pretty bad at the coding

nobody click here