XF 2.1 Building a widget to display forum list

ivp

Active member
Trying to create widget to display forum list like this:
Code:
Parent forum 1
- Sub-forum 1.1 (number of messages in Sub-forum 1.1)
- Sub-forum 1.2 (number of messages in Sub-forum 1.2)

Parent forum 2
- Sub-forum 2.1 (number of messages in Sub-forum 2.1)
- Sub-forum 2.2 (number of messages in Sub-forum 2.2)
- Sub-forum 2.3 (number of messages in Sub-forum 2.3)

...

It seems that XF\Pub\Controller\Forum is responsible for rendering forum list, so wondering is it possible to somehow use it and change template to "my_template_forum_list" instead of "forum_list" to achieve the goal?
 
I think it is necessary to create a new widget and pass it to $nodeTree

PHP:
/** @var \XF\Repository\Node $nodeRepo */
        $nodeRepo = \XF::repository('XF:Node');
        $nodeTree = $nodeRepo->createNodeTree($nodeRepo->getFullNodeList());
        //$nodeTree = $nodeRepo->createNodeTree($nodeRepo->getFullNodeList(),1);
        
        // only list nodes that are forums or contain forums
        $nodeTree = $nodeTree->filter(null, function($id, $node, $depth, $children, $tree)
        {
            return ($children || $node->node_type_id == 'Forum');
        });
        
        $nodeExtras = $nodeTree ? $nodeRepo->getNodeListExtras($nodeTree) : null;
        
        $viewParams = [
            'nodeTree' => $nodeTree,
            'nodeExtras' => $nodeExtras
        ];
 
Top Bottom