Joomla Create Plugin For Custom Component

In this tutorial I will show you how to create a custom plugin for your Joomla extension. This allows you to extend your component to other developers.

For this tutorial I will call my custom extension Blue. Within my component Blue, I would like to trigger an event after title is displayed.

Step 1
Create a folder named berry. Next create a php file and an xml file with the same name as the folder berry.php and berry.xml

--| berry
---| berry.php
---| berry.xml

Step 2
In our berry.xml file we will add the codes to install the plugin to the correct group folder. The following code is taken from the Joomla tutorial for creating a normal plugin.
Change all the fields in the xml to your own except for the group which in our tutorial should be blue.

<?xml version="1.0" encoding="utf-8"?>
<extension version="3.1" type="plugin" group="blue">
	<name>plg_blue_berry</name>
	<author>Joomla! Project</author>
	<creationDate>November 2005</creationDate>
	<copyright>Copyright (C) 2005 - 2013 Open Source Matters. All rights reserved.</copyright>
	<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
	<authorEmail>admin@joomla.org</authorEmail>
	<authorUrl>www.joomla.org</authorUrl>
	<version>3.1.0</version>
	<description>PLG_SEARCH_CATEGORIES_XML_DESCRIPTION</description>
	<files>
		<filename plugin="categories">categories.php</filename>
		<filename>index.html</filename>
	</files>
	<languages>
		<language tag="en-GB">en-GB.plg_search_categories.ini</language>
		<language tag="en-GB">en-GB.plg_search_categories.sys.ini</language>
	</languages>
	<config>
		<fields name="params">
 
			<fieldset name="basic">
				<field name="search_limit" type="text"
					default="50"
					description="JFIELD_PLG_SEARCH_SEARCHLIMIT_DESC"
					label="JFIELD_PLG_SEARCH_SEARCHLIMIT_LABEL"
					size="5"
				/>
 
			</fieldset>
 
		</fields>
	</config>
</extension>

Step 3
Edit the actual plugin file berry.php. In this file you will create actions for each defined plugin hook.
This example, I am using one plugin event onBlueDisplayTitle. Add the following to the berry.php file.

<?php
/*
 * @copyright Copyright (C) 2016 Western Studios
 * @license : Commercial
 * @Website : http://westernstudios.net
 */

// Check to ensure this file is included in Joomla!
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin' );



class plgBlueBerry extends JPlugin
{

public function onBlueDisplayTitle($item){

  return 'This is my new title!';
}// EOF


}

In our plugin event we also have access to the $item so you can use to query database or even replace the value ( $item->title = “My new title”; )

Step 4
The final step is to create a zip of your new plugin and install using the Joomla extension installer. Remember to activate your new plugin!

Step 5
Import my plugins from the folder of the same name blue. The following code can be placed in any of my files but for this example I will place it at the top of default.php view template file (components/blue/views/show/tmpl/default.php)

JPluginHelper::importPlugin( 'blue' );// This is my plugin group folder located in the plugins folder.

Step 6
Get an instance of our plugin, we can place this below the above code

$dispatcher = JEventDispatcher::getInstance();

Step 7
Now we must trigger our custom event, you can create as many as you like. In this example our event will be called onBlueDisplayTitle. Try to give your plugin triggers unique names.
I am also passing the $this->item object which contains the row for that view, you can pass any variable to the event.

$dispatcher->trigger( 'onBlueDisplayTitle', array( $this->item ) );

We now have our plugin event set, if you refresh your page you should see This is my new title! displayed where your plugin event exist.

That’s it. If you have any questions or other, leave a comment below. Don’t forget to follow us on Twitter @westernstudios for more great tutorials.

Please Post Your Comments & Reviews

Welcome to Western Studios

We provide professional website and mobile softwares including Joomla, Wordpress and more.