Hook_init Drupal 8

broken image


  1. Drupal 8 Requirements
  2. Hook_init Drupal 8 Tutorial
  3. Drupal 8 Hook_init
  • Before I start my writing about Queue Worker, it is important to understand about cron job in Drupal 8. In generic terms, cron is a scheduled job which runs periodically at fixed intervals. Cron manages typical tasks such as database maintenance, sending bulk emails, fetching data from a third-party on regular basis. It manages short running tasks with fewer resources and it will automatically.
  • Easy Content Authoring. Drupal is built with essential tools for content creation and publishing, like.

As of Drupal 8, hookinit no longer exists, since, after introducing the Symfony kernel and events it was not serving any specific need. If your module needs to perform changes on the request/response object very early to the request an event subscriber should be used listening to the kernel.request event.

In Drupal – 8 most of the Hooks such as hook_init, hook_boot are removed from the Drupal 8.

These Hooks are replaced with Event Subscriber in Drupal 8. If you are performing few actions such as redirect, add CSS, add JS or any other modification on request, it can be done by registering Event Subscriber.

As we know Drupal 8 introduces Symfony Event Components and includes many Symfony components in Drupal 8 Core. In future versions of Drupal 8, Symfony Events will play a vital role and will enhance the website performance, functionality.


Drupal 8 has adopted symfony as a part of its core. It uses Symfony kernel and events to do the same now. List of kernel events available in Drupal 8 are as follows:

KernelEvents::CONTROLLER

The CONTROLLER event occurs once a controller was found for handling a request.

KernelEvents::EXCEPTION

The EXCEPTION event occurs when an uncaught exception appears.

KernelEvents::FINISH_REQUEST

The FINISH_REQUEST event occurs when a response was generated for a request.

KernelEvents::REQUEST

The REQUEST event occurs at the very beginning of request dispatching.

KernelEvents::RESPONSE

The RESPONSE event occurs once a response was created for replying to a request.

KernelEvents::TERMINATE

The TERMINATE event occurs once a response was sent.

KernelEvents::VIEW

The VIEW event occurs when the return value of a controller is not a Response instance.
Drupal 8 provides a way to subscribe to all these events and attach callbacks to be executed when these events occur. If our module needs to perform changes on the request/response object very early to the request an event subscriber should be used listening to the
How to create an Event Subscriber?

The example below shows a Drupal 7 hook_init() implementation which appends Access-Control-Allow-Origin to the response headers to allow CORS(Cross Origin Resource Sharing).

Steps to convert hook_init into an event subscriber in Drupal 8

Step 1: Assign autocomplete properties to textfield

As per Drupal Change records, #autocomplete_path has been replaced by #autocomplete_route_name and #autocomplete_parameters for autocomplete fields ( More details -- https://www.drupal.org/node/2070985).

The very first step is to assign appropriate properties to the textfield:

  1. '#autocomplete_route_name':
  2. for passing route name of callback URL to be used by autocomplete Javascript Library.
  3. '#autocomplete_route_parameters':
  4. for passing array of arguments to be passed to autocomplete handler.

Thats all! for adding an #autocomplete callback to a textfield.

However, there might be cases where the routes provided by core might not suffice as we might different response in JSON or additional data. Lets take a look at how to write a autocomplete callback, we will be using using my_module.autocomplete route and will pass arguments: 'name' as field_name and 10 as count.

Step 2: Define autocomplete route

Now, add the 'my_module.autocomplete' route in my_module.routing.yml file as:

While Passing parameters to controller, use the same names in curly braces, which were used while defining the autocomplete_route_parameters. Defining _format as json is a good practise.

Drupal 8 Requirements

Step 3: Add Controller and return JSON response

Drupal 8 hook_init alternative

Finally, we need to generate the JSON response for our field element. So, proceeding further we would be creating AutoCompleteController class file at my_module > src > Controller > AutocompleteController.php.

Date

Finally, we need to generate the JSON response for our field element. So, proceeding further we would be creating AutoCompleteController class file at my_module > src > Controller > AutocompleteController.php.


We would be extending ControllerBase class and would then define our handler method, which will return results. Parameters for the handler would be Request object and arguments (field_name and count) passed in routing.yml file. From the Request object, we would be getting the typed string from the URL. Besides, we do have other route parameters (field_name and Count) on the basis of which we can generate the results array.

Hook_init Drupal 8 Tutorial

An important point to be noticed here is, we need the results array to have data in 'value' and 'label' key-value pair as we have done above. Then finally we would be generating JsonResponse by creating new JsonResponse object and passing $results.

Drupal 8 Hook_init

That's all we need to make autocomplete field working. Rebuild the cache and load the form page to see results.





broken image