1

Is there a way to prevent other preprocess hooks from being triggered?

I have a Drupal 8 hook_preprocess_HOOK() implementation, and I'm doing some alterations. There will be other modules with same hook, and I need the other preprocess hooks not to alter my changes to $variables.

The only options I have are:

  • Rely on weights system, which still could cause problems
  • Set a value inside $variables and prevent other modules from doing their thing if the variable is set
1

You can disable hooks via hook_module_implements_alter().

function MYMODULE_module_implements_alter(&$implementations, $hook) {
  if ($hook == 'preprocess_page') {
    unset($implementations['module_foo']);
    unset($implementations['module_bar']);
  }
}
| improve this answer | |
  • It doesn't work with preprocess functions implemented by themes, though. – kiamlaluno May 29 '17 at 11:19
  • The OP asks for modules, so the response is accurate. – jonhattan Jun 1 '17 at 6:51
  • For themes, if the hook suggestions system is not enough, perhaps hook_theme_registry_alter() can do the job, by overriding the preprocess functions. – jonhattan Jun 1 '17 at 6:52
  • I am wondering why is is immposible to get debuger to stop and enter into MYMODULE_module_implements_alter function to inspect values? – Marko Blazekovic Jun 6 '19 at 14:35
  • @MarkoBlazekovic it is because that function is called during cache clear and not page load. Try setting a breakpoint and clearing caches through UI. – Alex Skrypnyk Jul 22 '19 at 3:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.