0

I have a split navigation module defined in my custom template. It is set to display submenu items under the main navigation when the parent item is active. The module itself displays correctly, but if I perform a check to define a CSS class or display sample HTML, the if statement does not work correctly.

My template module code and parameters are included below.

Template Code (index.php)

<?php if($this->countModules('global-nested-navigation')): ?>
    <!-- Sample HTML -->Yolo!<!-- / Sample HTML -->
    <jdoc:include type="modules" name="global-nested-navigation" />
<?php endif; ?>

The nested navigation module is not displayed until the parent menu item is active, but the sample HTML is displayed on every page. I have included two screenshots showing the sample text, one for the homepage and the other with parent menu item active.

A screenshot of the nested navigation parameters in also included below.

Here are the top level navigation parameters:

  • "Base Item" set to Current
  • "Start Level" and "End Level" to 1
  • "Show Sub-menu Items" set to None

Any ideas?

Homepage with sample html

Active parent menu item with nested navigation

Nested navigation module parameters

Homepage, no nested nav rendered

Active parent menu item with nested nav rendered

2 Answers 2

1

Your module is always published. It counts even if it has no content. A feature to exclude empty modules when counting modules in template may get included in 4.0.

5
  • Yep. If the module is published, but empty, the count will include it and the countModules test will be true. It looks like that is the case here, so if it isn't @mike-hermary can you add enough detail to show us?
    – Arlen
    Commented Feb 26, 2020 at 14:04
  • The module is published, but should only display what is between the if statement when the parent menu item is active. I have updated my question showing that the non-nested navigation pages do not have the module included on them using tp=1. Commented Feb 26, 2020 at 16:44
  • I'm sorry, but "when the parent menu item is active" is hard for me to parse in this context. If the module is assigned to a menu item, it doesn't matter what the state of any particular menu item is, countModules will be true. That information is on the "Menu Assignment" tab of the module. If it's set to "on all pages" then countModules will always be true.
    – Arlen
    Commented Feb 26, 2020 at 19:33
  • @Arlen That makes sense. I just assumed that if no module position was displayed using tp=1, there would be no output. Commented Feb 27, 2020 at 1:12
  • It's a little tedious, but if you just want a menu module to be displayed in a location only when the current page is in a certain part of a menu tree, you can use the menu assignment tab to assign it only to those pages. Then it won't show up as available for the other pages, and thus will not be seen by countModules(). It's simple to do this, but tedious to maintain, but it should work.
    – Arlen
    Commented Feb 27, 2020 at 14:43
1

Usually i get around these kind of problems with a small custom PHP function. You can include it in you template index.php

function thereAreModules ($position) {
  $modules = JModuleHelper::getModules($position);
  $ret = false;
  if (count($modules) > 0) {
    $ret = true;
  }
  return $ret;
}

Usage:

if (thereAreModules('module-position-name-here')) {
  // do your stuff
}
2
  • 1
    Why not more simply: function thereAreModules($position) { return (bool)count(JModuleHelper::getModules($position)); } ? Commented Feb 27, 2020 at 21:23
  • @mickmackusa you're right of course. much more elegant and compact.
    – herrmarek
    Commented Feb 28, 2020 at 12:16

Your Answer

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

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