Modules: How to return null if there are no items

105 views
Skip to first unread message

Jisse Reitsma

unread,
Jun 3, 2014, 2:50:38 AM6/3/14
to joomla-...@googlegroups.com
Hi all,

I just made a pull request https://github.com/joomla/joomla-cms/pull/3705 for the mod_articles_popular module to add an if-empty check before showing any HTML output. This makes sure that the module does not show on the frontend (not even its module title) when the articles array is empty anyway. I took the approach here of adding the if-empty check within the layout template, so that a developer could still add in a "No items" error if wanted.

In the GitHub pull request discussion, @Bakual brought up the question whether the layout template should be used at all (tmpl/default.php) and if it wouldn't be a better idea to add it in the main file instead (mod_articles_popular.php). There is perhaps something to say for both situations.

After the comments of @Bakual and @sovainfo I checked the Joomla! core and found the various core modules to very inconsistent here:

A) Modules that stop output in the layout file:
modules/mod_articles_archive/tmpl/default.php
modules/mod_tags_popular/tmpl/default.php
modules/mod_users_latest/tmpl/default.php

B) Modules that stop output in the main file:
modules/mod_articles_category/mod_articles_category.php
modules/mod_related_items/mod_related_items.php
modules/mod_tags_similar/mod_tags_similar.php
modules/mod_weblinks/mod_weblinks.php

C) Modules that just output HTML even when the list is empty:
modules/mod_articles_categories/tmpl/default.php
modules/mod_articles_latest/tmpl/default.php

The approach under point A is the approach I took. There are quite a bunch of modules of approach B though. And the modules under approach C simply need fixing in my opinion.

We can make this happen by either modifying all modules to be using approach A, approach B or perhaps even an XML-switch to allow the administrator to switch between approach A and B (which sounds overly complex though).

Which approach should we take?

Regards,
Jisse Reitsma

Bakual

unread,
Jun 3, 2014, 4:11:46 AM6/3/14
to joomla-...@googlegroups.com
I agree that C) needs to be fixed and that a switch would be to complex.

I'm actually fine with both A) or B). The more I think about it, the less I think we need to standardise it.
It may depend on the module if it would make sense to show a "no item found" or just hide the module anyway. There may be modules where such a message has some meaning. For example the popular tags module usually should show something anyway. If it doesn't show an item, it's likely an error in the set up and it probably should show a message (instead of just not showing like now).
However the "related article" module has certainly instances where it can't show anything, and then it should not show at all. There is no point in showing the user "there is no related article". It has no value for the user at all. So it's fine to do the check already in the main file.

Actually I'm starting to think that if we do the check in the layout, we should also show a message for "no items found". Maybe make that message optional if needed. If we're not going to show a message anyway, bail out in the main file already.

Sergio Manzi

unread,
Jun 3, 2014, 4:12:20 AM6/3/14
to joomla-...@googlegroups.com
Great! Thank you!

+1 for approach A

Only problem I see is that if the module is the only one present in a flexible layout column and no HTML is generated, then the column could collapse, which is not necessarly what the web designer wished.

A possible solution could be to control the behavior through a "what_if_empty" parameter in the module's xml with the following possible values:
  1. Display title (make this the default if this is the old behavior, for not breaking b/c)
  2. Display title and "Nothing found" message (make this the default if this is the old behavior, for not breaking b/c)
  3. Display empty <div> (possibly with an &nbsp; in it...)
  4. Display nothing
Having that parameter, in 90% of cases an override would not needed.

Regards,
smz
--
You received this message because you are subscribed to the Google Groups "Joomla! CMS Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-cm...@googlegroups.com.
To post to this group, send email to joomla-...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-cms.
For more options, visit https://groups.google.com/d/optout.

Sergio Manzi

unread,
Jun 3, 2014, 4:15:16 AM6/3/14
to joomla-...@googlegroups.com
Thinking better... option 1) is redundant: this is controlled bay another parameter. Sorry!

Bakual

unread,
Jun 3, 2014, 4:41:17 AM6/3/14
to joomla-...@googlegroups.com
As for the options. The way our modules work, you can't display an empty <div> without the title. Showing the title is in the module chrome logic (part of the template). The module itself doesn't have control over that.

The chrome usually checks if the module returns any content and shows the whole thing including title only when there is. See for example the module chrome from the protostar template: https://github.com/joomla/joomla-cms/blob/staging/templates/protostar/html/modules.php#L39

So the options you can have are:
  • Return empty, not showing the whole module at all
  • Return a message instead of the items
  • Return empty div, which results in a blank module. Title will be shown or not the same it would have been when there were items.
I actually think only the first two options really make sense. The third one could be done with a template override if it's really needed for something.

sovainfo

unread,
Jun 3, 2014, 5:35:16 AM6/3/14
to joomla-...@googlegroups.com
As mentioned in the PR consider both A and B valid situations. Don't think it is a matter of choosing one or the other to standardize on one flavour. Think it is a design issue that should be carefully chosen depending on the situation. The objective would be to provide the functionality that is desired and expected but still leave room for exceptional situations.
In this case the layout produced undesired html output for all use cases, I think.

Agree on fixing C. When it is obvious that option A was implemented while option B is more appropriate (or the other way around) swap it. Otherwise leave it as is.

Sergio Manzi

unread,
Jun 3, 2014, 5:59:52 AM6/3/14
to joomla-...@googlegroups.com
I see what you mean... that would require adding another property to the module object. Something like this:
if ($module->content | $module->showempty)
OR...  generate a fake content with an &nbsp;
Not elegant, but it should work...

Sergio Manzi

unread,
Jun 3, 2014, 6:08:07 AM6/3/14
to joomla-...@googlegroups.com
Naaaaaa... the &nbsp; is a very bad idea....

Jisse Reitsma

unread,
Jun 5, 2014, 9:30:32 AM6/5/14
to joomla-...@googlegroups.com
Lol, glad you agree. 

So thumbs up for option C. I still prefer option A over option B personally, because option B makes editing layouts for a "No items" warning completely impossible. Option B would need to be implemented using a new module option (under Advanced or so).

sovainfo

unread,
Jun 5, 2014, 10:07:49 AM6/5/14
to joomla-...@googlegroups.com
Think that modules/mod_articles_categories/tmpl/default.php belongs in category B. Output is controlled by caller. It is rentrant and the condition is complex. template itself is not called when the list is empty.

sovainfo

unread,
Jun 5, 2014, 10:10:02 AM6/5/14
to joomla-...@googlegroups.com
rentrant -> re-entrant, calling itself recursively.
Reply all
Reply to author
Forward
0 new messages