ThemePartner, Professional Joomla! Business Templates

Theo

Retrieving Plugin, Module, Component and Template Parameters

December 17th, 2010 at 11:48 AM CET in Technical
Update: Joomla! 1.7 version of this blog post is now available! Check it out: here.

Since the release of Joomla 1.5, developers can enable users to configure extensions using parameters. While the possibility of being able to do this is great, it is often a chore to keep track of the code needed to access one of these parameters. Therefor, in this blog I will present an overview of the code needed to access all the different parameters.

Parameters

Image credits: Edwin Young

Plugin parameters from inside a plugin

$param = $this->params->get('paramName', 'defaultValue');

Plugin parameters from outside a plugin

$plugin = &JPluginHelper::getPlugin('exampleType', 'example');
$pluginParams = new JParameter($plugin->params);
$param = $pluginParams->get('paramName', 'defaultValue');

Module parameters from inside a module

$param = $params->get('paramName', 'defaultValue');

Module parameters from outside a module

$module = &JModuleHelper::getModule('example');
$moduleParams = new JParameter($module->params);
$param = $moduleParams->get('paramName', 'defaultValue');

Component parameters from inside a component

$componentParams = &JComponentHelper::getParams('com_example');
$param = $componentParams->get('paramName', 'defaultValue');

Component parameters from outside a component

$componentParams = &JComponentHelper::getParams('com_example');
$param = $componentParams->get('paramName', 'defaultValue');

Template parameters from inside a template

$param = $this->params->get('paramName');

Template parameters from outside a template

jimport('joomla.filesystem.file');
$mainframe = &JFactory::getApplication();
$params = $mainframe->getParams(JFile::read(JURI::root() .'/templates/template_name/params.ini'));
$param = $params->get('paramName', 'defaultValue');

Template parameters from an included file outside the Joomla framework

// Get params.ini relative to the current file location (use your own relative path here)
$paramsFile = dirname(__FILE__) . '/../../params.ini';

// Only continue if the file exists
if(file_exists($paramsFile)) {
    // Get contents from params.ini file
    $iniString = file_get_contents($paramsFile);

    // Escape double quotes in values and then double-quote all values (because Joomla doesn't do that for us..)
    $iniQuoted = preg_replace('/=(.*)\\n/', "=\"$1\"\n", addcslashes($iniString, '"'));

    // Parse the ini string to an associative array
    $iniParsed = parse_ini_string($iniQuoted);
} else {
    $iniParsed = '';
}

// Set params to obtained values or empty array
$params = (!empty($iniParsed)) ? $iniParsed : array();

// Get param value from array
$param = $params['paramName'];
Follow us on Twitter

User Comments (15)

Add comment

guru

February 17th, 2011 at 12:41 PM CET
hi

thanks for very good blog

guru katre

February 17th, 2011 at 12:44 PM CET
there is little mistake

1. $module = &JModuleHelper::getModule('mod_example');
2. $params = new JParameter($module->params);
3. $param = $moduleParams->get('paramName', 'defaultValue');

above code should be like this

1. $module = &JModuleHelper::getModule('mod_example');
2. $params = new JParameter($module->params);
3. $param = $params->get('paramName', 'defaultValue');

Theo

February 17th, 2011 at 12:54 PM CET
Thank you for the compliment and very useful feedback guru!

I've corrected the article to fix the mistake you pointed out in your comment. However, instead of changing line 3, I've changed line 2 to create the variable named $moduleParams to make it clear for future reference what type of params you are accessing.

guru katre

February 22nd, 2011 at 8:20 AM CET
hi Theo

there is one more thing i want to tell u
we don't have to specify "mod_" when we are getting the parameter from module

1. $module = &JModuleHelper::getModule('mod_example');
2. $moduleParams = new JParameter($module->params);
3. $param = $moduleParams->get('paramName', 'defaultValue');


above code should as follows

1. $module = &JModuleHelper::getModule('example');
2. $moduleParams = new JParameter($module->params);
3. $param = $moduleParams->get('paramName', 'defaultValue');


because we already calling function "getModule" so this take care about "mod_"

$module = &JModuleHelper::getModule('example');

Theo

February 22nd, 2011 at 10:20 AM CET
Thanks once again for your comment Guru.

I was aware of the fact that the mod_ isn't required in getting the module parameters. However, just to be 'more than sure' where I'm fetching parameters for (in this case a module), I kept the mod_ in place.

Thanks for pointing out to the readers that this part of the code isn't required!

dpt

May 16th, 2011 at 2:50 PM CEST
Hi Theo,

Do you have a way to fetch Module params from the component in Joomla 1.6?? as JParameter is deprecated in Joomla 1.6 refer the link here "http://docs.joomla.org/What's_new_in_Joomla_1.6#Deprecated_Features"

thnx

Thomas Welton

June 9th, 2011 at 1:30 AM CEST
dpt I saw your comment, then about 30 mins later finally found the answer I too was looking for http://forum.joomla.org/viewtopic.php?f=642&t=612230&p=2522636#p2522636

Kavvalos

June 14th, 2011 at 2:21 AM CEST
Hi, Do you know maybe how to set params? it would be also very useful.

I tried some solution but it was not correct

Jatinder Singh Thind

June 18th, 2011 at 7:53 AM CEST
Hi, any idea on how I can fetch templates parameters from outside a template in Joomla 1.6 ?

ecolora

June 21st, 2011 at 9:43 AM CEST
This code did not work for me:

$module = &JModuleHelper::getModule('mod_modname');

But this code works fine

$module = &JModuleHelper::getModule('modname');

donatello

August 16th, 2011 at 7:42 PM CEST
me too, same as ecolora. spent hours wondering why it wasn't working until i read the comments...maybe should be modified caus it just doesn't work with mod_

Theo

August 16th, 2011 at 7:54 PM CEST
I modified the original blog to reflect the changes mentioned by ecolora and donatello. I do however recall that I tested the original code exhausively, so if

$module = &JModuleHelper::getModule('modname');

isn't working for you, do try

$module = &JModuleHelper::getModule('mod_modname');

as well.

akh

October 18th, 2011 at 5:39 AM CEST
Thanks for your tutorial.

I can't access my plugin with

$plugin =& JPluginHelper::getPlugin('example');

I have to change it to (suppose plugin 'example' is of type 'user')

$plugin =& JPluginHelper::getPlugin('user', 'example');

guru katre

October 19th, 2011 at 8:15 AM CEST
Hi Theo,
akh is right.


we have to write

$plugin =& JPluginHelper::getPlugin($type, $plugin)

Instead of

$plugin =& JPluginHelper::getPlugin('example');

Theo

October 20th, 2011 at 12:09 PM CEST
Just made the change proposed by akh and guru katre, thanks for the suggestion!

Add a comment

*
*
*

*

 

Buy 2 Templates, Get Your Third Free!

Buy 2 Templates, Get Your Third Free!

By popular demand we've introduced a way to purchase multiple templates, at a discounted rate! If you add any three templates to your shopping cart, you'll only have to pay for two!

Blog categories

Conferences

Upcoming conferences

Past conferences

Reports on past conferences