4

How does one prevent a module from being displayed in the assigned position if no output is rendered? Why publish an empty module? Well, it's not always empty, but if particular conditions are not met, then there is no output to display, yet the empty position it's published in is still there.

I've googled it, but have found only examples from v1.5, and they're all hacks or modifying templates. Has this been addressed/fixed in v3+, and if so, how do I accomplish it.

3
  • 1
    You could install Advanced Module Manager and try the 'hide if empty' option explained here nonumber.nl/extensions/advancedmodulemanager/…
    – jonboy
    Commented Aug 26, 2014 at 15:32
  • @johnny_s - Thx for that...that suggests (as my googling did) that it is still a function of the template making the determination. I'd rather not depend on the template if possible.
    – GDP
    Commented Aug 26, 2014 at 15:37
  • The "Hide if empty" option in Advanced Module Manager works on most templates (in my experience), and is not template dependent. But as you say, apparently it doesn't work on themes based on the Warp Framework (yootheme.com/support/question/12981).
    – johanpw
    Commented Aug 26, 2014 at 15:44

4 Answers 4

5

If the module does not return any output (return false), nothing is going to be generated, not even the Module Style / chrome (ref: JDocumentRendererModules works).

2
  • So this would mean that you have to rely on the module to return false instead of just plowing forward and printing nothing or empty tags for the system to not render it? Commented Aug 27, 2014 at 15:30
  • 1
    Yes, the module renderer has no other way to know. An empty output '' is a valid output.
    – Anibal
    Commented Aug 28, 2014 at 13:18
2

I check if the content from any module from the given module position is empty or not. Combined it with $this->countModules(): the module position is just displayed when there are modules and the content is not empty.

function modulesHaveContent( $position ) {  
    $modules = JModuleHelper::getModules( $position );

    foreach($modules as $module) {
        if (!empty($module->content)) {
            $moduleHasContent = true;
        }
    }

    return $moduleHasContent;
}
2
  • What is the code for combining it with: $this->countModules()?
    – iamrobert
    Commented Jan 29, 2019 at 17:40
  • 1
    if ($this->countModules( 'your-module-position' ) && modulesHaveContent( 'your-module-position' )) { /* The code you want */ }
    – RedNails
    Commented Jan 31, 2019 at 10:02
1

Ultimately (and as it should be), Joomla leaves the presentation of output to the template. They generally seem to use $module->count to make that determination, and without cooperation of the template to examine the output, it makes perfect sense that the empty block appears.

I did find a solution though - In my case the pages having conditional module output are all com_content articles, so I wrote a content plugin that retrieved the module output. If the module returned anything to display, I altered the article output with it. Using CSS, I was able to position the module output the way I needed it to appear, and accomplished what I was trying to do.

-1

You just need to check in the template if the module is published before rendering it's wrapping elements, e.g:

<?php if($this->countModules('breadcrumbs')) : ?>
      <div class="breadcrumb-row">
          <div class="wrapper">
             <section class="content">
                 <jdoc:include type="modules" name="breadcrumbs" style="gangnam" />
              </section>
          </div>
      </div>
  <?php endif; ?>

In this example, if there is no module published to 'breadcrumbs' then the included .breadcrumb-row and contents will not be output.

4
  • This only considers if the module is published or not. The question is how to determine if the module will render any HTML to display.
    – GDP
    Commented Aug 26, 2014 at 16:58
  • Whether a module which renders with no content is displayed or not is module-specific, it's template chrome will always be rendered if the module is published. I can't imagine there's any catch-all, code-based, solution to that. Don't publish modules that don't render content is the best answer, but that necessitates the intervention of a functioning human brain. Commented Aug 26, 2014 at 17:13
  • My human brain is perfectly functional. I clearly stated that there is no output "if particular conditions are not met", and have found many posts with the same question, and wondered if there had been any development to address it.
    – GDP
    Commented Aug 26, 2014 at 17:50
  • Sorry, I didn't mean to appear rude. Ultimately it's dependant on the module in question if it renders an output when there is no content. Of course, you still also need to consider that the template may, or may not, render containing elements around the module whether it outputs anything or not. Commented Aug 26, 2014 at 19:31

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.