2

I'm having some issues with checking if a module is displayed on the current page.

I tried out different approaches with JModuleHelper methods, eg isEnabled, but it only works with module types not module "instances".

I also tried fetching the module by name or position, but objects returned do not contain any information about what menu items are assigned to this module.

I also considered fetching the menu item object or the article object (I'm checking this from a article layout override ~ com_content) but couldn't find any information that could help me with this.

Simply put: I need to check if a module is currently displayed on an article view.

I hope someone could help me with this. Thanks!

3
  • Modules are loaded after the component, hence I would suggest you try checking for modules later in the life cycle. Apr 11, 2018 at 8:00
  • I'm not sure if that would help this case. I need to check if a module is assigned to the current menu item/article, depending on that I'm rendering my article differently. But not taking this into account, I'm interested though, how would you check if the module is loaded later in the life cycle?
    – demi
    Apr 11, 2018 at 11:21
  • If you don’t care about access levels then it should be pretty straight forward to just match the itemId to the module_menu join table. Apr 11, 2018 at 11:31

1 Answer 1

3

After spending some time on checking this within the article component, I just gave up and took another approach.

Now I'm actually checking if the module is loaded, within the index.php file.

Here is the snippet that at the end I used to make this work:

$app = JFactory::getApplication();
$menu = $app->getMenu()->getActive();
$mod_loaded = false;
$_mods = JModuleHelper::getModules('mymoduleposition');
foreach ($_mods as $key => $mod) {
    foreach ($mod->advancedparams->conditions["menuitems_selection"] as $_key => $_menu_item) {
        if ($_menu_item == $menu->id) {
            $mod_loaded = true;
        }
    }
}

I load modules by position using the JModuleHelper::getModules('position_name') method, and then check if any modules in that position have an assigned menu item with the same ID as the current menu item ID.

Note that that this works from the template's index.php and might cause problems somewhere within the article component or other components.

Hope it helps those who have similar issues!

2
  • I think it is worthy mentioning that using two foreach() loops and both use the same variable $key is not a great idea because it will cause conflict with the intended value. The good news is, you don't use $key at all -- so the best thing to do would be to edit your answer to remove the $key => syntax entirely.
    – mickmackusa
    May 6, 2021 at 3:55
  • You now have enough rep to vote in the current moderator election. Please exercise your new democratic privilege.
    – mickmackusa
    May 6, 2021 at 3:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.