Plugin/Events/System

From Joomla! Documentation

< Plugin‎ | Events

This topic is aimed at "global" system events, including those triggered on every page load (onAfterInitialise, onAfterRoute, onAfterDispatch, onAfterRender). Visit Plugin Events for a complete list over available core events.

onAfterInitialise[edit]

Description[edit]

This event is triggered after the framework has loaded and the application initialise method has been called.

Parameters[edit]

None.

Return Value[edit]

None.

Called in files[edit]

  • index.php
  • administrator/index.php

onAfterRoute[edit]

Description[edit]

This event is triggered after the framework has loaded and initialised and the router has routed the client request.

Routing is the process of examining the request environment to determine which component should receive the request. The component optional parameters are then set in the request object that will be processed when the application is being dispatched.

When this event triggers, the router has parsed the route and pushed the request parameters into JInput to be retrieved by the application.

Beware[edit]

It's bad practice to instantiate the Document object inside this and earlier running plugin events. Don't use Factory::getApplication()->getDocument(), Factory::getDocument(); or HTMLHelper/JHtml methods in function onAfterRoute() that instantiate the Document too early (e.g. for loading scripts or stylesheets). See also comments github.com/joomla/joomla-cms/pull/25669#issuecomment-513574075 and github.com/joomla/joomla-cms/pull/25669#issuecomment-513574282

Parameters[edit]

None.

Return Value[edit]

None.

Used in files[edit]

  • index.php
  • administrator/index.php

onAfterDispatch[edit]

Description[edit]

This event is triggered after the framework has dispatched the application.

Dispatching is the process of pulling the option from the request object and mapping them to a component. If the component does not exist, it handles determining a default component to dispatch.

When this event is triggered the output of the component is available in the document buffer.

Parameters[edit]

None.

Return Value[edit]

None.

Used in files[edit]

  • index.php
  • administrator/index.php

onBeforeRender[edit]

Description[edit]

This event is triggered immediately before the framework has rendered the application.

Parameters[edit]

None.

Return Value[edit]

None.

Used in files[edit]

  • includes/application.php
  • administrator/includes/application.php

onAfterRender[edit]

Description[edit]

This event is triggered after the framework has rendered the application.

When this event is triggered the output of the application is available in the response buffer.

Parameters[edit]

None.

Return Value[edit]

None.

Used in files[edit]

  • includes/application.php
  • administrator/includes/application.php

onBeforeCompileHead[edit]

Description[edit]

This event is triggered before the framework creates the Head section of the Document.

Parameters[edit]

None.

Return Value[edit]

None.

Used in files[edit]

  • libraries/joomla/document/html/renderer/head.php

Example[edit]

This is an example system plugin. Please note that because system plugins are loaded before any other event group, they may also contain any other event.

The following class would be located in the file /plugins/system/example/example.php.

<?php
defined('_JEXEC') or die;

/**
 * Example system plugin
 *
 * @since  1.0
 */
class plgSystemExample extends JPlugin
{
	/**
	 * Constructor.
	 *
	 * @param   object  &$subject  The object to observe.
	 * @param   array   $config	An optional associative array of configuration settings.
	 *
	 * @since   1.0
	 */
	public function __construct(&$subject, $config)
	{
		// Calling the parent Constructor
		parent::__construct($subject, $config);

		// Do some extra initialisation in this constructor if required
	}

	/**
	 * Listener for the `onAfterInitialise` event
	 *
	 * @return  void
	 *
	 * @since   1.0
	 */
	public function onAfterInitialise()
	{
		// Do something onAfterInitialise
	}

	/**
	 * Listener for the `onAfterRoute` event
	 *
	 * @return  void
	 *
	 * @since   1.0
	 */
	public function onAfterRoute()
	{
		// Do something onAfterRoute
	}

	/**
	 * Listener for the `onAfterDispatch` event
	 *
	 * @return  void
	 *
	 * @since   1.0
	 */
	public function onAfterDispatch()
	{
		// Do something onAfterDispatch
	}

	/**
	 * Listener for the `onAfterRender` event
	 *
	 * @return  void
	 *
	 * @since   1.0
	 */
	public function onAfterRender()
	{
		// Do something onAfterRender
	}
}

The plugin's XML installation file would be located at /plugins/system/example/example.xml would be defined as follows:

<?xml version="1.0" encoding="utf-8"?>
<extension version="3.7" type="plugin" group="system">
  <name>plg_system_example</name>
  <author>Author</author>
  <creationDate>Month 2008</creationDate>
  <copyright>Copyright (C) 2008 Holder. All rights reserved.</copyright>
  <license>GNU General Public License</license>
  <authorEmail>email</authorEmail>
  <authorUrl>url</authorUrl>
  <version>1.0.1</version>
  <description>An example system plugin</description>
  <files>
	<filename plugin="example">example.php</filename>
  </files>
  <config>
	<fields name="params">
	  <fieldset name="basic">
		<field
			name="example"
			type="text"
			default=""
			label="Example"
			description="An example text parameter"
		/>
	  </fieldset>
	</fields>
  </config>
</extension>

More system Plugin examples can be found here: https://github.com/joomla/joomla-cms/tree/staging/plugins/system