31 August 2010

Joomla: Read custom article parameters from a module

In implementing a web site on Joomla, I came across a common issue: I needed to pass additional parameters with an article into a 3rd-party module. In this case, the module was the Joomla AnythingSlider, a very slick and flexible content slider/carousel.

The initial task of creating the custom parameters wasn't so bad; the first thread in this post covers that. You add the parameter to the advanced section of the file administrator/components/com_content/models/article.xml:

<param name="article_type" type="radio" default="default" 
label="Article Type" description="article type">
<option value="default">Default</option>
<option value="picture">Picture (rotator)</option>
<option value="video">Video (rotator)</option>
However, reading the values in a module is a different story. Here's the syntax to fetch all the parameters from article.xml:
$paramsdefs = JPATH_COMPONENT.DS.'models'.DS.'article.xml';
$myparams = new JParameter( $paramsdata, $paramsdefs );
And here's how a specific custom parameter is read (in this case, it's called article_type):
echo "Article type: ". $myparams->get('article_type');
This is thanks to another reply to the same post on the Joomla forums, but page 2.

A good quested was raised about one of the variables used in the above code: $paramdata. Where was it defined? Well, it's not in the above code, but in the AnythingSlider Joomla extension, mod_anythingslider; you can download it from here.

The mod_anythingslider has a helper.php file, which defines the class ModSlideShow:
* AnythingSlider Helper Classes
* @package Joomla 1.5.0
* @subpackage Modules
* @Copyright Copyright (c)2010 Dnote Software
* @license GNU/GPL, see http://www.gnu.org/copyleft/gpl.html
* mod_anythingslider is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* GNU General Public License for more details.
* Parts of this software are based on AnythingSlider By Chris Coyier: http://css-tricks.com

defined('_JEXEC') or die('Direct Access to this location is not allowed.');

class ModSlideShow
* Returns a list of articles
function getSlideshow(&$params, &$access)
global $mainframe;

$db =& JFactory::getDBO();
$user =& JFactory::getUser();
$aid = $user->get('aid', 0);

$catid = (int) $params->get('catid', 0);
$items = (int) $params->get('items', 0);

$contentConfig = &JComponentHelper::getParams( 'com_content' );
$noauth = !$contentConfig->get('show_noauth');
$date =& JFactory::getDate();
$now = $date->toMySQL();

$nullDate = $db->getNullDate();

// Ordering
switch ($params->get( 'ordering' ))
case 'o_asc':
$ordering = 'a.ordering';
case 'o_dsc':
$ordering = 'a.ordering DESC';
case 'm_dsc':
$ordering = 'a.modified DESC, a.created DESC';
case 'c_dsc':
$ordering = 'a.created DESC';

// query to determine article count
$query = 'SELECT a.*,' .
' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(":", a.id, a.alias) ELSE a.id END as slug,'.
' CASE WHEN CHAR_LENGTH(cc.alias) THEN CONCAT_WS(":", cc.id, cc.alias) ELSE cc.id END as catslug'.
' FROM #__content AS a' .
' INNER JOIN #__categories AS cc ON cc.id = a.catid' .
' INNER JOIN #__sections AS s ON s.id = a.sectionid' .
' WHERE a.state = 1 ' .
($noauth ? ' AND a.access <= ' .(int) $aid. ' AND cc.access <= ' .(int) $aid. ' AND s.access <= ' .(int) $aid : '').
' AND (a.publish_up = '.$db->Quote($nullDate).' OR a.publish_up <= '.$db->Quote($now).' ) ' .
' AND (a.publish_down = '.$db->Quote($nullDate).' OR a.publish_down >= '.$db->Quote($now).' )' .
' AND cc.id = '. (int) $catid .
' AND cc.section = s.id' .
' AND cc.published = 1' .
' AND s.published = 1' .
' ORDER BY '.$ordering ;
$db->setQuery($query, 0, $items);
$rows = $db->loadObjectList();

return $rows;

Then in the mod_anythingslider.php, we define a few more variables:
$params->set('intro_only', 1);
$params->set('hide_author', 1);
$params->set('hide_createdate', 0);
$params->set('hide_modifydate', 1);

// Disable edit ability icon
$access = new stdClass();
$access->canEdit = 0;
$access->canEditOwn = 0;
$access->canPublish = 0;
// Other code here...
$slideShow = ModSlideShow::getSlideshow($params, $access);
Now we bring it all together in the default.php, which generates the HTML for the slider:
Variables used throughout the main foreach loop.
Don't define these in the loop b/c they'll get
created for each article in the carousel.
$paramsdefs = JPATH_COMPONENT.DS.'models'.DS.'article.xml';
$limitstart = JRequest::getVar('limitstart', 0, '', 'int');
$dispatcher =& JDispatcher::getInstance();

Main foreach loop of the carousel component. Runs
once for each article in the slideshow.
foreach ($slideShow as $item){
echo "<li>";
$aparams =& $item->parameters;

$item->text = $item->introtext;
$results = $dispatcher->trigger('onPrepareContent', array (& $item, & $params, $limitstart));

$paramsdata = $item->attribs;
$myparams = new JParameter( $paramsdata, $paramsdefs );
$article_type = $myparams->get('article_type');
// More processing inside the foreach...
Hope this helps those confused by the $paramdata variable.


ashishmeshram said...

What is @paramdata in above case?

write articles said...

I must appreciate you for the information you have shared.I find this information very useful and it has considerably saved my time.thanks:)