Drupal core has a few problems when it comes to pattern based hooks or "hook suggestions". I have a patch up for core. It could probably use more work but there little interest in it. Figured I'd take what I know about the issue and apply it to the theme until it is fixed.

Problems

  1. If a hook suggestion is called through the theme() function, e.g., <?php theme('hook__suggestion'); ?> it will only invoke the preprocess and process functions for the connected "base hook". It's half baked in core preventing variable processors specific to the hook suggestion from being called.
  2. Closely related to #1, if the base hook does not have its own processor functions then the processors specific to the suggestion will be called. Makes no sense.
  3. If the hook suggestion is set through the variable key <?php $vars["theme_hook_suggestions"][] = 'hook__suggestion'; ?> from a variable processor, again, the variable processors specific to the suggestion will not be invoked. The reason for this is very different and difficult to explain. Since it is in the context of manipulating variables, changing the hook through the variable will not change the set of variable processors that are specific to the suggested hook.

The patch fixes all the above issues. Hook suggestions whether it's been passed to the function or set from a variable processor, it will always have the full list of variable processor functions invoked specific to the hook suggestion and its parent hook.

But the patch also does something that will be a great convenience to theme devs and that is auto registration of these hook suggestions. It can be done through preprocess functions, process functions and .vars.inc files. The significance of this is that you can control the data "logic" without altering with the template or theme function.

For example, lets say you want to change a few variables in the page.tpl.php file but you only want to affect under specific conditions. Normally you'd have to go into the preprocess/process function with a few if/else conditions potentially resulting in spaghetti code.

What you can do now is create a new preprocess/process function or .vars.inc file based on a pattern without a new theme function or template file. This will cause the pattern to register and it will inherit the meta from its closest parent hook.


/**
 * This will only be used on the front page.
 */
function THEME_preprocess_page__front(&$variables) {
  //...
}

That's all that's needed to register the "suggestion". Creating a variables include file named "page--front.vars.inc" will also do the trick. If you end up creating a theme function or template named after this suggestion, then it will be associated with that function/template as expected.

Nice and clean.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dvessel’s picture

Status: Active » Fixed
FileSize
32.05 KB
dvessel’s picture

Title: Consistent availability of variable processor functions for hook__suggestions and auto registration. » Consistent availability of variable processor, file includes and auto registration for hook__suggestions.
FileSize
5.49 KB

Simplified some parts and this followup patch fixes another core bug related to hook suggestions which is doing file includes.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.