- 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.
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
KernelEvents::RESPONSE
KernelEvents::TERMINATE
KernelEvents::VIEW
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:
- '#autocomplete_route_name':
- for passing route name of callback URL to be used by autocomplete Javascript Library.
- '#autocomplete_route_parameters':
- 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
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.
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.