? ? ? Failure

User tests: Successful: Unsuccessful:

avatar Fedik
Fedik
20 Jan 2018

Pull Request for Issue # there somewhere in history, hard to find .

Issue

$this->countModules('foobar') return number of active modules in the position, but do not check whether module has any content, or not (so it will be not visible)

This is one of most annoying things in Joomla! templating to me.
It can happen with 3d modules for display some associated lists (or similar). HtmlDocument::countModules still count this module as "exists" even if associated list is empty, and so module will not be displayed.

The idea, the fix

The idea is to allow to count only the module which actually has a content.
I have added flag $withContentOnly to HtmlDocument::countModules which allow to do it.

And split ModuleHelper::renderModule to 2 part ModuleHelper::renderRawModule (render module content) and ModuleHelper::renderModule (render module Chrome, trigger the events etc). So HtmlDocument::countModules will trigger rendering only for module content ModuleHelper::renderRawModule and count only the modules which actually have the content.

Testing Instructions

It a bit tricky.

First make override to mod_custom, call it empty.php, and remove all content from it. This will imitate the module without content.

Then create 2 mod_custom modules:
"module1" which use override "empty", at position foobar;
"module2" with some random text and without any override, at position foobar;

In the index.php of the template add next code:

var_dump($this->countModules('foobar', true)); // Count only modules which has content
var_dump($this->countModules('foobar')); // Count all modules in selected position

Expected result

int 1 
int 2 

Actual result

int 2
int 2 

Documentation Changes Required

yeap

avatar Fedik Fedik - open - 20 Jan 2018
avatar Fedik Fedik - change - 20 Jan 2018
Status New Pending
avatar joomla-cms-bot joomla-cms-bot - change - 20 Jan 2018
Category Libraries Front End Templates (site)
avatar Fedik Fedik - change - 20 Jan 2018
The description was changed
avatar Fedik Fedik - edited - 20 Jan 2018
avatar Fedik Fedik - change - 20 Jan 2018
Labels Added: ? ?
avatar Fedik Fedik - change - 20 Jan 2018
The description was changed
avatar Fedik Fedik - edited - 20 Jan 2018
avatar brianteeman
brianteeman - comment - 20 Jan 2018

If i remember correctly the problem comes when the module has a title but no content. See #3705

avatar brianteeman
brianteeman - comment - 20 Jan 2018

I'm really not sure about this - I can see the support emails now. "I've created and published the module but it doesn't appear!".

avatar Fedik
Fedik - comment - 20 Jan 2018

I'm really not sure about this - I can see the support emails now. "I've created and published the module but it doesn't appear!".

It happens even without this changes ?
User did not set a position, made incorrect menu assignment, set non existing position etc etc... usual things ?

But, by default I keep an old behavior.

avatar Fedik
Fedik - comment - 20 Jan 2018

ah, I think you missed the idea.

it does not hide the module, does not prevent displaying it.
But it allow template developer to do action (eg display the position) depend if module has a content or not, like this:

<?php if ($this->countModules('bottom-a')) : ?>
	<div class="grid-child container-bottom-a">
		<jdoc:include type="modules" name="bottom-a" style="cardGrey" />
	</div>
<?php endif; ?>

Currently the invisible module (module without content) still will be non visible, but template will render empty wrapper, that often a problem.

<div class="grid-child container-bottom-a"></div>

Current pull request prevent it,
if you do:

<?php if ($this->countModules('bottom-a', true)) : ?>
	<div class="grid-child container-bottom-a">
		<jdoc:include type="modules" name="bottom-a" style="cardGrey" />
	</div>
<?php endif; ?>

And position bottom-a has invisible module, then the wrapper will not be rendered.

avatar brianteeman
brianteeman - comment - 20 Jan 2018

Is the title part of the content?

avatar Fedik
Fedik - comment - 20 Jan 2018

Is the title part of the content?

nope

avatar ggppdk
ggppdk - comment - 20 Jan 2018

