d8 dispatcher / subscriber

20
Drupal 8 Hooks or Events

Upload: joshirohit100

Post on 16-Jan-2017

151 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: D8 dispatcher / subscriber

Drupal 8 Hooks or Events

Page 2: D8 dispatcher / subscriber

Drupal is in its 8th version and we are seeing a shift away from many Drupalisms towards more modern PHP architectural decisions.

Page 3: D8 dispatcher / subscriber

1. Using PSR-4 for autoloading.2. Plugins and annotations are taking away much of the need for info hooks.3. Dependency injection.4. Event Dispatcher and Subscriber.5. PHPUnit.6 Guzzle etc….

Example

Page 4: D8 dispatcher / subscriber

Event Dispatcher and Event Subscriber

Page 5: D8 dispatcher / subscriber

Why Event Dispatcher / Subscriber ?1. Developer (not drupal background) can easily

understand.2. More industry standard approach.3. Used by Laravel, Zend, Symphony etc. frameworks.4. In Drupal 9, we may not see hook system.5. If you are thinking to create your own framework/cms.6. Inbox performance.

Page 6: D8 dispatcher / subscriber

Benefits of Event driven approach1.Lazy Loading2.Stopping Propagation3.Priority

Page 7: D8 dispatcher / subscriber

Prerequisite1.OOPS Concept2.Service Container

Page 8: D8 dispatcher / subscriber

Event Dispatcher

The EventDispatcher component provides tools that allow your application components to communicate with each other by dispatching events and listening to them.

Page 9: D8 dispatcher / subscriber

1. It uses symphony’s EventDispatcherInterface to implement this.

2. Tag service with ‘event_dispatcher’.3. Uses dispatch() method to trigger events.

Page 10: D8 dispatcher / subscriber

Dispatch an Event1. Get the instance of Event dispatcher :- $dispatcher = \Drupal::service('event_dispatcher');

2. Dispatch the event. $dispatcher->dispatch(‘my_event’, $e);

Page 11: D8 dispatcher / subscriber

Dispatch Method

Dispatch method take two arguments - a. Event Nameb. Event Object

Page 12: D8 dispatcher / subscriber

Event Naming Convention1. Must be unique.2. use only lowercase letters, numbers, dots (.), and

underscores (_);3. prefix names with a namespace followed by a dot (e.g.

kernel.);4. end names with a verb that indicates what action is

being taken (e.g. request).

Page 13: D8 dispatcher / subscriber

Few Symphony Events Example - 1.KernelEvents::Request2.KernelEvents::Response3.KernelEvents::Controller4.KernelEvents::View5.KernelEvents::Terminate

Page 14: D8 dispatcher / subscriber

Event ObjectIt contains the detail that dispatcher passes to the subscriber.

It should be an instance of a class extending symphony’s Event base class.

Page 15: D8 dispatcher / subscriber

Event SubscriberAn event subscriber is a PHP class that's able to tell the dispatcher exactly which events it should subscribe to.

Page 16: D8 dispatcher / subscriber

Define Event Subscriber1. Implements EventSubscriberInterface2. Tag service as ‘event_subscibe’.

Page 17: D8 dispatcher / subscriber

Register Subsciber1.$dispatcher->addListener('my.event',

array($listener, 'onMyAction'));2.Static getSubscribedEvents() method.

Page 18: D8 dispatcher / subscriber

public static function getSubscribedEvents() {$events[DummyEvents::DUMMY_CONFIG_SAVED]

[] = array('onConfigSave', 0);return $events;

}

Page 19: D8 dispatcher / subscriber

1.Dummy sample code -https://github.com/joshirohit100/event_example

2. Documentation - https://api.drupal.org/api/drupal/core!modules!system!core.api.php/group/events/8

Page 20: D8 dispatcher / subscriber

Thankyou