The Infinite Wonder of the Symfony Event Dispatcher

19

Transcript of The Infinite Wonder of the Symfony Event Dispatcher

Page 1: The Infinite Wonder of the Symfony Event Dispatcher
Page 2: The Infinite Wonder of the Symfony Event Dispatcher

The Infinite Wonder of the Symfony Event Dispatcher

Adam Englander

Page 3: The Infinite Wonder of the Symfony Event Dispatcher

Why Are You Here?• Drupal developer trying to

learn about Symfony?

• Symfony developer trying to learn about Drupal?

• PHP developer learning about Drupal?

• Cool presentation title?

• None of the above?

Page 4: The Infinite Wonder of the Symfony Event Dispatcher

How Did We Get Here?A brief overview of Drupal customization history

Page 5: The Infinite Wonder of the Symfony Event Dispatcher

The before time…the long, long ago• Copy core or template code to

site code

• Hack around for what you need

• Beam with the pleasure of customizing Drupal

• Break things unknowingly

• Only patch when required because you changed core

Page 6: The Infinite Wonder of the Symfony Event Dispatcher

Drupal 5-7• Copy core and customize for

true core functionality

• Use hooks for node events

• Use global functions to access core data and transforms

• Upgrade fairly simply as long as the hooks or global functions did not change

• Unit tests?Copyright 2006 Daniel Jaeger - Creative Commons - Attribution Share Alike

Page 7: The Infinite Wonder of the Symfony Event Dispatcher

Drupal 8 with Symfony

• Event subscribers for hooks

• Dependency injection for core services

• Portable code in open ecosystem

• Fully unit testable

• Upgrade at will

Page 8: The Infinite Wonder of the Symfony Event Dispatcher

Why Use an Event Dispatcher?I had this with hooks before all this Symfony nonsense

Page 9: The Infinite Wonder of the Symfony Event Dispatcher

Highly Modular Code• Build your modules and

templates like building blocks

• Reusable code in an open ecosystem

• Use proven libraries inside and outside the Drupal ecosystem

• Highly testable code at the unit level

Page 10: The Infinite Wonder of the Symfony Event Dispatcher

Highly Decoupled Code• Injects services in the

Dependency Injection Container

• Should not require intimate knowledge of core systems only event name and event class

• Allows for mix and match of components providing services based on events

Page 11: The Infinite Wonder of the Symfony Event Dispatcher

Highly Extensible Code

• Modular code is extensible

• Logic can be extracted services

• Logic can be extracted outside the Drupal environment

• Services can register and trigger their own events for extensibility

Page 12: The Infinite Wonder of the Symfony Event Dispatcher

How To Use the Event DispatcherThis better not be just some sales job

Page 13: The Infinite Wonder of the Symfony Event Dispatcher

Oversimplified Overview1. Create a service in your module that

implements the EventSubscriberInterface.

2. Write handlers for those events in your service.

3. Returned a list of handled events with the handlers and their priority with the service’s getSubscribedEvents method.

4. Register the service with the tag event_subscriber in your services.yml file.

Page 14: The Infinite Wonder of the Symfony Event Dispatcher

Example > 1000 Slides

https://github.com/aenglander/drupal-event-dispatcher-example

Page 15: The Infinite Wonder of the Symfony Event Dispatcher

This Is Only The BeginningWell, we can only hope

Page 16: The Infinite Wonder of the Symfony Event Dispatcher

Build A Better Module• More modular

• Loosely coupled

• Highly extensible

• Highly configurable

• Bulletproof

• Utilizing external libraries to build better and faster

Page 17: The Infinite Wonder of the Symfony Event Dispatcher

Build A Better Drupal• Create pre and post events for

everything

• Get away from copy core and modify

• Utilize more non-Drupal libraries for more functionality

• Accelerate platform growth

Page 18: The Infinite Wonder of the Symfony Event Dispatcher

My Contact Info• [email protected]

• @adam_englander on Twitter

• #aenglander on Freenode

• http://spkr8.com/t/66771

• https://github.com/aenglander/drupal-event-dispatcher-example

Page 19: The Infinite Wonder of the Symfony Event Dispatcher

Please rate my talk…http://spkr8.com/t/66771

Thanks!