How do you name a Joomla event, will it be triggered?

– No! You, of course, can name events whatever you want and call them from anywhere, but correctly composed event names will not require memorization, they will seem to be printed on the screen themselves.

How should you name events in your components? – So that they are read as full-fledged sentences, and for this you need to understand their structure.

A regular sentence consists of a subject, a predicate and an object.

Let's look at a few events from the Joomla core:

onContentBeforeDisplay

  • on the prefix with which it is customary in Joomla to start the event name;

  • Content — a noun, is the subject of a sentence, in the com_content component there is only one type of entity (materials), so the name of the component is enough to determine the noun;

  • Before – a preposition of time indicating that an event occurs before the specified action;

  • Display – a verb that is the predicate in a sentence.

onBeforeCompileHead

And in the event onBeforeCompileHead there is no subject (this is normal for Joomla core events, it is assumed that the action is performed by the system). But it has an addition:

  • on – prefix;

  • Before – preposition of time;

  • Compile – predicate;

  • Head – a noun, is an object in a sentence, defines the object on which the action will be performed.

onFinderCategoryChangeState

In the event onFinderCategoryChangeState the subject consists of two words: the name of the component – Finder and entity name – Category.

  • on – prefix;

  • FinderCategory – subject;

  • Before – preposition of time;

  • Compile – predicate;

  • Head – a noun, is an object in a sentence, defines the object on which the action will be performed.

onComUsersControllerMethodBeforeAdd

Another example of a readable name for an event; when you encounter such an event in a plugin, you will immediately understand where it is called.

  • on – prefix;

  • ComUsersControllerMethod – subject (component+controller);

  • Before – preposition of time;

  • Add – predicate, (method).

The class name is usually made a noun, the method name is a verb or verb+noun. This gives us the basic formula for the event name: on + Class + Before/After + method . What if

Separately, I would like to draw attention to events that return results

Some event names, for example: onGetStatsData, onApiGetFields… do not include a preposition of time (Before/After), but do contain a verb Get because the event is called neither before nor after, but instead of an action, in order to receive data from the plugins.

<?php
$result = Factory::getApplication()->triggerEvent('onGetStatsData', ['stats.field.data']);
<?php
/** @var OnGetApiFields $eventResult */
$eventResult = Factory::getApplication()->getDispatcher()->dispatch('onApiGetFields', $event);

I hope these recommendations will help beginners and not-so-very Joomla developers to make their code more readable.

Similar Posts

Leave a Reply

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