Content Events
Content plugin events are triggered not only by com_content
but also by other components such as Contacts, Categories and Tags, as well as some modules.
They are triggered during the process of:
- preparing the content for display in a View - both for textual HTML and for HTML forms
- handling the POST data submitted from a form - relating to data validation and CRUD operations at the MVC Model level
The sections below give a brief description of each content event, what the event parameters / arguments are, and any examples of their use which are available in the Joomla Manual.
For background on Joomla transitioning to using classes for events see Joomla 4 and 5 changes, where you can also find explanations for accessing the arguments and returning values.
onContentPrepare
Description
This is the first stage in preparing content for output and is the most common point for content-orientated plugins to do their work. Since the item and related parameters are passed by reference, event handlers can modify them prior to display.
Event Arguments
The event class \Joomla\CMS\Event\Content\ContentPrepareEvent has the following arguments:
-
context
- The context of the content being passed to the plugin. This is the component name and view - or name of module (e.g. com_content.article, com_contact.contact, com_users.user). Use this to check whether you are in the desired context for the plugin. -
item
- The item which is being rendered by the view, for example, an article, contact or user. You can access properties of this object using, for example,$item->title
; the properties available will depend on what type ofitem
is being passed. If you set any of these properties then they will be carried through to the webpage output, but not persisted in the database. -
params
- A reference to an associative array of the item parameters (usually theparams
field in the item's database record, but theattribs
database field for com_content). Any values which you set can affect the webpage output, but are not persisted in the database. -
page
- An integer which has been associated with the offset when displaying paginated lists of items. However, it is often set to 0 or null, so you probably shouldn't rely on it.
Return Value
None.
Examples
The Basic Content plugin provides a feature which processes the text of articles to replace {fieldname}
with the value of fieldname
.
onContentAfterTitle
This is a request for information that should be placed between the content title and the content body.
Event Arguments
The event class \Joomla\CMS\Event\Content\AfterTitleEvent has the following arguments:
Although item
and params
are passed by reference, this is not the event to modify item data. Use onContentPrepare for that purpose.
-
context
- The context of the content being passed to the plugin. This is the component name and view - or name of module (e.g. com_content.article, com_contact.contact, com_users.user). Use this to check whether you are in the desired context for the plugin. -
item
- The item which is being rendered by the view, for example, an article, contact or user. -
params
- A reference to an associative array of the item parameters (usually theparams
field in the item's database record, but theattribs
database field for com_content). -
page
- An integer which has been associated with the offset when displaying paginated lists of items. However, it is often set to 0 or null, so you probably shouldn't rely on it.
Return Value
String of HTML. This will get displayed after the item title.
The Joomla Custom Fields (com_fields) functionality has an associated plugin which is triggered by this event. This plugin returns the HTML for those custom fields which have the Automatic Display option set to After Title. So this event is triggered within the view which displays an item such as an article (com_content Article View), contact (com_contact Contact View), etc. If you implement a component which supports custom fields then you will need to dispatch this event.
onContentBeforeDisplay
This is a request for information that should be placed immediately before the component's main content (eg article text).
For views that generate HTML, this might include the use of styles that are specified as part of the content or related parameters.
Event Arguments
The event class \Joomla\CMS\Event\Content\BeforeDisplayEvent has the following arguments:
Although item
and params
are passed by reference, this is not the event to modify item data. Use onContentPrepare for that purpose.
-
context
- The context of the content being passed to the plugin. This is the component name and view - or name of module (e.g. com_content.article, com_contact.contact, com_users.user). Use this to check whether you are in the desired context for the plugin. -
item
- The item which is being rendered by the view, for example, an article, contact or user. -
params
- A reference to an associative array of the item parameters (usually theparams
field in the item's database record, but theattribs
database field for com_content). -
page
- An integer which has been associated with the offset when displaying paginated lists of items. However, it is often set to 0 or null, so you probably shouldn't rely on it.
Return Value
String of HTML. This will get displayed before the text of the item.
Examples
None.
The Joomla Custom Fields (com_fields) functionality has an associated plugin which is triggered by this event. This plugin returns the HTML for those custom fields which have the Automatic Display option set to Before Display Content. So this event is triggered within the view which displays an item such as an article (com_content Article View), contact (com_contact Contact View), etc. If you implement a component which supports custom fields then you will need to dispatch this event.
It is also used by the Joomla Vote plugin which allows website visitors to specify a star rating against content.
onContentAfterDisplay
This is a request for information that should be placed immediately after the component's main content (eg article text).
Event Arguments
The event class \Joomla\CMS\Event\Content\AfterDisplayEvent has the following arguments:
Although item
and params
are passed by reference, this is not the event to modify item data. Use onContentPrepare for that purpose.
-
context
- The context of the content being passed to the plugin. This is the component name and view - or name of module (e.g. com_content.article, com_contact.contact, com_users.user). Use this to check whether you are in the desired context for the plugin. -
item
- The item which is being rendered by the view, for example, an article, contact or user. -
params
- A reference to an associative array of the item parameters (usually theparams
field in the item's database record, but theattribs
database field for com_content). -
page
- An integer which has been associated with the offset when displaying paginated lists of items. However, it is often set to 0 or null, so you probably shouldn't rely on it.
Return Value
String of HTML. This will get displayed immediately after the text of the item.
The Joomla Custom Fields (com_fields) functionality has an associated plugin which is triggered by this event. This plugin returns the HTML for those custom fields which have the Automatic Display option set to After Display Content. So this event is triggered within the view which displays an item such as an article (com_content Article View), contact (com_contact Contact View), etc. If you implement a component which supports custom fields then you will need to dispatch this event.
onContentPrepareData
Description
This event is called after the initial data for a Joomla Form has been set, and can be used to modify that pre-fill data.
Within Joomla, when a form is loaded there is a callback loadFormData
which is used to specify the initial data for the form.
(For example, when editing a record the initial data will be the existing fields of the record).
At the end of loadFormData
Joomla extensions call preprocessData
and the default version of this function triggers this event.
Within Joomla extensions it is triggered before onContentPrepareForm.
Event Arguments
The event class \Joomla\CMS\Event\Model\PrepareDataEvent has the following arguments:
-
context
- The context of the content being passed to the plugin. This is the component name and name of item (e.g. com_content.article, com_contact.contact, com_users.user). Use this to check whether you are in the desired context for the plugin. -
data
- The initial (pre-fill) data for the fields of the form, passed as a PHP object. You can access properties of this object using, for example,$data->title
; the properties available will depend on what type ofdata
is being passed. If you set any of these properties then they will be modified in the form data which is presented to the user.
However, setting the properties directly is deprecated, and you should use updateData
instead:
$data = $event->getData();
// modify $data
$event->updateData($data);
Return Value
None.
onContentPrepareForm
This event is triggered after a Form has been loaded and can be used to modify the Form object using techniques described in Dynamically Changing Forms.
In Joomla components a form is loaded
- whenever the form is about to be displayed on a page
- in the process of handling the POST from a form (as it's needed to perform the field validation)
In both these cases the onContentPrepareForm event is dispatched.
Event Arguments
The event class \Joomla\CMS\Event\Model\PrepareFormEvent has the following arguments:
-
form
- The Form object. Thename
object of this object (which you can get via$form->getName()
) will be of the form 'com_content.article', 'com_contact.contact', and you can use this to check whether you are in the desired context for the plugin. You can then modify thisform
object using methods of the Form API. -
data
- The pre-fill data in the fields of the submitted form, passed as a PHP object. You can access properties of this object using, for example,$data->title
; the properties available will depend on what type ofdata
is being passed. If you set any of these properties then they will be modified in the form data which is presented to the user.
If the event is triggered in the process of handling the POST from a form then the data
will be empty and modifying it will have no effect.