Joomla PHP: extending config.xml with plugins |
Sunday, 10 May 2009 | ||||
The scenario is as follows: I developed a Joomla 1.5 component(we'll call it com_example), and I saw there is a new toolbar button, called Parameters, which displays an interface for global component configuration.The interface consists of a modal overlay, which renders the parameters from administrator/components/com_example/config.xml. As simple call to JToolBarHelper displays the parameter button in joomla component toolbar. Here's a minimal view.html.php to achieve this:
<?php // no direct access defined( '_JEXEC' ) or die( 'Restricted access' ); jimport('joomla.application.component.view'); class mainViewMain extends JView { function display($tpl=null) { global $mainframe; JToolBarHelper::preferences( 'com_example', '600','600' ); parent::display($tpl); } }
Now, when accessing administrator/index.php&option=com_example&view=main, the parameters button should be there. Next, we're taking care of a config.xml <?xml version="1.0" encoding="utf-8"?> <config> <params addpath="/administrator/components/com_example/elements"> <param name="" type="group" default="PAYMENT CONFIG" /> <param name="" type="payment" /> </params> </config>
The interesting bit about this config file is that it's telling Joomla to load our own renderer classes from the /elements folder in component admin files. We have two new paramater types, group and payment. The files in the 'elements' folder should be named exactly as the paramater types, so Joomla will autoload them. Here's /administrator/components/com_example/elements/group.php, which we use for cosmetic purposes: <?php defined('JPATH_BASE') or die(); class JElementGroup extends JElement { var $_name = 'MenuItems'; function fetchElement($name, $value, &$node, $control_name) { $str=(string) $node->attributes('default'); $ret='<h3>'.JText::_($str).'</h3>'; return $ret; } } This will simply render our default attribute as a h3 subtitle so we can visually distinguish the 'group'. The payment element lods several xml files and renders them. The advantage is that upon building a pluggable architecture for a component, you can make it load plugin config files and config the plugin params together with the component's params. I used this technique to configure multiple payment methods. Here's my /administrator/components/com_example/elements/payment.php. You will notice the class name, it must be JElement + the capitalized element type, in our case JElementPayment: <?php defined('JPATH_BASE') or die(); class JElementPayment extends JElement { var $_name = 'MenuItems'; function fetchElement($name, $value, &$node, $control_name) { // trick jomla into granting us the full row width $ret='</td></tr><tr><td colspan="2">'; $adminpath=JPATH_ADMINISTRATOR.DS.'components'.DS.'com_example'; $paymentpath=$adminpath.DS.'lib'; $files=JFolder::files($paymentpath,'^payment\.(.*)?\.xml',false,true); // $ret.=serialize($files); $component = JComponentHelper::getComponent( 'com_example' ); if(count($files)){ foreach ($files as $key => $path) { $myparams = new JParameter($component->params,$path); // determine the payment method title $res=preg_match('/payment\.(.*?)\.xml$/ui',$path,$matches); $title=$matches[1]; // $ret.=serialize($myparams); $ret.="<h3>" . $title . "</h3>"; $ret.=$myparams->render(); $myparams=null; } } ob_start(); ?> <input type="hidden" name="params[payment_settings]" value="foo" /> <?php $ret.=ob_get_contents(); ob_end_clean(); return $ret; } } This class uses JFolder to lists files in a directory, filtering them trhough a regex to match payment.*.xml This creates a new JParamaters instance $myparams = new JParameter($component->params,$path); ,and this adds the plugin paramaters interface to our display: $ret.=$myparams->render(); There is a namespace problem, since different plugin xml files may have params with the same name. My workaround was to prefix the plugin variables with the plugin's name: Here's the paypal plugin config file(payment.paypal.xml): <?xml version="1.0" encoding="utf-8"?> <config> <params > <param name="paypal_enabled" type="radio" default="1" label="Enabled" description="Enabled" > <option value="1">Yes</option> <option value="0">No</option> </param> <param name="paypal_testmode" type="list" default="1" label="Test(sandbox simulation) mode?" description="PayPal Test Mode" > <option value="1">Yes</option> <option value="0">No</option> </param> <param name="paypal_email" type="text" size="50" label="PayPal email" description="PayPal email" /> <param name="paypal_token" type="text" size="50" label="PayPal Token" description="PayPal Token" /> <param name="paypal_test_email" type="text" size="50" label="PayPal test email" description="PayPal test email" /> <param name="paypal_test_token" type="text" size="50" label="PayPal test Token" description="PayPal test Token" /> </params> </config>
So , our final result is having Joomla's parameter button render several xml files, allowing for easy plugin configuration In the controller, we can read the configured values using code like this: $component = JComponentHelper::getComponent( 'com_example' ); $params = & new JParameter( $component->params ); $email=$params->get('paypal_email'); Thank you for reading .
Are you a Joomla/PHP/Web developer? You may want to check our list of freelance jobs websites.
Thanks, Just what I was looking for Great tutorial! i've always wondered how to do this. Thanx Hi, Write Comment |
||||
Last Updated ( Thursday, 18 June 2009 ) |
Joomla stuff
Newsletter
Auto tags
joomla config.xml
how to use joomla in php
joomla component config
JToolBarHelper::preferences
Joomla xml plugin
joomla plugin configuration
joomla load xml file
config.xml joomla
joomla access.xml
joomla jparameter
joomla xml config
jelement joomla 1.5
joomla component config xml
extends jelement
joomla component configuration
config file component joomla
joomla plugin parameters
joomla component configuration file
joomla plugin configuration check
How to use php in joomla
joomla plugin configuration file
joomla read config.xml