JOOMLA FICTION LAB
(5 votes)

How to load the parameters of a Joomla module inside an external php file

Thursday, 12 April 2012 14:46
With this code you can load the parameters of any Joomla module inside an external php file.

1. Load Joomla Framework


First of all, we must load the Joomla framework inside the external php file:
// Get Joomla! framework
define( '_JEXEC', 1 );
define( '_VALID_MOS', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__)));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
$user =& JFactory::getUser();
$session =& JFactory::getSession();

We assume that the external php file is located in the Joomla root.
If the file is inside another folder, then you must change this line accordingly:
define( 'JPATH_BASE', realpath(dirname(__FILE__)));

For example, if the file is located inside the folder modules/mod_yourmodule/tmpl/ then the above line should be:
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/../../..' ));
(3 levels away from root)

2. Load Module Parameters


Now that we have loaded the framework, we can include the module parameters:

Joomla 1.5

jimport( 'joomla.application.module.helper' );
$module = &JModuleHelper::getModule('mymodulename');
$moduleParams = new JParameter($module->params);
where mymodulename is the name of the module.
For example if the module is called mod_mymodule then the name of the module is mymodule.

Now you can call each parameter:
$param = $moduleParams->get('paramName', 'defaultValue'); 
Joomla 2.5

jimport( 'joomla.application.module.helper' );
$module = JModuleHelper::getModule('mymodulename');
$moduleParams = new JRegistry();
$moduleParams->loadString($module->params);
Now you can call each parameter:
$param = $moduleParams->get('paramName', 'defaultValue');


Did this work out for you?
Let us know in the comments below.




Add comment


Security code
Refresh