Drupal 8 Events

What is an event ?

Any action performed by a user or software is called as an event. E.g when a user clicks on a button, an action was performed i.e "Button was clicked" . So "CLICK" is an event. A page is loaded by a browser so "LOAD" is an event. Pressing a key on your keyboard,window resize,a touch gesture all are events performed by you. This is just a simple term which describes a certain action. 

What is an event handler / event listener?

As every action has its reaction. Same rule applies to the event. When you click on a button, a function defined by site developer gets executed. This type of function is termed as event handler or listener. Because they wait to be executed after an event triggered. Both the listener and handler terminologies are same. If you can see in most blogs you will find some small difference described.

In javascript, just to elaborate a bit further, remember that using your syntax you can only bind one handler to an onclick event. If you tried to add ".onclick = someotherfunction" later on in your code, the initial handler would no longer be run when a click is registered. To run multiple handlers on a single event, use the " .addEventListener() " method. Look up the documentation for syntax, but it takes a listener and a handler as arguments. You can use different type of handler like onclick, onkeypress etc as arguments to an eventlistener. It means when both mouse click and key pressed simultaneously the code inside eventlistener will be executed.  But I suggest don't be confused for now.

How to deal with "Events & Event Listeners" in more complex program

To deal with more complex program you may need a mediator who keeps the information of which event triggers which method (i.e event listeners). These information are stored like a mapped data or in an array. e.g

event1 => event_listeners1 ;

event2 => event_listeners2 ; 

Mediator is responsible to dispatch right event at right time. So mediator in programming terminology is called event Dispatcher.

Before we go just read this article the best way to deal with events in complex program using event dispatcher. http://blog.servergrove.com/2013/10/23/symfony2-components-overview-eventdispatcher/                                                

Well you read the above article from the link. You must have still some doubts. Let me try to clear it. Event listener is nearly same as event subscriber.  The main difference is that when an event listener have knowledge about which event they are going to response  then that is event subscriber. 

The main advantage of subscribers is that it keeps the knowledge of the events in the class rather than in the service definition, making it easier to reuse the listener in other contexts.

On the other hand, register listeners through the service definition gives more flexibility to the DI setup of the bundle allowing to add the tag conditionally based on the config for instance.

See more 

  1. http://symfony.com/doc/current/event_dispatcher.html
  2. http://symfony.com/doc/current/components/event_dispatcher.html 

Is Hook same as Events ?

Yes they do nearly equal job but differ in performance basis. Read this article hook vs events here

How to define an event and its listener in Drupal ?

  1. https://www.computerminds.co.uk/drupal-code/drupal-8-event-subscribers-successor-alter-hooks 
  2. https://api.drupal.org/api/drupal/core%21core.api.php/group/events/8.2.x