Drupal hook event dispatcher event subscriber example

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] even dispatcher instead of hopes it is a module that defines some events and lets us use them it is using events instead of hooks so let's use it in our next module where we're going to implement an event for the hook entity view and we're going to see which events we could use with the help of the hook event dispatcher module so on drupal.org under the projects page we see that who given dispatcher is basically a module that dispatches events for several Drupal core hooks so let's copy this bit here from the URL and let's jump to our Drupal code base and let's composer require this module in order to download it perfect and then let's go ahead and jump to our Drupal instance and under extent let's go ahead and enable the module who event dispatcher there is so install and as we're installing it it's not doing anything but it's enabling us to use events in order to hook to some Drupal core hooks so in order to use it would have to create our custom module and basically use the events from the hook event dispatcher module so let's jump to our code base and inside of the modules folder let's go ahead and create a new folder called custom and inside of it we're going to be creating our new module it's going to be called Drupal up hook fence and we're going to be creating a new file called the name of our module dot in 40ml and we're going to say it's Drupal up hook events description events instead of hooks type module for eight dot X and here we are going to declare also dependencies and we're going to see that we have a dependency to Drupal and then we're going to see that it's the hook event dispatcher machine name so it's always prefixed with Drupal and then hook event dispatcher so dependencies and we're going to now create a event subscriber and we're going to create a normal event subscriber the way we will do it for any other event inside of Drupal 8 but we're going to be subscribing to an event that is declared inside of the hook event dispatcher module so let's go ahead and create our event subscriber by creating a new file inside of our module inside of the SRC folder and inside of the event subscriber folder and we're going to call our event subscriber entity View subscriber and the entity view subscriber is going to be registering a callback for the hook entity view event so this way we're writing here a HB class that is called entity view subscriber and we are having a namespace that is always starting with Drupal and then the name of our module Drupal up hook events and then we're including also the event subscriber folder here inside of the namespace event subscriber and that's all we're going to be implementing the event subscriber interface so in order to do that we're going to be saying here use symfony component event dispatcher and then the event subscriber interface and we're going to be here implementing it the event subscriber interface that's perfect so we're seeing here that this is our entity view subscriber class and inside of the event subscriber class we are supposed to declare a public static function called get subscribed events and we're going to be returning a array with the subscribed events of the event and the callback where the callback would be a function that is a public function inside of the event subscriber class so in order to see what events do you have declared from the hook event dispatcher module we're going to go to the country folder and hook event dispatcher this is the folder that we downloaded with the help of composer and we're going to open the SRC and then the hook event dispatcher interface and it's a good practice we have all the events here as constants so we're interested in the entity view but as you see here there is also a entity insert entity update pre delete delete pre save entity access there are events for food access there are events for form order which is very popular or paths events view events team events and you could check them by yourself but we're going to be implementing the entity view event and in order to implement it it's a good practice just to use here like this we're saying use and we are kind of including this way the hook event dispatcher interface and here we could say just hook event dispatcher and we are calling the constant that is having the name of the event so we're saying entity view and as a value we are saying the callback that we're going to declare inside of our entity view subscriber class and we're going to call this entity view callback and we're going to declare it here as a public function and event like this so we could say our callback function and here it's a good practice just see like this that's perfect and now if we just go to our custom module and if we create a new file called the name of our module dot services dot UML we're going to see here services and we're going to say first Drupal up so the name of our module and we're going to say hook entity view and for a class of this service we're going to define Drupal then Drupal up hook events and then event subscriber and then the name of our event subscriber class which is called entity view subscriber and this is the normal way to declare a service and in order to make it an event subscriber we're going to adhere tags and we're going to say name event subscriber that's perfect so let's go ahead and jump to our Drupal instance again and refresh the extent page and if we search for our Drupal up hook events module we see that under here it has as a requirement the hook event dispatcher which comes from the dependencies inside of our influental file so let's go ahead and enable this module and after we enable it we're going to put here a output so like we're going to see a change event so that we see what we have there and then died and if we now go to our content page and if we add a new article for example because the articles are events and if we say test article and if we save it and we view it we see that our event subscribers callback is being triggered and if we check to see the output from our King in order to use Kint of course we have to have the development module installed just FYI so here we see what metals do we have at our disposal so we could get the entity and do stuff with it and then set the entity in order to alter it and this is all way to hook in the entity view so everything that you would usually do in the hook entity view you could now use the hook event dispatcher in order to register a callback for this event so as you see here the built is something that you could alter but in our case we are just getting the build and then we could be just setting the build which is a much nicer way to interact with the Drupal API so let's go ahead and quickly get the built so let's say build and here now we have the build so we have the View mode which is full we have the entity type for example we could check for the entity type and do stuff there we have here some render bowls so let's go ahead and just add some render go there so let's go ahead and say build equals event get built and let's say build new renderable is simply a render both type markup and with markup hello from our changed renderable that's perfect so let's close it correctly and in the end would have to say event obviously set built as you saw it exists so we're going to simply set the build with the Butte and we're going to just remove this and if we now just refresh the page we see that we have here an additional renderable that is coming from the hook entity view event that we subscribe to so if you like this video please take a look at the links in the description below like subscribe ask if you have any questions take a look at the patreon page of Drupal up in order to support this project share on Twitter and Facebook if you want to and if you like this video and thanks for watching [Music]
Info
Channel: Drupal Up - Drupal 8 Video Tutorials
Views: 4,516
Rating: 4.9574466 out of 5
Keywords: drupal, drupal8, drupal tutorial, drupal 8 tutorial learn drupal, hook_event_dispatcher, event subscriber, hook event dispatcher
Id: DzoqAVa1Bys
Channel Id: undefined
Length: 16min 36sec (996 seconds)
Published: Tue Feb 05 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.