XF 2.1 findNodesForList() and limit to an individual category and its subforums

Scandal

Well-known member
Well, on Node repository, there is the following method which return the $finder for the forums which will be displayed on forum home page.
PHP:
    public function findNodesForList(\XF\Entity\Node $withinNode = null)
    {
        /** @var \XF\Finder\Node $finder */
        $finder = $this->finder('XF:Node');
        if ($withinNode)
        {
            $finder->descendantOf($withinNode);
        }
        $finder->listable()
            ->setDefaultOrder('lft');
       
        return $finder;
    }
Let's say that I have the following structure that's returned by the above method:
- Category 1
-- Sub1
-- Sub2
-- Sub3
--- Sub4
--- Sub5
-- Sub6
- Category 2
-- Sub7
-- Sub8
--- Sub9
-- Sub10
- Category 3
-- Sub11
-- Sub12

My question: what do I have to apply to the $finder so the result on the forum list to be:
- Category 2
-- Sub7
-- Sub8
--- Sub9
-- Sub10

?

Let's say that the Category 2 has node_id = X.

I tried ->where('parent_node_id', X) but this was not returned something.
 
The method you posted takes an argument that will only return the descendants of a given node.

To get all listed nodes, in order, with hydrated data and filtered by visitor permissions:
PHP:
$nodes = $repository->getNodeList($category->Node);

To just get all listed nodes, in order:
PHP:
$finder = $repository->findNodesForList($category->Node);

...or use findDescendants() if you want to potentially include unlisted nodes (an optional second argument restricts the results to listed nodes only, defaulting to true):
PHP:
$finder = $repository->findDescendants($category->Node, false);
 
My issue is this:
  • On forum_list, I want to display only forums of a specific Category (node_id = 264) and all its children, instead of showing all forums.
  • Let's make it hardcoded, just for test. I'm going to Node.php Repository, and I added the relative line so the withinNode never be null:
PHP:
    public function getNodeList(\XF\Entity\Node $withinNode = null)
    {
        // Scandal start
        $withinNode = $this->finder('XF:Node')->where('node_id', 264)->fetchOne();
        // Scandal end
        
        if ($withinNode && !$withinNode->hasChildren())
        {
            return $this->em->getEmptyCollection();
        }

        $nodes = $this->findNodesForList($withinNode)->fetch();

        $this->loadNodeTypeDataForNodes($nodes);

        return $this->filterViewable($nodes);
    }
The result is a completely empty forum list (forum home) page. I don't know why.
 
It's because the tree is built expecting the root node to be 0. You'd be better off extending the forum controller and modifying the nodeTree param instead:

PHP:
$rootId = 264;
$nodeRepo = $this->getNodeRepo();
$nodeTree = $nodeRepo->createNodeTree($nodes, $rootId);
 
Omg, so great! All working as expected now, I can see all subforums in the correct depth level etc, thanks :)
 
Back
Top Bottom