XF 2 Small XenForo 2.3.7 add‑on: force specific public navigation tabs to be selected for two routes

zoldos

Well-known member
Licensed customer
@AndyB

XenForo version: 2.3.7
Theme: ThemeHouse UI.X Dark (but issue is core nav logic, not styling)
What I want

I have custom top‑level public navigation tabs that point to:

New Posts:

Nav ID: nav1000whatsnewZ

Type: Basic

Link: {{ link('whats-new/posts') }}

Badge callback: AddonsLab Unread Post Count → AL\UnreadPostCount\Callback::getUnreadPostCount

Member Albums:

Nav ID: albums001browse

Type: Basic

Link: /media/albums/

Both appear and work, but when those pages are loaded, XenForo highlights the wrong top‑level tab:

/whats-new/posts/48185/ highlights the forums/home/What’s New owner tab instead of my New Posts tab.

/media/albums/ highlights the Media/Gallery owner tab instead of my Member Albums tab.

I want each of these two routes to highlight my nav items (by their nav IDs) as the selected top‑level tab.
What I’ve already tried

Created the two custom nav items in Setup → Navigation → Public navigation (see above).

Moved “New Posts” out of the sub‑nav and into its own tab, same with “Member Albums” (XFMG).

Edited the whats_new_wrapper template; I can see $pageSelected == 'new_thread' is used for whats-new/posts, but changing things there didn’t change the top‑level selected tab.

Tried setting nav context via <xf:page option="navSelected" ...> and <xf:page option="selectedNav" ...> in the wrapper – no effect.

Created a small add‑on and experimented with bootstraps / listeners to call $controller->setSectionContext('...') for the routes, but from the public API side it looks like XenForo is already fixing the section context earlier at the route/controller level, so those hooks did not change which tab is selected.

So this really needs to be done the proper XenForo way at the route/controller level, not with another Basic nav link or late template hack.
What I’m asking you to implement

A very small XenForo 2.3 add‑on that:

For any request to the New Posts route family ( whats-new/posts/* – in my case /whats-new/posts/48185/):

Forces the public navigation section context to my nav item ID:
nav1000whatsnewZ

So that my New Posts tab shows as the active/selected top‑level tab for all New Posts pages.

For any request to Member Albums in XFMG (e.g. media/albums/* – in my case /media/albums/):

Forces the public navigation section context to my nav item ID:
albums001browse

So that my Member Albums tab shows as the active/selected top‑level tab for album browsing instead of the default Media/Gallery owner tab.

I’m fine with a clean, minimal solution that uses class extensions of the actual public controllers / route handlers and sets the section context at the right point in the lifecycle (before the navigation is rendered), following XenForo’s documented XFCP_* extension pattern.
 
try


Code:
  <?php

  namespace Test\NavFix;

  use XF\Mvc\Controller;
  use XF\Mvc\ParameterBag;

  class Listener
  {
      public static function controllerPreDispatch(Controller $controller, $action, ParameterBag $params)
      {
          $route = $controller->request()->getRoutePath();

          if (strpos($route, 'whats-new/posts') === 0)
          {
              $controller->setSectionContext('nav1000whatsnewZ');
          }
          else if (strpos($route, 'media/albums') === 0)
          {
              $controller->setSectionContext('albums001browse');
          }
      }
  }
 
Back
Top Bottom