XF 2.0 Getting nodetree from specific node onwards

arms

Well-known member
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?
 
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;
 
Almost @Chris D ,

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

1507834297760.webp
also tried;
PHP:
return ($item->lft >= $fromNode->lft && $item->rgt <= $fromNode->rgt);
 
Last edited:
Tried <= and >= earlier.
nodeTree picks up node 2.

PHP:
$nodeExtras = $nodeTree ? $nodeRepo->getNodeListExtras($nodeTree) : null;

nodeExtras doesn't pick it up.

1507839178727.webp
 
@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;
 
That's basically what I was referring to before. I presume the bit you initially missed was the root ID? Looks good to me.
 
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 Bottom