Joomla PHP: extending config.xml with plugins

Sunday, 10 May 2009
This explains how to use Joomla 1.5  PHP API in order to render and save component parameters from multiple files. This way a developer can etend the parameters to allow, for example, component plugins

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.



Give us some social love (it really works now)!

Reddit! Del.icio.us! StumbleUpon! Yahoo! Swik!



Comments (3)
RSS comments

Thanks, Just what I was looking for

2. Great!20-05-2009 13:15

Great tutorial! i've always wondered how to do this. Thanx

3. Works Great19-05-2009 12:47

Hi, 
 
thanks for this, I was biggining to think I couldn't do this until I found your solution. 
 
I'm doing this for a module rather than a component but it works a treat. I had to get the module params by hand: 
 
 
$db =& JFactory::getDBO(); 
$id = JRequest::getVar( 'id', 0, '', '' ); 
$cid = JRequest::getVar( 'cid', array($id), 'method', 'array' ); 
JArrayHelper::toInteger($cid, array(0)); 
 
$query = 'SELECT params FROM #__modules WHERE id='. $cid[0]; 
$db->setQuery($query); 
$params = $db->loadResult(); 
 
 
I also modified your code to remove the table tags from the params->render() output so you don't need the trick to get a full width table row: 
 
 
$ret.='Plugin Parameters'. $title . ''; 
$params_html = $myparams->render(); 
$params_html = substr($params_html, 0, -8)
$params_html = substr($params_html, 65); 
$ret.=$params_html; 
 
Cheers and thanks again, 
 
Alan.

Write Comment
  • Please keep the topic of messages relevant to the subject of the article.
  • Please don't use comments to plug your web site. Links are rel='nofollow'-ed
  • Please refresh the page if you're having trouble with the security image code
Name:
E-mail
Homepage
Title:
Comment:

:) :grin ;) 8) :p
:roll :eek :upset :zzz :sigh
:? :cry :( :x
Code:* Code

Last Updated ( Thursday, 18 June 2009 )
 
Post and Bid on Joomla/PHP projects! Join EUFreelance.com for free!

Europe freelancer directory

Newsletter

Subscribe to TeachMeJoomla's newsletter
Name:
Email:


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