Sometimes there is a need to hide Joomla! modules from registered (non-guest) users. For example, we may have a "please register" banner rendered by a module, which makes it rather pointless to be shown to registered users. Unfortunately, there is no such functionality in Joomla! 1.5.x. Instead of groing old waiting for Joomla! 1.6 to arrive, you can use a viable workaround. 


For this recipe you'll need Jumi, the all-purpose arbitrary code inclusion solution for Joomla!. Jumi allows you, among other things, to easily create modules which run a custom PHP snippet. It's like having the customization power of Drupal on your Joomla! site. The concept of our recipe is to create a Jumi-powered module which will only load our "real" module – in fact, a module position – if and only if the user is a guest (not logged in). If you haven't already done so, please download and install Jumi.

Creating a faux module position

The first step is to create a "faux" module position. I call it like that because it's not a "real" position, i.e. it isn't actually used as-is by your site's template. It's more of a "proxy", used internally in our solution. Creating it is a piece of cake.

Start by creating, or editing, the module you want to make toggleable. In the module editor, there is a combo box labelled "Position". Intead of using the drop down list, type in a name of your liking. In my example I've used faux_example.

If you need multiple modules to be shown/hidden on the same place in the page, depending on wether the user is logged in or not, repeat this process for any number of modules. For instance, you may have a Custom HTML module and a Banner module on top of it to be shown/hidden at once. Remember to use the same Position parameter!

Creating the "toggle" module

The "toggle" module is the one which controls if the modules in the "faux" module position get shown or not. To create this, go to Joomla!'s Module Manager and create a new module of the type Jumi. In the left-hand column, set the following parameters:

  • Show Title: No. We don't want to see a title if the user is logged in, so leave this blank.
  • Position: Choose where you want the module(s) to be shown, e.g. left.

In the right-hand column, inside the "Code written" text area, enter the following code:

$position = 'faux_example';
$user =& JFactory::getUser();
  $document = &JFactory::getDocument();
  $renderer = $document->loadRenderer('modules');
  $contents = '';
  echo $renderer->render($position, array('style'=>'xhtml'));

Just replace the faux_example text with your own faux position's name. Publish the module. Now try viewing the page where this module is placed as a logged out and then as a logged in user. See? It was easy!

No comments