📜 ⬆️ ⬇️

Creating a plugin for Joomla 1.5

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

Here I will describe my experience of creating the simplest plug-in and bringing it to a working state, therefore, if some of my decisions seem to be wrong or non-optimal, 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 to insert the contents of the category in the right place of the article as a list of links to articles of this category. Obviously, in the terminology of Joomla - this is a content plugin. Joomla 1.5 allows you to connect extensions written in the old way (as is customary in branch 1.0) 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. The plugin we call ItemList. To use the plugin, you need to specify a special tag in the form of {itemlist: category id} in the right place of the article content.

Implementation



To create your own plugin, you need to create a class inherited from JPlugin and implement the required functionality in it. For the successful operation of the plugin in the Joomla environment, some methods with predefined names must be implemented in the plugin class - these are exactly the methods that Joomla will call during the work. For content plugins (according to the Andrew Eddie article ) such methods are predefined 5 pieces:

To create our simplest plugin, we need only the first of the methods, i.e. onPrepareContent , which has three parameters:

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


Our plugin consists of only two files.

  itemlist.php 


<?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 = '<ul>' ;
for ( $i = 0 ; $i < count ( $items ) ; $i ++ ) {
$link = 'index.php?option=com_content&task=view&id=' . $items [ $i ] -> id ;
$output .= '<li><a href="' . $link . '">' . $items [ $i ] -> title . '</a></li>' ;
}
$output .= '</ul>' ;

$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 that, for compatibility with PHP4, is implemented in the old style. Initialization is reduced only to the fact that it is necessary to load information about the plugin, which is done by calling the getPlugin method of the JPluginHelper class. 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 


<?xml version = "1.0" encoding = "utf-8" ?>
<install version = "1.5" type = "plugin" group = "content" >
<name > Content - ItemList </name >
<author > </author >
<creationDate > </creationDate >
<copyright > Copyright (C) 2009. All rights reserved. </copyright >
<license > www.gnu.org/licenses/gpl-2.0.html GNU/GPL </license >
<authorEmail > </authorEmail >
<authorUrl > </authorUrl >
<version > 1.0 </version >
<description >
A category items list plugin. <br /> <br />
Usage: {itemlist:category_id}
</description >
<files >
<filename plugin = "itemlist" > itemlist.php </filename >
</files >
</install >



Now we archive the zip-ohm these two files into one archive, install the plugin 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} .

Source: https://habr.com/ru/post/54221/


All Articles