Extra Block Types (EBT) - New Layout Builder experience❗

Extra Block Types (EBT) - styled, customizable block types: Slideshows, Tabs, Cards, Accordions and many others. Built-in settings for background, DOM Box, javascript plugins. Experience the future of layout building today.

Demo EBT modules Download EBT modules

❗Extra Paragraph Types (EPT) - New Paragraphs experience

Extra Paragraph Types (EPT) - analogical paragraph based set of modules.

Demo EPT modules Download EPT modules

Scroll

9.2. What "hook" does it mean in Drupal?

23/09/2019, by mikhail

This article is intended to familiarize you with Drupal, we will write code examples further, after adding our custom module.

Drupal is not a monolithic system in which everything is perfect and nothing needs to be changed. Very often, customers are asked to add this or that feature to the site. In order to be able to maintain Drupal core code and add functionality to Drupal, we use modules. The modular system allows you to expand the capabilities of Drupal. But what if we need to expand the capabilities of an off-the-shelf counter module? Of course, we can send a request to refine the functionality we need by creating an issue on drupal.org and possibly in a month, two .. a year or two, we will get the functionality we need in a particular module. But we can act differently and write the code we need ourselves. In order to expand the capabilities of additional modules of Drupal, as well as of Drupal itself, we will use hooks and plugins in our own modules.

The main idea is that we don’t need to support the Drupal code, the code of additional modules, and in this we would get a lot of savings on paying for the work of the programmer. In order to be able to easily update the code of modules and Drupal, you cannot make changes to the core of Drupal and counter-modules. Otherwise, all your changes will be erased the next time the module or Drupal core updated.

For the interconnection of modules and Drupal core, as well as modules among themselves, Drupal has a hook system. A hook is a callback of a function, that is, when the execution of the code reaches the hook, the code of our function is added, which we add in our module. Thus, we can process user data, menus, taxonomies, nodes of various content types at any time from changing, adding, deleting, or simply loading and displaying. There are quite a lot of hooks for the 8th Drupal, but already fewer compared to the 7th Drupal, a lot of hooks were implemented through third-party symfony components:

https://api.drupal.org/api/drupal/core%21core.api.php/group/hooks/8.2.x

If you look at the 7th version, you have to scroll down a long time:

https://api.drupal.org/api/drupal/includes%21module.inc/group/hooks/7.x

But this does not mean that the 8th Drupal has become less powerful, just everything that is implemented in the 7th Drupal hook, in the 8th you need to add the appropriate plug-in for this.

If you carefully look at the list of hooks in the 8th Drupal, you will see _alter at the end in their name, which means that this hook is designed to change the values ​​of variables, for example:

hook_form_alter () - allows you to change the array of the form during its processing in the Drupal. Later we will analyze how forms are generated from arrays in Drupal. To apply this hook in the module, like the other hooks, we simply write a function:

function mymodule_form_alter() {
  // change the form array
}


I simplified the example, did not write the arguments of the function, namespaces, we will figure it out for now, while we need to understand how hooks work. Mymodule is our module, we write instead of the word hook the name of our module and the function automatically picks up and modifies the form arrays. How it works? In the prepareForm method, another alter() method is called:

$this->moduleHandler->alter($hooks, $form, $form_state, $form_id);

This means that each module in the Drupal that implements hook_form_alter () inserts its own code in this place. Thus, if we add the code to some place in the Drupal, then the first thing we should do is through the hook, then through the plugin and if it is already completely impossible not to change the plug-in module, then we will patch / hack / change the module (unfortunately, we have to to do so). So far, we are not faced with tasks that require changing the plug-ins, so let's learn how to write our own modules.