{"id":59075,"date":"2023-10-19T14:24:43","date_gmt":"2023-10-19T08:54:43","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=59075"},"modified":"2023-11-05T14:18:55","modified_gmt":"2023-11-05T08:48:55","slug":"how-to-create-custom-event-in-custom-in-drupal-10","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-create-custom-event-in-custom-in-drupal-10\/","title":{"rendered":"How to create custom event in custom in Drupal 10"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">Events in Drupal help to communicate between various modules and components in a decoupled manner. Events provide a flexible and standardized way for developers to extend the behavior of the system, enhance integration capabilities and execute custom logic at specific points in application flow.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">You have to understand the below points about event concepts in Drupal.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><b>Event Subscribers<\/b><span style=\"font-weight: 400;\"> \u2013 Sometimes called \u201cListeners\u201d, are callable methods or functions that react to an event being propagated throughout the Event Registry.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Event Registry<\/b><span style=\"font-weight: 400;\"> \u2013 Where event subscribers are collected and sorted.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Event Dispatcher<\/b><span style=\"font-weight: 400;\"> \u2013 The mechanism in which an event is triggered, or \u201cdispatched\u201d, throughout the system.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\"><strong>Step 1<\/strong>. Create custom Event in drupal 10\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In order to define an event,<\/span><span style=\"font-weight: 400;\"> create a class UserLoginEvent in your custom module \/src\/Event\/ path.<\/span><\/p>\n<pre>namespace Drupal\\custom_event\\Event;\r\n\r\nuse Drupal\\Component\\EventDispatcher\\Event;\r\nuse Drupal\\user\\UserInterface;\r\n\r\n\/**\r\n* Event that is fired when user login\r\n*\/\r\nclass UserLoginEvent extends Event {\r\n\r\n\u00a0 \u00a0 const EVENT_NAME = 'custom_event_user_login';\r\n\r\n\u00a0 \u00a0 \/**\r\n\u00a0 \u00a0 * the user account\r\n\u00a0 \u00a0 *\r\n\u00a0 \u00a0 * @var \\Drupal\\user\\UserInterface\r\n\u00a0 \u00a0 *\/\r\n\u00a0 \u00a0 public $account;\r\n\r\n\u00a0\r\n\u00a0 \u00a0 public function __construct(UserInterface $account)\r\n\u00a0 \u00a0 {\r\n\u00a0 \u00a0 \u00a0 \u00a0 $this-&gt;account = $account;\r\n\u00a0 \u00a0 }\r\n\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\"><strong>Step 2<\/strong> : Dispatch an event which you have created i.e UserLoginEvent.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">We are dispatching userLoginEvent in Hook_user_login at the time when user login in the system.<\/span><\/p>\n<pre>use Drupal\\custom_event\\Event\\UserLoginEvent;\r\n\r\n\/**\r\n\u00a0 * Implement Hook_user_login().\r\n\u00a0 *\/\r\n\u00a0 function custom_event_user_login($account){\r\n\u00a0 \u00a0\r\n\u00a0 \u00a0 \/\/initiate our event\r\n\u00a0 \u00a0 $event = new UserLoginEvent($account);\r\n\r\n\u00a0 \u00a0 \/\/Get the event dispatcher service and dispatch the event.\r\n\u00a0 \u00a0 $eventDispatcher = \\Drupal::service('event_dispatcher');\r\n\u00a0 \u00a0 $eventDispatcher-&gt;dispatch($event, UserLoginEvent::EVENT_NAME);\r\n\r\n\u00a0 }<\/pre>\n<p><span style=\"font-weight: 400;\"><strong>Step 3<\/strong> : Subscribe to the dispatched event. <\/span><span style=\"font-weight: 400;\">Create a class in module path \/src\/EventSubscriber\/UserLoginSubscriber<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">We have to extend this class with <\/span><b>EventSubscriberInterface. <\/b><span style=\"font-weight: 400;\">In this class we have to define function <\/span><b>getSubscribedEvents <\/b><span style=\"font-weight: 400;\">function.\u00a0<\/span><span style=\"font-weight: 400;\">This function will return events array with event and callback function details as below,<\/span><\/p>\n<pre>public static function getSubscribedEvents()\r\n\u00a0 \u00a0 {\r\n\u00a0 \u00a0 \u00a0 \u00a0 return [\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 UserLoginEvent::EVENT_NAME =&gt; 'onUserLogin',\r\n\u00a0 \u00a0 \u00a0 \u00a0 ];\r\n\u00a0 \u00a0 }<\/pre>\n<p><span style=\"font-weight: 400;\">Complete code of UserLoginSubscriber.php\u00a0 provided below. <\/span><span style=\"font-weight: 400;\">In this we are adding a message when that user was created.<\/span><\/p>\n<pre>&lt;?php\r\n\r\nnamespace Drupal\\custom_event\\EventSubscriber;\r\n\r\nuse Drupal\\custom_event\\Event\\UserLoginEvent;\r\nuse Symfony\\Component\\EventDispatcher\\EventSubscriberInterface;\r\n\r\nclass UserLoginSubscriber implements EventSubscriberInterface {\r\n\u00a0\r\n\u00a0 \u00a0 \/**\r\n\u00a0 \u00a0 * @inheritdoc\r\n\u00a0 \u00a0 *\/\r\n\u00a0 \u00a0 public static function getSubscribedEvents()\r\n\u00a0 \u00a0 {\r\n\u00a0 \u00a0 \u00a0 \u00a0 return [\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 UserLoginEvent::EVENT_NAME =&gt; 'onUserLogin',\r\n\u00a0 \u00a0 \u00a0 \u00a0 ];\r\n\u00a0 \u00a0 }\r\n\r\n\r\n\u00a0 \u00a0 \/**\r\n\u00a0 \u00a0 * subscribe to the user login event Dispatched.\r\n\u00a0 \u00a0 *\/\r\n\u00a0 \u00a0 public function onUserLogin(UserLoginEvent $event){\r\n\u00a0 \u00a0 \u00a0\r\n\u00a0 \u00a0 \u00a0 \u00a0 $database = \\Drupal::database();\r\n\u00a0 \u00a0 \u00a0 \u00a0 $dateFormatter = \\Drupal::service('date.formatter');\r\n\r\n\u00a0 \u00a0 \u00a0 \u00a0 $account_created = $database-&gt;select('users_field_data', 'ud')\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 -&gt;fields('ud', ['created'])\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 -&gt;condition('ud.uid', $event-&gt;account-&gt;id())\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 -&gt;execute()\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 -&gt;fetchField();\r\n\r\n\u00a0 \u00a0 \u00a0 \u00a0 \\Drupal::messenger()-&gt;addStatus(t('Welcome, Your account was created on %created_date.', [\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 '%created_date' =&gt; $dateFormatter-&gt;format($account_created, 'short'),\r\n\u00a0 \u00a0 \u00a0 \u00a0 ]));\r\n\r\n\u00a0 \u00a0 }\r\n}\r\n\r\n\r\n<\/pre>\n<p><span style=\"font-weight: 400;\">Tag event subscriber with event_subscriber<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Create\u00a0 {module_name}.services.yml file in module path. Add below in this file.<\/span><\/p>\n<pre>services:\r\n\u00a0 custom_event_user_login:\r\n\u00a0 \u00a0 class: '\\Drupal\\custom_event\\EventSubscriber\\UserLoginSubscriber'\r\n\u00a0 \u00a0 tags:\r\n\u00a0 \u00a0 \u00a0 - { name: 'event_subscriber' }<\/pre>\n<p><span style=\"font-weight: 400;\">When user login to the system, this event dispatched and the message which we added in eventSubscriber will display as below :<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-59074 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/10\/admin-DrupLearn.png\" alt=\"\" width=\"956\" height=\"306\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/10\/admin-DrupLearn.png 956w, \/blog\/wp-ttn-blog\/uploads\/2023\/10\/admin-DrupLearn-300x96.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/10\/admin-DrupLearn-768x246.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/10\/admin-DrupLearn-624x200.png 624w\" sizes=\"(max-width: 956px) 100vw, 956px\" \/><\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Events in Drupal help to communicate between various modules and components in a decoupled manner. Events provide a flexible and standardized way for developers to extend the behavior of the system, enhance integration capabilities and execute custom logic at specific points in application flow. You have to understand the below points about event concepts in [&hellip;]<\/p>\n","protected":false},"author":1620,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":473},"categories":[3602],"tags":[5261,5508,1181],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/59075"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1620"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=59075"}],"version-history":[{"count":2,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/59075\/revisions"}],"predecessor-version":[{"id":59380,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/59075\/revisions\/59380"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=59075"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=59075"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=59075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}