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

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.

| improve this answer | |
  • 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 Feb 26 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. – Mike Hermary Feb 26 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 Feb 26 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. – Mike Hermary Feb 27 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 Feb 27 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
}
| improve this answer | |
  • 1
    Why not more simply: function thereAreModules($position) { return (bool)count(JModuleHelper::getModules($position)); } ? – mickmackusa Feb 27 at 21:23
  • @mickmackusa you're right of course. much more elegant and compact. – herrmarek Feb 28 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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