Joomla! Module Tutorial – Article Intro

Displaying the Current Article’s Intro Text in a Module

Perhaps you would like to have a module which displays the current article’s introductory text somewhere on the page, like you see at the top of this article’s page.

 

Everything you type ahead of the “Read more…” line in an article is stored in jos_content.introtext, and everything you type after that line is stored in jos_content.fulltext.

 

A module can determine what article is being displayed by using

$article_id = JRequest::getVar(‘id’,”);

That’s about all we need to know to get our module going.

A module, following the proper Joomla! MVC architecture, in its very basic form consists of:

  • an xml file defining its parameters and listing its required files
  • a php file to actually do the work and prepare the content
  • a template file to format the display of the content

We will start with our xml file: mod_articleintro.xml.

 <?xml version=”1.0″ encoding=”utf-8″?>
<install type=”module” version=”1.5.0″>
    <name>Article Intro</name>
    <author>nodwell.net</author>
    <creationDate>March 2009</creationDate>
    <copyright>Copyright (C) 2008 – 2010 nodwell.net. All rights reserved.</copyright>
    <license>http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL</license>
    <authorEmail>joomla@nodwell.net</authorEmail>
    <authorUrl>www.nodwell.net</authorUrl>
    <version>1.5.0</version>
    <description>If you want to over-ride the style of output, copy the mod_articlintro/tmpl/default.php to /templates/your_template/html/mod_articleintro/default.php and make changes there</description>
    <files>
        <filename module=”mod_articleintro”>mod_articleintro.php</filename>
        <filename>tmpl/default.php</filename>
        <filename>tmpl/index.html</filename>
        <filename>helper.php</filename>
        <filename>index.html</filename>
    </files>
    <params>
        <param name=”moduleclass_sfx” type=”text” default=”” label=”Module Class Suffix” description=”PARAMMODULECLASSSUFFIX” />        
        <param name=”introlength” type=”text” default=”250″ label=”Intro Text Length” />

    </params>
    <params group=”advanced”>
        <param name=”cache” type=”list” default=”0″ label=”Caching” description=”Select whether to cache the content of this module”>
            <option value=”1″>Use global</option>
            <option value=”0″>No caching</option>
        </param>
        <param name=”cache_time” type=”text” default=”900″ label=”Cache Time” description=”The time before the module is recached” />
    </params>
</install>

 Here we define the name of the module, list the files included in the module distribution .zip to be installed, and define the module parameters. We want to let the admin decide how many characters to display of the intro text and choose a module styling for the module itself. The advanced parameter lets the admin decide whether to cache the module’s output or not. We’ve set the default to no cacheing so that if cache is turned on in the site, the module will be sure to get the current article and not the current article from cache_time_ago.

Now we need a php file to do tell the module what to do: mod_articleintro.php.

<?php
/**
* @version        $Id: mod_articleintro.php nodwell.net $
* @package        Joomla
* @copyright    Copyright (C) 2005 – 2008 Open Source Matters. All rights reserved.
* @license        GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

// no direct access
defined(‘_JEXEC’) or die(‘Restricted access’);

// Include the syndicate functions only once
require_once (dirname(__FILE__).DS.’helper.php’);

$text = modArticleIntroHelper::getText($params);
require(JModuleHelper::getLayoutPath(‘mod_articleintro’));

This file does two things. It tells the module to use the helper class (we’ll create that next) to process the module parameters, and it calls the module template. It will use the template we have written and included with the module unless an over-ride is provided in the site template in use on this page.

Now we need to make our helper file: helper.php.

<?php
/**
* @version        $Id: helper.php nodwell.net $
* @package        Joomla
* @copyright    Copyright (C) 2005 – 2008 Open Source Matters. All rights reserved.
* @license        GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

// no direct access
defined(‘_JEXEC’) or die(‘Restricted access’);

require_once (JPATH_SITE.DS.’components’.DS.’com_content’.DS.’helpers’.DS.’route.php’);

class modArticleIntroHelper
{
    function getText(&$params)
    {
        global $mainframe;

        $db            =& JFactory::getDBO();
        $user        =& JFactory::getUser();
        $userId        = (int) $user->get(‘id’);

        $introlength        = (int) $params->get(‘introlength’, 250);

        $aid        = $user->get(‘aid’, 0);
        $article_id = JRequest::getVar(‘id’,”);

        $contentConfig = &JComponentHelper::getParams( ‘com_content’ );
        $access        = !$contentConfig->get(‘shownoauth’);

        $nullDate    = $db->getNullDate();

        // Content Items only
        $query = ‘SELECT * FROM #__content WHERE  id = ‘ . $article_id;

        $db->setQuery($query, 0, $count);
        $row = $db->loadObject();

        $text    = array();
        $text[0]->link = JRoute::_(ContentHelperRoute::getArticleRoute($row->slug, $row->catslug, $row->sectionid));
        $text[0]->text = htmlspecialchars( $row->title );
        if (strlen($row->introtext) > $introlength) { // trim it down
            $introtext = substr($row->introtext,0,$introlength) . ” . . . “;
        } else {
            $introtext = $row->introtext;
        }
        $text[0i]->intro =$introtext;

        return $text;

    }
}

This file will return the article’s text (and title if we want to use it for anything later).

We’re nearly finished, now. We just need a file to handle how the module is drawn on the page: /tmpl/default.php

<?php // no direct access

defined(‘_JEXEC’) or die(‘Restricted access’);
?>
<div id=”article_intro_mod”>
<!– create a style for this div in your template’s css or over ride this file in your template’s /html/mod_articleintro/default.php –>
<?php
echo $text[0]->intro;

?>
</div>

This file writes out our styled intro text into a div that can be styled in the template’s css file.

 

If you are registered and logged in, you can download this module.

 

Leave a Reply

Your email address will not be published. Required fields are marked *