Hooks for Drupal 8 Themes


Data manipulation is one of the most basic fundamentals in creating functions and procedures and in Drupal, it's doing all the work for us and making it easier to access those information and what's more about that we can create our own function and create our own variable and be called together with the default variables, and with this in Drupal its achievable thru hooks.

Hooks allow modules to alter and extend the behavior of Drupal core, or another module. They are one of the various ways that code components in Drupal can communicate with one another. Using hooks a module developer can change how core or another module works -- without changing the existing code.From an implementation perspective hooks are specially-named functions called at specific times to alter or add to the base behavior. There are many type of hooks but for this topic we will work on the hook_preprocess. This hook allows modules to preprocess theme variables for theme templates. It is called for all theme hooks implemented as templates, but not for theme hooks implemented as functions. hook_preprocess_HOOK() can be used to preprocess variables for a specific theme hook, whether implemented as a template or function.

Example:

Changing field template suggestions

From the get go, Drupal gives us lots of options for templates that each field can use. Sometimes, though, you may want to point multiple different (programmatically unrelated) fields to the same Twig template. A common use case that I personally employ on most projects is something like field__bare - a field template I create that has no wrapper divs, labels, or classes, but simply renders the field's contents. There are plenty of other design reasons that you might want different fields to point to the same template, though.

/sites/default/files/assets/images/article-image/Anosa/Screen%20Shot%202020-12-21%20at%2013.19.39.png

 

Profile picture for user Jessie Allen Anosa
Jessie Allen Anosa Front-End Specialist

"Kumusta!". I call myself Jessie! A Filipino nomad raised in Philippines. Currently a Front-End Developer here in Acret-PH which is an awesome job that highlights my passion in designing and making things look good. Working here taught me a lot with the challenges and experience new things which helped me grow and continue to thrive to be better and do better, together with the support of the team and management it drives me to push myself to my limits and break my boundaries to be one of the person who will you can count and rely on. To sum it all, I'm in my happiest designing and making my living most from it :) — and trust me, I know things.

株式会社アクレットは、15年以上のDrupal開発実績を持っています。
プロジェクトの要件定義、システム設計から、Drupalサイト構築、運用サポートまで、Drupalに関するあらゆるサービスを提供いたします。

お問い合わせ