How Parameters Works in Joomla Module

Module parameters give site administrators a way of configuring your module without digging into PHP code. They also give your module a level of reusability as different model instances can have different settings for the parameters.

Configuring XML for module parameters

Let's add a parameter to the Noida City module now. We are going to open up mod_noidacity.xml and in this file we have a config section that we're going to add the following code to the existing XML file.

  1. <config>
  2. <fields name="params">
  3. <fieldset name="basic">
  4. <field
  5. name="message"
  6. type="text"
  7. default=""
  8. label="Message"
  9. description="Message to display above places list" />
  10. </fieldset>
  11. </fields>
  12. </config>

So you'll notice in this XML we have a config element and then a fields element with a name of params and this tells Joomla that these are the parameters that we want to use to configure the module.

Inside params we have a field set with a name basic. You can also have an advanced section, but for the moment we were just going to keep it at basic and then inside the field set is a field and this allows us to define what the name of the field is going to be, what type it is, whether or not we have a default value and how we want to label and describe the parameter.

So now let's go to the PHP file and we're going to add a piece of code that allows us to get the value of the parameter and display it in the front end.

So open up the mod_noidacity.php file which we have created in earlier tutorial. Now add the following line

  1. <p><?php echo $params->get('message'); ?></p>

So you'll notice here there is already an object that's named params and all we have to do is call the get function and pass in the name of the variable, in this case message, that we want to get an echo out. Params is automatically defined whenever you load a module. So to see this in action we need to go to the backend and adjust the configuration for our module.

Module Manager in Joomla

Module parameters in backend

Go to Extensions>Module Manager and then choose Know More About Noida City from the list. You'll notice on the right here under Basic Options we have Message and if we mouse over you see a description of what message is intended to be. So in this box we are going to type Visit the most famous places in Noida City. Then we are going to click Save & Close. If we go back to the front end and hit Refresh you now see Visit the most famous places in Noida City listed right above the list of all the different places that are in Noida City.

Parameters showing in frontend

Let's also create another instance of this module just to see the difference.

New Button in joomla

Module Type in Joomla

So for instance if we go back here and click New, we can choose Know More About Noida City as the module type.

Instance of Module

We are going to call this Noida City Copy and we're going to Select position-7 from this list. We're going to leave the Status as Published, and then we are going to also leave the Module Assignment as On All Pages. Than we click Save & Close and we go back to the front end and hit Refresh.

Module Instances in frontend

So you'll notice that Noida City Copy does not have the message while Other Module we created does have the message. That's because, we have not set the parameter for Noida City Copy, but we have set it for the original.

So in that way you can reuse the module over and over again with different settings for different situations. Parameters transform your module from hard coded PHP into configurable reusable packages of code. You can use parameters to keep site administrators away from PHP and focused on managing the site.