More flexible module positions
Written by Thomas Kahl   
Monday, 24 January 2011 06:17

We have often heard or read that is is not possible to display modules in Joomla depending on the usergroup or other dynamic values. Sure, you can set a module to "registered" to show it only when a user is logged in - but a public module is still visible when a user is logged in.

But this is only half true. There is a simple way to make the module positions far more flexible and it works with a lot of extensions:

Create module positions in the template with dynamic names.

This is easier than it sounds. You might know how the modules are loaded in the template:

<jdoc:include type="modules" name="user4" />

If you haven't seen this before, check your templates index.php. You will find some of these "tags" in the code (if your template is built upon one of these template frameworks, the modules can be loaded somewhere else, but this is the normal way to load them).

It is no problem to insert a dynamic name-parameter. For example:

<jdoc:include type="modules" name="user_<?php echo $my_parameter;?>" />

If the value of $my_parameter is for example "unregistered", the name of the module position in this example is "user_unregistered". Now you can create modules, that are only shown to not logged in users by adding this code:

<?php
$my_user=JFactory::getUser();
$my_parameter=$my_user->id ? 'registered' : 'unregistered';
?>

This will set the variable to "registered" when the user is logged in and "unregistered" if not. This will lead to two possible module positions in our example: "user_registered" and "user_unregistered".

OK, you can say that there is a cool extension called "Advanced ModuleManager" from Nonumber.nl. Yes, it gives you several options to do things like this. You can also add PHP code to select the page where the module is active. But you have to do this each time you create a module - and this is not very easy for people who are not familiar with PHP-code. "Our" solution can be used by the normal shop owner who has no knowledge of PHP and other technical stuff. He / she just has to know the naming convention of the module position.

Some more examples for Virtuemart:

  • Show modules on special product browse-pages (categories)
    $my_parameter='vmcat'.JRequest::getInt(category_id);
  • Show modules for different Shoppergroups:
    $my_parameter='shoppergroup'.$_SESSION['auth']['shopper_group_id'];
  • Show modules on special Virtuemart pages (like the cart):
    $my_parameter=JRequest::getVar('page');

These are of course very simple (but powerful) examples. I'm sure that you can think of more complex situations. This technique can be used with nearly all components - it only has to be possible to read the value you want within the template file.

Make sure that you create a unique value - as you can see above, we added a string to the shoppergroup-example. Otherwise, there would only be the ID as module name extension. This could lead to a conflict if you use this for different things.

Of course, this also works with other module related template functions like "countModules" (this function returns the number of published modules assigned to a special position). Just use the function like this:

$this->countModules('user_'.$my_parameter)

I hope this could help you creating a more dynamic module structure on your site.



Trackback(0)
Comments (0)Add Comment

Write comment
smaller | bigger

security code
Write the displayed characters


busy
 

Blog Newsletter

Blog RSS-Feed Click here

Subscribe to our Blog Newsletter

Enter your email address:

Delivered by FeedBurner