How to set active menu options using hooks?

Started by @rjen, April 22, 2022, 01:47:31 PM

Previous topic - Next topic

@rjen

SMF version 2.1.1.

Used to have a number of extra menu options in SMF by directly updating subs.php.

Now I have created my own mod, using hook integrate_menu_buttons.
Works a charm and I got rid of a large chunk of custom code in subs.php.

Now I also made some changes in subs.phs that were needed to set the 'active menu option' depending on the page url. Using TinyPortal, so I need to set the active tab base on url information.

I would like to also move this code out of subs.php, but I do not know what hook to use to do this.
Any pointers?

FYI. Attached a piece of the adapted subs.php that I want to move out..

    // Logging out requires the session id in the url.
    if (isset($context['menu_buttons']['logout']))
        $context['menu_buttons']['logout']['href'] = sprintf($context['menu_buttons']['logout']['href'], $context['session_var'], $context['session_id']);

    // AW added URL grab to set active tab
    $curPageURL = 'http';
        if ( isset( $_SERVER["HTTPS"] ) && strtolower( $_SERVER["HTTPS"] ) == "on" ) {$curPageURL .= "s";}
            $curPageURL .= "://";
            $curPageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    // AW end additions
    // Figure out which action we are doing so we can set the active tab.
    // Default to home.
    $current_action = 'home';

    if (isset($context['menu_buttons'][$context['current_action']]))
        $current_action = $context['current_action'];
    elseif ($context['current_action'] == 'search2')
        $current_action = 'search';
    elseif ($context['current_action'] == 'theme')
        $current_action = isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'pick' ? 'profile' : 'admin';
    elseif ($context['current_action'] == 'signup2')
        $current_action = 'signup';
    elseif ($context['current_action'] == 'login2' || ($user_info['is_guest'] && $context['current_action'] == 'reminder'))
        $current_action = 'login';
    elseif ($context['current_action'] == 'groups' && $context['allow_moderation_center'])
        $current_action = 'moderate';
//AW added based on URL grab
    elseif ($curPageURL == $scripturl . '?cat=Over-de-FJR')
        $current_action = 'Over_de_FJR';
    elseif ($curPageURL == $scripturl . '?page=Geschiedenis-van-de-FJR')
        $current_action = 'Over_de_FJR';
    elseif ($curPageURL == $scripturl . '?page=Model-RP28-ultimate')
        $current_action = 'Over_de_FJR';
    elseif ($curPageURL == $scripturl . '?page=Model-RP28')
        $current_action = 'Over_de_FJR';
    elseif ($curPageURL == $scripturl . '?page=Model-RP23')
        $current_action = 'Over_de_FJR';
    elseif ($curPageURL == $scripturl . '?page=Model-RP13A')
        $current_action = 'Over_de_FJR';
    elseif ($curPageURL == $scripturl . '?page=Model-RP13')
        $current_action = 'Over_de_FJR';
    elseif ($curPageURL == $scripturl . '?page=Model-RP08-en-RP11')
        $current_action = 'Over_de_FJR';
    elseif ($curPageURL == $scripturl . '?page=Model-RP04')
        $current_action = 'Over_de_FJR';
    elseif ($curPageURL == $scripturl . '?page=Modellen-Verenigde-Staten')
        $current_action = 'Over_de_FJR';
    elseif ($curPageURL == $scripturl . '?cat=Onze-activiteiten')
        $current_action = 'Evenementen';
    elseif ($curPageURL == $scripturl . '?page=Onze-activiteiten')
        $current_action = 'Evenementen';
    elseif ($curPageURL == $scripturl . '?page=Fotohoek')
        $current_action = 'Evenementen';
    elseif ($curPageURL == $scripturl . '?page=Shop')
        $current_action = 'Shop';
    elseif ($curPageURL == $scripturl . '?cat=Shop')
        $current_action = 'Shop';
// AW end additions

    // There are certain exceptions to the above where we don't want anything on the menu highlighted.
    if ($context['current_action'] == 'profile' && !empty($context['user']['is_owner']))
Running SMF 2.1 with latest TinyPortal at www.fjr-club.nl

@rjen

Ok, just found the hook: integrate_current_action.

Can anyone by any chance point me to a sample mod that implements this hook?
Running SMF 2.1 with latest TinyPortal at www.fjr-club.nl

Arantor

I can't really imagine many mods actually using it.

But basically the hook function will be a function that accepts &$current_action as a variable, and you will want to overwrite it with whatever 'action' you want it to be if it matches your criteria.
Holder of controversial views, all of which my own.


@rjen

Figured it out...

moved the whole code to the hook and included that with my personal menu mod...

For reference:

In the package-info install the hook:
<hook hook="integrate_current_action" function="fjrclub_current_action" file="$sourcedir/fjrclub_menu.php" />

and in the code set the action...

function fjrclub_current_action(&$current_action)
{
global $context, $scripturl;
  loadLanguage('fjrclub_menu');

// URL grab
$curPageURL = 'http';
if ( isset( $_SERVER["HTTPS"] ) && strtolower( $_SERVER["HTTPS"] ) == "on" ) {$curPageURL .= "s";}
$curPageURL .= "://";
$curPageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];

// Set the current action
if ($curPageURL == $scripturl . '?cat=Over-de-FJR')
$current_action = 'Over_de_FJR';
elseif ($curPageURL == $scripturl . '?page=Geschiedenis-van-de-FJR')
$current_action = 'Over_de_FJR';
elseif ($curPageURL == $scripturl . '?page=Model-RP28-ultimate')
$current_action = 'Over_de_FJR';
elseif ($curPageURL == $scripturl . '?page=Model-RP28')
$current_action = 'Over_de_FJR';
elseif ($curPageURL == $scripturl . '?page=Model-RP23')
$current_action = 'Over_de_FJR';
elseif ($curPageURL == $scripturl . '?page=Model-RP13A')
$current_action = 'Over_de_FJR';
elseif ($curPageURL == $scripturl . '?page=Model-RP13')
$current_action = 'Over_de_FJR';
elseif ($curPageURL == $scripturl . '?page=Model-RP08-en-RP11')
$current_action = 'Over_de_FJR';
elseif ($curPageURL == $scripturl . '?page=Model-RP04')
$current_action = 'Over_de_FJR';
elseif ($curPageURL == $scripturl . '?page=Modellen-Verenigde-Staten')
$current_action = 'Over_de_FJR';
elseif ($curPageURL == $scripturl . '?cat=Onze-activiteiten')
$current_action = 'Evenementen';
elseif ($curPageURL == $scripturl . '?page=Onze-activiteiten')
$current_action = 'Evenementen';
elseif ($curPageURL == $scripturl . '?page=Fotohoek')
$current_action = 'Evenementen';
elseif ($curPageURL == $scripturl . '?page=Shop')
$current_action = 'Shop';
elseif ($curPageURL == $scripturl . '?cat=Shop')
$current_action = 'Shop';
}
Running SMF 2.1 with latest TinyPortal at www.fjr-club.nl

Advertisement: