XF 2.2 What's the easiest way to make $nodeTree available from PAGE_CONTAINER?

TrevC2

Member
Am I missing an obvious way to make $nodeTree accessible from the page container? I have a hacky solution involving $navTree to list nodes/forums contained under one category, but it feels like this should be easier.

I'm just trying to build a nav menu that contains a list of all forums and would prefer not to wrap everything in a category node to do it.

Thanks in advance.
 
This is one way.

Edit the forum_list template and add this:

HTML:
<xf:page option="nodeTree" value="{$nodeTree}" />
 
This is one way.

Edit the forum_list template and add this:

HTML:
<xf:page option="nodeTree" value="{$nodeTree}" />
Thanks, but I'm looking for it to be available on all pages. Right now I'm using navTree to accomplish the same thing, except the entire forum structure is wrapped in a category container to make it possible.
 
Thanks, but I'm looking for it to be available on all pages. Right now I'm using navTree to accomplish the same thing, except the entire forum structure is wrapped in a category container to make it possible.

If you want to make it available to all pages via PAGE_CONTAINER, I think it would be best to set up a controller_post_dispatch code event listener (see https://xenforo.com/docs/dev/criteria/#adding-code-event-listener). Inside the static method, assemble the node tree. Then use the reply object's setPageParam() method to assign it. It should look something like this:

Code:
<?php

namespace Your\Addon;

class Listener
{
    public static function controllerPostDispatch(
        \XF\Mvc\Controller $controller,
        $action,
        \XF\Mvc\ParameterBag $params,
        \XF\Mvc\Reply\AbstractReply &$reply
    ) {
            $nodeRepo = $this->getNodeRepo();
            $nodes = $nodeRepo->getNodeList();
            $nodeTree = $nodeRepo->createNodeTree($nodes);

            $reply->setPageParam('nodeTree', $nodeTree);
        }
    }
}
 
I almost forgot... You'll only want that running when the reply is a view, so wrap the code inside the following if statement:

Code:
if ($reply instanceof \XF\Mvc\Reply\View) {
    // Do the nodeTree assembly and assigning here.
}
 
templater_template_pre_render with event hint public:PAGE_CONTAINER is the more suitable code event I think.
 
  • Like
Reactions: Zig
Back
Top Bottom