Nice idea, it is needed to finally get real module counting

Looking at code changes,

i see that the raw rendered content (without plugin event triggered) of the modules, that is created during module counting
is cached so that it is not re-created when modules content is displayed,

thus no performance penalty or other side-effects because of having the module created twice

but what about the cases that full module content is created by by the event onRenderModule ?
in such a case the module will be considered empty but countModules, but it will not be

Are there such template frameworks that do this ?
such frameworks also have their own "countModules" thing ?

avatar Fedik
Fedik - comment - 20 Jan 2018

but what about the cases that full module content is created by by the event onRenderModule ?

that would be very strange ?
if the module content are created by some plugin, it just not right.

Are there such template frameworks that do this ?

I do not know, I do not use them.
But when I do new site/template I use similar thing, I have helper wich render a modules and count the result.

avatar brianteeman brianteeman - change - 22 Jan 2018
The description was changed
avatar brianteeman brianteeman - edited - 22 Jan 2018
avatar ReLater
ReLater - comment - 24 Feb 2018

Because renderRawModule(...) is public I would love to see this method in Joomla to get rid of annoying workarounds for this issue.

avatar Fedik
Fedik - comment - 4 Aug 2018

Conflict fixed.

I think about rename the events:
onRenderModule => onRenderModuleChrome
onAfterRenderModule => onAfterRenderModuleChrome
that more accurate to what is actually rendered. But it not b.c. of course. Thoughts?

b0a0c79 4 Aug 2018 avatar Fedik c s
avatar dgrammatiko
dgrammatiko - comment - 4 Aug 2018

Thank you @Fedik for removing that eval which is evil ?

avatar laoneo
laoneo - comment - 4 Aug 2018

From a quick look on the code, do I see it right that a module is rendered twice now? It can also happen that a module is executed even when it is not rendered then through an include?

avatar Fedik
Fedik - comment - 4 Aug 2018

do I see it right that a module is rendered twice now?

Nope, you see it wrong ?
Look at renderModule() and renderRawModule(),
Module object has a flag/prperty: contentRendered, that is used to be sure that the module content are rendered only once.

It can also happen that a module is executed even when it is not rendered then through an include?

hm, the rendering process stay the same as was before, it just split to 2 part:

  • raw module rendering - render only the module content
  • chrome style rendering - add any extra Chrome style to the module content, depend from the template
avatar franz-wohlkoenig franz-wohlkoenig - change - 19 Apr 2019
Title
[4.0][RFC] HtmlDocument::countModules improve, allow to count the modules with the content only
[4.0] [RFC] HtmlDocument::countModules improve, allow to count the modules with the content only
avatar franz-wohlkoenig franz-wohlkoenig - change - 19 Apr 2019
Title
[4.0][RFC] HtmlDocument::countModules improve, allow to count the modules with the content only
[4.0] [RFC] HtmlDocument::countModules improve, allow to count the modules with the content only
avatar franz-wohlkoenig franz-wohlkoenig - edited - 19 Apr 2019
avatar sanderpotjer
sanderpotjer - comment - 5 May 2019

@Fedik thanks for the PR, nice fix of the module count. Can you please update the PR to fix the conflicts? Thanks in advance!

avatar Fedik
Fedik - comment - 5 May 2019

@sanderpotjer I will check when will have a more time, the conflict pretty complex

avatar Fedik Fedik - change - 11 May 2019
Labels Added: ?
Removed: J4 Issue ?
331328e 11 May 2019 avatar Fedik phpcs
avatar Fedik Fedik - change - 11 May 2019
Labels Added: ?
avatar Fedik
Fedik - comment - 11 May 2019

conflict fixed :)

avatar roland-d roland-d - change - 31 Aug 2019
Labels Removed: ? ?
avatar Schmidie64 Schmidie64 - test_item - 2 Sep 2019 - Tested successfully
avatar Schmidie64
Schmidie64 - comment - 2 Sep 2019

