Displaying modules conditionally

502 views
Skip to first unread message

dami

unread,
Jan 26, 2012, 9:03:18 PM1/26/12
to Joomla! General Development
Hi, I'm quite new to Joomla development, but I've readed almost every
piece of documentation that I could find and I think I'm making
progress.

I'm interested to know the best approach for a requirement I have.
On the site I'm working, I need to load a custom module when certain
articles are shown.

The solution I've found was to create a templete style just for the
menu that shows this articles. Then with a config parameter on my
template, I can add the module position conditionally.

Now if I do this, I get the module displayed either on the category
view or the article itself, and I needed the module just for the
articles.
So on the main module file I'm checking the current view and, if it's
a category, I resume the module execution using return:

if(JRequest::getVar('view')=== 'category')
{
return;
};

The code works fine, but I'm not really sure if its the best
solution.

Another approach I thought, was simply create an alternative layout
for the article that loaded the module position, but then, my client
would have to set this alternative layout on every article that he
created for this section.

So my questions are:

* Do you think there is a more simple solution to solve this?

* If not, do you think that using "return" is the cleaner way to
prevent the render of my module?

thanks in advance
dami

Andy Nagai

unread,
Jan 27, 2012, 3:32:25 PM1/27/12
to joomla-de...@googlegroups.com
You assign modules by menu item. This is how you control what module
shows on what page.

> --
> You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
> To post to this group, send an email to joomla-de...@googlegroups.com.
> To unsubscribe from this group, send email to joomla-dev-gene...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.
>

Matt Thomas

unread,
Jan 27, 2012, 3:43:46 PM1/27/12
to joomla-de...@googlegroups.com
Dami,

While Andy is correct, Joomla's approach to controlling the display of modules, based on menu assignment, can be limited in situations like the one you describe. While trying to overcome this limitation myself, I basically came to same conclusion as you. I can't speak as to whether or not there is a better solution that using "return", but I can say that fundamentally I agree with the approach.

Hope that helps!

Best,

Matt Thomas
Founder betweenbrain
Phone: 203.632.9322
Twitter: @betweenbrain

Helder Garcia

unread,
Jan 27, 2012, 3:48:00 PM1/27/12
to joomla-de...@googlegroups.com
If I understand your need, the simplest solution I see is to use in your articles:

{loadposition custom_name}

The custom_name is a name you create. Then in your module configuration you set the position to custom_name. You don't select anything on the drop down list, you just type it on the field.
The plugin Content - Load Module must be enabled.

Mark Dexter

unread,
Jan 27, 2012, 7:44:09 PM1/27/12
to joomla-de...@googlegroups.com
You can also make module positions conditional in the template's index.php. For example, something like:

if(JRequest::getCmd('component' == 'com_content' && JRequest::getCmd('view') == 'article')
{
       <jdoc:include type="modules" name="position-2" />
};

That way, the position only exists when the component and view match. So you can assign the module to all menu items, but it will only show on article layouts. Mark

dami

unread,
Jan 28, 2012, 10:39:29 AM1/28/12
to Joomla! General Development
Thank you all for your replies =)

@Matt: Thanks, is good to know that I'm not getting it too wrong.

@Helder: Yes, I could use the Load Module plugin. But then my client
would have to use the {loadposition} structure on every article. I'm
looking for a more transparent solution because the module isn't
related to the article's content at all. It's more like a layout
widget.

@Mark: That could do the job, but the module would be displayed on
every article on the site, and I want to use it just when some
category's articles are shown. So I would have to check the category
id too. I could set the category id as a parameter on the template
style, so I wouldn't have to hard-code it on the IF statement.

Anyways thanks for all your help.

While I was trying to solve this, I couldn't help by thinking that
would be an interesting feature to be able to set an alternative
layout for articles at category level.

What do you think?

kind regards,
dami

On Jan 27, 9:44 pm, Mark Dexter <dextercow...@gmail.com> wrote:
> You can also make module positions conditional in the template's index.php.
> For example, something like:
>
> if(JRequest::getCmd('component' == 'com_content' &&
> JRequest::getCmd('view') == 'article')
> {
>        <jdoc:include type="modules" name="position-2" />
>
> };
>
> That way, the position only exists when the component and view match. So
> you can assign the module to all menu items, but it will only show on
> article layouts. Mark
>
> On Fri, Jan 27, 2012 at 12:48 PM, Helder Garcia <helder.gar...@webify.com.br
>
>
>
>
>
>
>
> > wrote:
> > If I understand your need, the simplest solution I see is to use in your
> > articles:
>
> > {loadposition custom_name}
>
> > The custom_name is a name you create. Then in your module configuration
> > you set the position to custom_name. You don't select anything on the drop
> > down list, you just type it on the field.
> > The plugin Content - Load Module must be enabled.
>
> > --
> > Helder Garcia
> > helder.gar...@webify.com.br

hector garzo

unread,
Jan 27, 2012, 8:19:17 PM1/27/12
to joomla-de...@googlegroups.com
hello,
is not the best, I make the following, enables a menu (not visible) and then assign thatto the Vizualization menu of categories and after this module go to and say, 'I want or not want to show you when this menu active "

the core joomla so I associated my content to this menu item it can be for a specificitem or categoróa or to anyone, it is the case you're looking for you.

I hope is given to understand, do not write English properly and helped me with a translator jejeje


blessings 



2012/1/27 Helder Garcia <helder...@webify.com.br>



--
Héctor Garzo Fuster.
Ingeniero 



Reply all
Reply to author
Forward
0 new messages