Ready-made extensions and custom development for Joomla!

Plugins tips and tricks

programmin joomla pluginsI'm reading the book "Programming Joomla Plugins" by our friend Jisse Reitma of Yireo.

As expected, many topics are already familiar in GiBiLogic, but I'm also learning a few cool new tricks.

Here's some bits that I found most interesting.

If you're really into plugin development you should probably buy the book yourself.

Inherited properties and methods

If you do the right thing and make your plugin class extend from JPlugin, you'll get an additional bonus of properties already set and methods available.

So, for example, in your plugin code you can use:

  • $this->params to access the plugin configuration parameters 
  • $this->_name to get the plugin name
  • $this->_type to get the plugin type (group)
  • $this->_subject to get the current event name
  • $this->_errors to get the plugin errors (empty by default)

Also, the following methods may be useful once you know they exist:

  • loadLanguage(): load the backend language file for the plugin
  • def(), get(), set(): define, read and assign a value to a property
  • getProperties(): read all plugin properties
  • setProperties(): set multiple properties at once

Note: your plugin inherits also setErrorgetError and getErrors but since JError is deprecated I suggest not to use them.

Autoload language

As you have just read, you can use loadLanguage() - probably in your __construct method - to load the backend language file for the plugin.

But with Joomla 3 you have another option, which is to set an additional property in your class:

protected $autoloadLanguage = true;

That will automatically call the loadLanguage() method for you.

App and db objects included

Starting from Joomla 3.1, you can get a JApplication and a JDatabase objects included as plugin properties.

You only have to declare one or both as class properties in your plugin:

protected $app;
protected $db;

The JPlugin constructor will initialize them for you, so you don't have to call JFactory::getApplication() and JFactory::getDbo(); you will be able to refer to them as $this->app and $this->db in the rest of your plugin code.

P.S. in OOP this technique - including external objects as properties of the current object - is called object composition and it's one of the approaches suggested by the Design Patterns book.

Conclusions

I haven't yet finished the book and already many useful tips have come up: congratulations to Jisse for his extensive knowledge of the topic.

Definitely a suggested read for any Joomla developer.