Generation of customized names for Twig templates using hook_theme_suggestions _HOOK_alter. Dmytro Selentsov

Dmitro Selentson performed today on the internal educational evening in DEWEB Studio. Main points from the report you may find below.

*******************************

In order to manipulate names of Twig templates we can use one of three following hooks:
hook_theme_suggestions_alter
hook_theme_suggestions_HOOK_alter
hook_theme_suggestions_HOOK

For the begining, let's consider the sequence of hook calls.

First, each hook works out for modules, according to its weight in the system, then according to alphabetical order.

Then the cycle repeats, but for all basic themes (keeping inheritance hierarchy).

At last, hooks works out according to custom theme.

Within each module and theme first hook_theme_suggestions_alter works out, then hook_theme_suggestions_HOOK_alter.

hook_theme_suggestions_HOOK_alter is what is so called "specific hook", it allows to alter specific elements - nodes, page, block, etc.

hook_theme_suggestions_alter - the "basic" hook, which will work for each element separately. Generally, the hierarchical chain of hook calls constructed in such a way that the more specific hook will be called by system at last.

 

So now we can conclude that for majority of problems associated with the changing templates names we need hook_theme_suggestions_HOOK_alter.

Coming back to our issue in order to manipulate Twig names of templates we use hook_theme_suggestions_HOOK_alter:

function hook_theme_suggestions_HOOK_alter (array & $ suggestions, array $ variables)

In the signature of function hook is appeared twice. The first hook is replaced by the name of the topic or modul, from which current hook is called, the second hook is replaced by the entity, which suggestions we will alter.

 

Arguments. As you can see, the function takes two arguments - an array of existing suggestions (passed by reference), and an array of variables, that belong to the pattern.

Then everything is simple - we place our custom logic, with its help we form suggestions, then we write results in the body of the function into array $suggestions. Few examples:

/ **
* Implements hook_theme_suggestions_HOOK_alter
* /
function my_module_theme_suggestions_block_alter (array & $ suggestions, array $ variables) {

 // Get view_id from current route object
 $ View_id = \ Drupal :: routeMatch () -> getRouteObject () -> getDefault ( 'view_id');
 if (! empty ($ view_id)) {
   $ Suggestions [] = 'my_module'. '__'. $ View_id;
 }

 // Add an interface-language specific suggestion for current theme hook.
 $ Suggestions [] = 'my_module'. '__'. 'Block'. '__'. \ Drupal :: languageManager () -> getCurrentLanguage () -> getId ();

 // Add suggestion for auth. users
 if (\ Drupal :: currentUser () -> isAuthenticated ()) {
   $ Suggestions [] = 'my_module'. '__'. 'Block'. '__'. 'Logged';
 }
}

*******************************

We hope you find it useful. Your feedback is very welcome!

Social activity
21.02.2017