Difference between revisions of "JReviews:Developers Events"
(→Using Event Listeners) |
|||
Line 33: | Line 33: | ||
You can create your own custom provider in overrides as we'll explain shortly, but if you want to override an existing provider or listener file you can also do that using [[JReviews_Code_Overrides#Overriding_JReviews_PHP_Files|JReviews Overrides]] for PHP files. | You can create your own custom provider in overrides as we'll explain shortly, but if you want to override an existing provider or listener file you can also do that using [[JReviews_Code_Overrides#Overriding_JReviews_PHP_Files|JReviews Overrides]] for PHP files. | ||
+ | |||
+ | Lets say you want to create a wildcard listener that listens for any event. To do this you would create new listener provider in overrides. It can be specific for this wildcard event customization or you can use a single listener provider for multiple listeners. In this example we'll use a dedicated provider and name it accordingly. | ||
+ | |||
+ | |||
+ | '''Joomla''' | ||
+ | |||
+ | <pre>/templates/jreviews_overrides/events/providers/wildcard_listener_provider.php</pre> | ||
+ | |||
+ | |||
+ | '''WordPress''' | ||
+ | |||
+ | <pre>/jreviews_overrides/events/providers/wildcard_listener_provider.php</pre> | ||
+ | |||
+ | The code inside this listener provider: | ||
+ | |||
+ | <source lang="php"> | ||
+ | <?php | ||
+ | defined( 'MVC_FRAMEWORK') or die; | ||
+ | |||
+ | use Clickfwd\Listener\ListenerProvider; | ||
+ | |||
+ | class WildcardListenerProvider extends ListenerProvider | ||
+ | { | ||
+ | protected $listen = [ | ||
+ | '*' => [ | ||
+ | 'JReviews\Listeners\Eavesdropper' | ||
+ | ] | ||
+ | ]; | ||
+ | } | ||
+ | </source> | ||
+ | |||
= Available Filters = | = Available Filters = |
Revision as of 15:38, 16 February 2018
JReviews 3 comes with Events that allow you to extend the functionality of JReviews without having to modify core files.
What is an Event?
An event is an action that typically takes place after some user interaction. Each event can have one or more listeners that react to the event. Unlike Filters, events do not modify data, they only trigger other actions through event listeners.
For example, when a user submits a review, this triggers the "ReviewWasCreated" event. There are several core event listeners that listen for this particular event to:
- Send e-email notifications
- Post the review on Twitter
- Post the review on Facebook
- Post the review to the EasySocial or JomSocial activity streams (Joomla)
- Trigger native Joomla or WordPress events
Event listeners run syncronously, one after the other, so this can delay the request response time after a user performs an action, especially if any of the events send out notifications, or require communicating with external APIs like for posting to Twitter or Facebook. To improve performance, we developed an add-on that allows queing the listener actions so they can be executed asyncrously, independent of the main program flow. Please refer to the Queue Add-on documentation for more information.
Using Event Listeners
With event listeners you can execute any code after a specific event runs. Listeners are assigned to events through a ListenerProvider. For reference you can find existing listener providers in the core in the following paths:
Joomla
/components/com_jreviews/jreviews/events/providers/jreviews_listener_provider.php
/components/com_jreviews/jreviews/cms_compat/joomla/events/providers/jreviews_joomla_listener_provider.php
WordPress
/wp-content/plugins/jreviews/jreviews/events/providers/jreviews_listener_provider.php
/wp-content/plugins/jreviews/jreviews/cms_compat/wordpress/events/providers/jreviews_wordpress_listener_provider.php
You can create your own custom provider in overrides as we'll explain shortly, but if you want to override an existing provider or listener file you can also do that using JReviews Overrides for PHP files.
Lets say you want to create a wildcard listener that listens for any event. To do this you would create new listener provider in overrides. It can be specific for this wildcard event customization or you can use a single listener provider for multiple listeners. In this example we'll use a dedicated provider and name it accordingly.
Joomla
/templates/jreviews_overrides/events/providers/wildcard_listener_provider.php
WordPress
/jreviews_overrides/events/providers/wildcard_listener_provider.php
The code inside this listener provider:
<?php defined( 'MVC_FRAMEWORK') or die; use Clickfwd\Listener\ListenerProvider; class WildcardListenerProvider extends ListenerProvider { protected $listen = [ '*' => [ 'JReviews\Listeners\Eavesdropper' ] ]; }