10. How to display Joomla modules conditionally using MetaMod
Sometimes, it might be useful for you to be able to display Joomla modules only when certain conditions are met. By default, articles can be published in Joomla for a specific range of dates, but that's the limit to what you can do
MetaMod is an extension that brings that same functionality -- and more -- to modules. It includes other Joomla modules, so it will work transparently with any type of module
As with all extensions we'll be showing you, start by going to extensions.joomla.org. Then, navigate to the Access & Security / Frontend Access Control category and find the MetaMod page
1) Click Download
You'll be taken to the MetaMod site
2) Click download
3) Save the file to your computer
4) Now, install the module as you would any other extension
For assistance with finding or installing extensions, watch the first two tutorials in this series
5) Once the module has been successfully installed, go to Extensions
6) Click Module Manager
Scroll right
7) Click New
Scroll down
8) Click MetaMod
MetaMod is a pretty versatile extension, with many conditions that can be used to determine whether or not it will display a particular module
We'll go over everything briefly. If you ever question the purpose of a setting, consult the detailed documentation that can be found on the extension's website and on each option's hover description
For this module, we're going to be recreating our User Menu so that it contains some admin links, too. Ordinarily, the links would show up to all registered users; with MetaMod, we'll be able to allow only admins to see them
9) Type a title for this module
10) Set the Access Level to Registered
11) Since this module is acting as a main menu module, type _menu here to give the module the curved edges main menus typically have
There are two ways to include modules with MetaMod: by using "quick" mode and by manually typing PHP code. Unless explicitly stated, all settings you enter will apply to both quick module includes as well as regular module includes
If you want a module to only show up for a certain date range, that can be specified here
Scroll down
Here, you can choose to whom the quick modules you'll specify below will be displayed
You can also allow or disallow people from specific countries or other geographical areas to see the modules
This requires that you download and install one of three databases, as described here
Scroll down
The final condition allows you to display the modules only to people whose preferred language is in the list
Scroll down
This list is important; it specifes the IDs of all enabled modules. You'll need them when specifying the modules you want to include in the fields that follow
With Quick module id include, you can specify a list of modules to include after applying all of the conditions you've entered above. Quick modules will be included before any you manually enter below
When listing quick modules, you should separate each ID by a comma
Scroll down
Here is where you manually enter PHP code to choose which modules to display. All conditions above will apply in addition to any you add here
For a list of examples and a list of the PHP variables you can use, scroll down
Read over the description and examples carefully
Scroll down
Here is a list of all the PHP variables that can be accessed in the box above. Their only purpose is to further extend what sort of conditions you can apply
Again, the documentation is quite clear, so read over it carefully if you're thinking about using any of these more advanced conditions
Scroll down
...the list continues
Scroll back up
We're going to be including the existing User Menu without any further conditions
12) Type its ID here
13) Next, use code similar to the examples below to include any additional modules
Note that a return statement will end PHP execution, so it's not possible to have multiple return statements in your code. If you want to specify multiple module IDs, you must do so using a comma separated list or a PHP array
In this case, we'll be including the Admin Menu for only Administrators (and Super Administrators)
14) Clear the sample code
15) Type the new code
The variable $user->usertype is described below
Be sure to use double equal signs as shown here and in the examples below
| This means "or" in most programming languages. The pipe symbol is used, and can typically be typed by pressing SHIFT and backslash
This is the ID for Admin Menu, as listed up here
Scroll up
16) For this MetaMod module, we'll be changing the style for the included modules to Naked. This will prevent each individual module from displaying its title
Scroll up
17) Click Save
The module has been created!
Now we need to prevent the included modules from showing up anywhere but inside the MetaMod
18) Scroll down and find the old User Menu
Scroll down
19) Under Menu Assignment, click None
Scroll back up to the top
20) Click Save
21) Repeat the process with the Admin Menu, then scroll down and find the new User Menu we just made with MetaMod
22) Change the order to bring the module up below the Main Menu
Scroll up
23) Click the save icon
Finally, it's time to check out our new User Menu
24) Click Preview
Scroll down
As you can see, the menu doesn't even show up for unregistered users
25) Log in as an administrator
The menu looks correct for an admin. Let's verify that a normal Registered user can't see the admin links
It worked! We've successfully combined the two menus, allowing only admins to see the admin links
This is the end of the tutorial. You now know how to display modules conditionally using MetaMod