Hooks are special functions to extend the Drupal core functionalites. Pick top 10 most used Drupal hooks to make your website powerfull. #Hooks in Drupal
What is a hook in Drupal 10?
Drupal modules can communicate to each other via hook system. Drupal core provides most of the hooks but contributed and custom modules can also provide hooks to interact with Drupal core APIs.
Hooks can perform various tasks such as register a twig template, alter the form fields, define fields definitions, react on CRUD operations and many other thigs.
How to find hooks provided by any module
Drupal core hooks can be found online and offline. Every module that provides hooks gives module.api.php file. There we can get the hooks provided by a particular module. For eample all the hooks provided by core User module can be found web/core/modules/user/user.api.php.
Top 10 most used Drupal hooks
- hook_schema
- hook_update_N
- hook_form_alter
- hook_ENTITY_TYPE_insert
- hook_ENTITY_TYPE_presave
- hook_ENTITY_TYPE_update
- hook_cron
- hook_theme
- hook_preprocess_HOOK
- hook_page_attachment_alter
#1 hook_schema
Before Drupal 8 this hook was everyone's favourite because of its flexibility to allow module developer to create custom database tables and their metadata such as keys, indexing details etc. Still this is most used hook but after introduction of custom entities, usage of this hook has been reduced.
The hook_schema must be defined in modulename.install file at module's root folder. For example if module name is simple_stemap then web/modules/contrib/simple_stemap/simple_stemap.install file should implement this hook.
Drupal automatically call this hook during module installation to create defined tables and related metadata. Uninstalling this module will delete tables from database created by this module. So be careful while working with this hook. The hook_schema executedc before hook_install and after hook_uninstall.
#2 hook_update_N
The hook_update_N can be useful when you are planning to release update of your module. The updates includes minor to minor or minor to major version, however, version upgrade such as Drupal 7 to 8 || 9 ||10 must be done via Migration API. This hook also lives in module.install file and n at the end must be a number.
Each update must be written in the new implementation by changing the N. For example pathauto_update_8001, pathauto_update_8002 and so on.
Update number must not be renamed, because Drupal stores the number in the database to track which update was executed in the past and which one is pending. To exucute this hook run drush updatedb or updb command.
#3 hook_form_alter
The hook_form_alter comes 3rd in the top 10 most used drupal hooks. This hook is much popular among junior developer because of it's use case. It can change the order of fields, field type, field value or set default value to any form field. You can leverage power of Ajax API to enable ajax or State API to make the form more inteactive.
Hook must be implemented in module.module file. There is another version of this hook available hook_form_FORM_ID_alter which can be used to target a specific form instead of all the available forms. If you are using hook_form alter then you must specify the condition to work on a specific form based on form id.
#4 hook_ENTITY_TYPE_insert
The beauty of this hook is the time of execution which is after an entity has been stored to the database. When you want to do some action once an Entity gets created, you can use this hook. As other major hooks module.module file is the location where this hook must be implemented.
#5 hook_ENTITY_TYPE_presave
You may want to do some data preparation before inserting data into the database. That prapared data can be used in various other purpose and this hook_ENTITY_TYPE_presave is the perfect time to do the activity. You can use this hook for any core, contributed and custom entity just replace the ENTITY_TYPE with the entity machine name. In this hook you will not get the entity ID if the entity is new and this hook also gets executed in case of entity update but before the hook_ENTITY_TYPE_update.
in the top 10 most used Drupal hooks the order can not fixed. Above ranking has been given based on my usage but that doesn't mean that the hook coming at the end is less important.
#6 hook_ENTITY_TYPE_update
In some situation you may want to act when an entity is getting updated. May be an email to the senior content editor so that the content can be published.
One point you should keep in mind that you will have the full object which is just updated that means you can't alter the object. But you can compare the old object and new object to see what was the change in any of the property.
#7 hook_cron
You have a module that needs to perform auto publish list of contents or need to clear the cache of specific page so that the users can see the fresh content at fix interval then you can set a cron job.
#8 hook_theme
Often we work with a controller and returns a render array as response. That response may use a twig template to display data into html format. To let Drupal know which twig template will be responsible to render the data, you can register your custom template using this hook_theme.
Now go the your custom module folder and create a twig template at templates/my-template.html.twig. From your controller you can return the render array like below code.
function build() {
return [
'#theme' => 'my_template',
'#data' => $prapared_data,
];
}
#9 hook_preprocess_HOOK
The hook can be used when you want to add or alter variables of a specific theme hook.
#10 hook_page_attachment_alter
The last hook in the top 10 most used Drupal hooks is hook_page_attachment_alter. The hook can be used to add or remove specially libraries which may depend on another module's library. It adds or remove the attachment before a page gets rendered.
Conclusion
Drupal hooks are PHP functions that let modules change and interact with the fundamental functionality of the core without changing the core files itself. Through the use of hooks, developers can add unique functionality to Drupal applications at particular stages of operation, such as the creation, display, or deletion of content. Every hook has an associated event, such as hook_node_insert for handling new content. By encouraging extensibility and flexibility, this architecture makes it possible to seamlessly include third-party modules and custom features into Drupal's core functions without sacrificing maintainability or future updates.