I have tested this item successfully on 5770b73

works fine for me. I think it's a very usefull patch, especially for template developers.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/19416.

avatar Neppert Neppert - test_item - 19 Oct 2019 - Tested successfully
avatar Neppert
Neppert - comment - 19 Oct 2019

I have tested this item successfully on 5770b73

Counted the Modules correctly


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/19416.

avatar MarLinDT MarLinDT - test_item - 19 Oct 2019 - Tested successfully
avatar MarLinDT
MarLinDT - comment - 19 Oct 2019

I have tested this item successfully on 5770b73

It works fine! Thanks nice function.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/19416.

avatar MastersOfMedia
MastersOfMedia - comment - 19 Oct 2019

Is the title part of the content?

I would say it is

avatar SharkyKZ
SharkyKZ - comment - 19 Oct 2019

@Quy add "Conflicting Files" label please.

avatar Fedik Fedik - change - 20 Oct 2019
Labels Added: Conflicting Files
avatar Fedik
Fedik - comment - 20 Oct 2019

conflict fixed

avatar Fedik Fedik - change - 14 Dec 2019
Labels Added: ?
Removed: Conflicting Files
avatar peteruoi
peteruoi - comment - 17 Feb 2020

Maybe this pr can be considered as beta blocker since it has some bc problems but it seems from all the commenters that this is the right way to solve a problem that was unsolvable for joomla 3.0.
Also i see only succesfull tests so maybe rtc after conficts?

avatar brianteeman
brianteeman - comment - 17 Feb 2020

it does not block anything

avatar Fedik
Fedik - comment - 23 Feb 2020

@wilsonge what do you think about merging this? :)

avatar SharkyKZ SharkyKZ - change - 23 Feb 2020
Status Pending Ready to Commit
avatar SharkyKZ
SharkyKZ - comment - 23 Feb 2020

RTC.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/19416.

avatar wilsonge
wilsonge - comment - 2 Mar 2020

So I really dunno what to do with this. It's probably more expected behaviour. But presumably by definition will break the entire purpose of What Nothing https://extensions.joomla.org/extension/what-nothing/ which entirely exploited this behaviour...

I'm kinda leaning to it's one of these things where if it was new I'd do it this way but at this point it's so expected I'm going to break more websites than I fix by merging it?

avatar Fedik
Fedik - comment - 2 Mar 2020

but at this point it's so expected I'm going to break more websites than I fix by merging it?

hm, why it break? it does not change default behavior, it just extending it ;)
Everyone who use $this->countModules('foobar') will get "old result",
New is $this->countModules('foobar', true) with extra boolean parameter

avatar wilsonge
wilsonge - comment - 2 Mar 2020

?‍♂ totally missed it was a parameter somehow.

avatar wilsonge
wilsonge - comment - 3 Mar 2020

@regularlabs tagging you so you can give some feedback here if you'd like. Otherwise will merge this in the next week or so.

avatar regularlabs
regularlabs - comment - 3 Mar 2020

No feedback needed from me.

avatar wilsonge wilsonge - change - 9 Mar 2020
Status Ready to Commit Fixed in Code Base
Closed_Date 0000-00-00 00:00:00 2020-03-09 11:57:21
Closed_By wilsonge
Labels Added: ?
avatar wilsonge wilsonge - close - 9 Mar 2020
avatar wilsonge wilsonge - merge - 9 Mar 2020
avatar wilsonge
wilsonge - comment - 9 Mar 2020

Thanks!

avatar Fedik
Fedik - comment - 27 Jan 2021

@zero-24 this need to be documented as possible compatibility break for stuff like
$this->countModules( 'user1 or user2' ), that was dropped in J4, because deprecated.

And maybe warning about it at https://docs.joomla.org/Customising_the_way_modules_are_displayed
I tried to edit that page, but not found how to make it ?

Add a Comment

Login with GitHub to post a comment