Creating a plugin for Joomla 1.5

    I encountered (and continue to encounter) the problem of creating extensions for Joomla 1.5 when transferring one site from version 1.0 to version 1.5 and was surprised to find that branch 1.5 was poorly documented. The API itself, which is now called the Joomla Framework, is more or less normally documented, but there are no sane tutorials even on the official wiki, so I had to use the scarce information gathered from blogs on the English-speaking Internet, to understand Jumla’s code and the code of its demo examples . Given this fact, as well as the fact of the almost complete absence of any useful information on the development of Jumla 1.5 in the Russian-speaking Internet, I decided on this modest article.

    Here I will describe my experience in creating a simple plug-in and bringing it to working condition, therefore, if some of my decisions seem incorrect or suboptimal to someone, please let me know, because otherwise, getting the “right” information on this issue is extremely difficult.



    Formulation of the problem



    It is required to create a “native” plugin that would allow you to insert the contents of a category in the form of a list of links to articles of this category in the right place. Obviously, in Joomla terminology, it is a content plugin. Joomla 1.5 allows you to connect extensions written in the old way (as is customary in the 1.0 branch) in compatibility mode (legacy), for this you need to publish the Legacy system plugin and enable the Compatibility Mode (Legacy Mode). Nevertheless, it was decided to do everything “in a new way”, i.e. in the "native" mode. We will call the plugin ItemList. To use the plugin, you need to specify a special tag in the right place in the contents of the article in the form {itemlist: category id} .

    Implementation



    To create your plugin, you need to create a class inherited from JPlugin and implement the required functionality in it. For the plugin to function successfully in the Joomla environment, some methods with predefined names must be implemented in the plugin class — these are the methods Joomla will call in the process. For content plugins (according to an article by Andrew Eddie), 5 methods are predefined:
    • onPrepareContent
    • onAfterDisplayTitle
    • onBeforeDisplayContent
    • onBeforeContentSave
    • onAfterContentSave

    To create our simplest plugin, we need only the first of the methods, i.e. onPrepareContent , which has three parameters:
    • & $ article - link to the article object
    • & $ params - link to the article parameters object
    • $ limitstart - page number (as I understand it, it is used for pagination)

    The body of this method will actually implement all the functionality of our plugin, for which it is necessary:
    1. Find the tag for the plugin in the body of the article,
    2. Calculate from it the id of the category whose contents you want to display,
    3. Replacing the plugin tag with a list of links to category articles.


    Our plugin consists of only two files.

    itemlist.php


        defined('_JEXEC') or die('Доступ запрещен.');
       
        jimport( 'joomla.plugin.plugin' );
       
        class plgContentItemList extends JPlugin {
          function plgContentItemList(&$subject, $params) {
            parent::__construct($subject, $params);
            $this->_plugin = JPluginHelper::getPlugin('content', 'itemlist');
          }
       
          function onPrepareContent(&$article, &$params, $limitstart) {
            $content = $article->text;
       
            if(preg_match_all("/{itemlist:.+?}/", $content, $matches, PREG_PATTERN_ORDER) > 0) {
              foreach ($matches as $match) {
                foreach ($match as $m) {
                  $catid = intval(str_replace('}', '', str_replace('{itemlist:', '', $m)));
                  $query = 'SELECT id, title FROM #__content WHERE catid = ' . $catid . ' AND state=1';
       
                  $database = JFactory::getDBO();
                  $database->setQuery($query);
                  $items = $database->loadObjectList();
       
                  $output = '
      ';
                    for($i = 0; $i < count($items); $i++) {
                      $link = 'index.php?option=com_content&task=view&id='. $items[$i]->id;
                      $output .= '
    • ' . $items[$i]->title . '
    • ';
                    }
                    $output .= '
    ';
       
                  $content = preg_replace('/{itemlist:.+?}/', $output, $content, 1);
                }
              }
            }
       
            $article->text = $content;
            return true;
          }
        }
        ?>



    This file implements all the functionality of our plugin. The plgContentItemList class has a constructor, which, for compatibility with PHP4, is implemented in the old style. Initialization is reduced only to the fact that you need to download the information about the plugin that made by calling getPlugin class JPluginHelper . The onPrepareContent method implements the steps described above using regular expressions.

    To install the plugin in Joomla, we also need an xml- file with a description of the plugin.

    itemlist.xml



       
          Content - ItemList
         
         
          Copyright (C) 2009. All rights reserved.
          www.gnu.org/licenses/gpl-2.0.html GNU/GPL
         
         
          1.0
         
            A category items list plugin.


            Usage: {itemlist:category_id}
         

         
            itemlist.php
         

       



    Now we zip these two files into one archive, install the plug-in through the Joomla admin panel and do not forget to publish it. To view the results of his work in the body of the article, use the tag {itemlist: category_id} .

    Also popular now: