XF 2.0 Getting nodetree from specific node onwards

arms

Active member
#1
I'm trying to get the node list of node_id =2 and all its children

I'm using:

PHP:
    $nodeRepo = $this->getNodeRepo();
        $nodes = $nodeRepo->getNodeList();
        $nodeTree = count($nodes) ? $nodeRepo->createNodeTree($nodes) : null;//

        $nodeTree = $nodeTree->filter(null, function($id, $node, $depth, $children, $tree)
        {
            return ($children || $node->parent_node_id == '2');
        });
Is this the best way?
 

Chris D

XenForo developer
Staff member
#2
This might need to be tested, but we use something similar to this in XFMG to display the category tree from the current category onwards (if that option is enabled):
PHP:
$nodeRepo = $this->getNodeRepo();
$nodeList = $nodeRepo->getNodeList();
$fromNode = $this->em()->find('XF:Node', 2);

$nodeList = $nodeList->filter(function($item) use ($fromNode)
{
    return ($item->lft > $fromNode->lft && $item->rgt < $fromNode->rgt);
});

$nodeTree = count($nodeList) ? $nodeRepo->createNodeTree($nodeList, $fromNode->node_id) : null;
 

arms

Active member
#3
Almost @Chris D ,

That only gives the children. Node 2 isn't showing:

1507834297760.png
also tried;
PHP:
return ($item->lft >= $fromNode->lft && $item->rgt <= $fromNode->rgt);
 
Last edited:

arms

Active member
#5
Tried <= and >= earlier.
nodeTree picks up node 2.

PHP:
$nodeExtras = $nodeTree ? $nodeRepo->getNodeListExtras($nodeTree) : null;
nodeExtras doesn't pick it up.

1507839178727.png
 

arms

Active member
#6
@Chris D

Thanks, went for;

PHP:
        $nodeRepo = $this->getNodeRepo();
        $nodeList = $nodeRepo->getNodeList();
        $fromNode = $this->em()->find('XF:Node', 2);
        
        $nodeList = $nodeList->filter(function($item) use ($fromNode)
        {
            return ($item->lft >= $fromNode->lft && $item->rgt <= $fromNode->rgt);
        });
        
        $nodeTree = count($nodeList) ? $nodeRepo->createNodeTree($nodeList, $fromNode->parent_node_id) : null;
 

Chris D

XenForo developer
Staff member
#7
That's basically what I was referring to before. I presume the bit you initially missed was the root ID? Looks good to me.
 

arms

Active member
#8
That's basically what I was referring to before. I presume the bit you initially missed was the root ID? Looks good to me.
It was driving me nuts as $nodeTree was right but $nodeExtras wasn't. It didn't click that getNodeListExtras looked at children only.
 
Top