Skip to content

Instantly share code, notes, and snippets.

@asika32764
Last active April 29, 2019 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asika32764/67c9894c9cee758b7d64d9a72fa70fe3 to your computer and use it in GitHub Desktop.
Save asika32764/67c9894c9cee758b7d64d9a72fa70fe3 to your computer and use it in GitHub Desktop.
Joomla often used events example
<?php
/**
* @package {ORGANIZATION}.Plugin
* @subpackage system.plg_flower
* @copyright Copyright (C) 2012 {ORGANIZATION}.com, Inc. All rights reserved.
* @license GNU General Public License version 2 or later.
*/
use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\CMSPlugin;
defined('_JEXEC') or die;
/**
* Flower System Plugin
*
* @package Joomla.Plugin
* @subpackage System.flower
* @since 1.0
*/
class PlgSystemFlower extends CMSPlugin
{
/**
* Property self.
*
* @var PlgSystemFlower
*/
public static $self;
/**
* Property app.
*
* @var CMSApplication
*/
protected $app;
/**
* Constructor
*
* @param object $subject The object to observe
* @param array $config An array that holds the plugin configuration
*
* @throws Exception
*/
public function __construct($subject, $config)
{
parent::__construct($subject, $config);
$this->loadLanguage();
$this->app = Factory::getApplication();
self::$self = $this;
}
/**
* Get self object.
*
* @return mixed
*/
public static function getInstance()
{
return self::$self;
}
// System Events
// ======================================================================================
/**
* onAfterInitialise
*
* @return void
*/
public function onAfterInitialise()
{
}
/**
* onAfterRoute
*
* @return void
*/
public function onAfterRoute()
{
}
/**
* onAfterDispatch
*
* @return void
*/
public function onAfterDispatch()
{
}
/**
* onAfterRender
*
* @return void
*/
public function onAfterRender()
{
}
// Content Events
// ======================================================================================
/**
* Flower prepare content method
* Method is called by the view
*
* @param string $context The context of the content being passed to the plugin.
* @param object $content The content object. Note $article->text is also available.
* @param array $params The content params.
* @param int $page The 'page' number.
*
* @return void
* @throws Exception
*/
public function onContentPrepare($context, &$content, &$params, $page = 0)
{
$app = Factory::getApplication();
// Do some stuff
}
/**
* Flower after display title method
* Method is called by the view and the results are imploded and displayed in a placeholder
*
* @param string $context The context of the content being passed to the plugin.
* @param object $content The content object. Note $article->text is also available.
* @param object $params The content params.
* @param int $limitstart An integer that determines the "page" of the content that is to be generated.
*
* @return string
* @throws Exception
*/
public function onContentAfterTitle($context, &$content, &$params, $limitstart = 0)
{
$app = Factory::getApplication();
$result = null;
// Do some stuff
return $result;
}
/**
* Flower before display content method
* Method is called by the view and the results are imploded and displayed in a placeholder
*
* @param string $context The context of the content being passed to the plugin.
* @param object $content The content object. Note $article->text is also available.
* @param array $params The content params.
* @param int $limitstart An integer that determines the "page" of the content that is to be generated.
*
* @return string
* @throws Exception
*/
public function onContentBeforeTitle($context, &$content, &$params, $limitstart = 0)
{
$app = Factory::getApplication();
$result = null;
// Do some stuff
return $result;
}
/**
* Flower after display content method
* Method is called by the view and the results are imploded and displayed in a placeholder
*
* @param string $context The context of the content being passed to the plugin.
* @param object $content The content object. Note $article->text is also available.
* @param array $params The content params.
* @param int $limitstart An integer that determines the "page" of the content that is to be generated.
*
* @return string
* @throws Exception
*/
public function onContentAfterDisplay($context, &$content, &$params, $limitstart = 0)
{
$app = Factory::getApplication();
$result = null;
// Do some stuff
return $result;
}
/**
* Flower before save content method
* Method is called right before content is saved into the database.
* Article object is passed by reference, so any changes will be saved!
* NOTE: Returning false will abort the save with an error.
*You can set the error by calling $article->setError($message)
*
* @param string $context The context of the content being passed to the plugin.
* @param object $content A JTableContent object.
* @param bool $isNew If the content is just about to be created.
*
* @return bool If false, abort the save.
* @throws Exception
*/
public function onContentBeforeSave($context, $content, $isNew)
{
$result = array();
// Do some stuff
return !in_array(false, $result, true);
}
/**
* Flower after save content method
* Article is passed by reference, but after the save, so no changes will be saved.
* Method is called right after the content is saved
*
* @param string $context The context of the content being passed to the plugin.
* @param object $content A JTableContent object.
* @param boolean $isNew If the content is just about to be created.
*
* @return boolean
* @throws Exception
*/
public function onContentAfterSave($context, $content, $isNew)
{
// Do some stuff
return true;
}
/**
* Flower before delete method.
*
* @param string $context The context for the content passed to the plugin.
* @param object $data The data relating to the content that is to be deleted.
*
* @return boolean False to abort.
*/
public function onContentBeforeDelete($context, $data)
{
$result = array();
// Do some stuff
return !in_array(false, $result, true);
}
/**
* Flower after delete method.
*
* @param string $context The context for the content passed to the plugin.
* @param object $data The data relating to the content that is to be deleted.
*
* @return boolean
*/
public function onContentAfterDelete($context, $data)
{
$result = array();
// Do some stuff
return !in_array(false, $result, true);
}
/**
* Flower on change state method.
*
* @param string $context The context for the content passed to the plugin.
* @param array $pks A list of primary key ids of the content that has changed state.
* @param int $value The value of the state that the content has been changed to.
*
* @return boolean
*/
public function onContentChangeState($context, $pks, $value)
{
$result = array();
// Do some stuff
return !in_array(false, $result, true);
}
// Form Events
// ====================================================================================
/**
* Pre process form hook.
*
* @param JForm $form The form to be altered.
* @param array $data The associated data for the form.
*
* @return boolean
* @throws Exception
*/
public function onContentPrepareForm($form, $data)
{
$app = Factory::getApplication();
$result = null;
// Do some stuff
return $result;
}
// User Events
// ====================================================================================
/**
* Utility method to act on a user after it has been saved.
*
* @param array $oldUser An associative array of the columns in the user table (current values).
* @param boolean $isNew True if a new user is stored.
* @param array $newUser An associative array of the columns in the user table (new values).
*
* @return boolean
*/
public function onUserBeforeSave($oldUser, $isNew, $newUser)
{
$result = array();
// Do some stuff
return !in_array(false, $result, true);
}
/**
* Utility method to act on a user after it has been saved.
*
* @param array $user Holds the new user data.
* @param boolean $isNew True if a new user is stored.
* @param boolean $success True if user was succesfully stored in the database.
* @param string $msg Error message if store failed.
*
* @return void
*/
public function onUserAfterSave($user, $isNew, $success, $msg)
{
$result = array();
// Do some stuff
}
/**
* This method should handle any login logic and report back to the subject
*
* @param array $user Holds the user data
* @param array $options Array holding options (remember, autoregister, group)
*
* @return boolean True on success
*/
public function onUserLogin($user, $options = array())
{
$result = array();
// Do some stuff
return !in_array(false, $result, true);
}
/**
* This method should handle any logout logic and report back to the subject
*
* @param array $credentials Holds the user data.
* @param array $options Array holding options (client, ...).
*
* @return bool True on success
*/
public function onUserLogout($credentials, $options = array())
{
$result = array();
// Do some stuff
return !in_array(false, $result, true);
}
/**
* Utility method to act on a user before it has been saved.
*
* @param array $user Holds the new user data.
*
* @return boolean
*/
public function onUserBeforeDelete($user)
{
$result = array();
// Do some stuff
return !in_array(false, $result, true);
}
/**
* Remove all sessions for the user name
*
* @param array $user Holds the user data.
* @param boolean $success True if user was succesfully stored in the database.
* @param string $msg Error message if delete failed.
*
* @return boolean
*/
public function onUserAfterDelete($user, $success, $msg)
{
$result = array();
// Do some stuff
return !in_array(false, $result, true);
}
/**
* Prepare content data.
*
* @param string $context The context for the data
* @param int $data The user id
*
* @return boolean
*/
public function onContentPrepareData($context, $data)
{
$result = array();
// Do some stuff
return !in_array(false, $result, true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment