1

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!

  • Modules are loaded after the component, hence I would suggest you try checking for modules later in the life cycle. – jamesgarrett Apr 11 '18 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 '18 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. – jamesgarrett Apr 11 '18 at 11:31
1

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!

  • If you are happy with this solution, please award your solution the green tick. If you would be happy to receive a better solution (I don't have one for you) then perhaps don't award the green tick and volunteers may be more likely to visit this page hoping to help. – mickmackusa May 26 '18 at 22:16

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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