2

Let's say some people are working on a contrib module (link) to provide some obvious template suggestions which are not covered by the default Twig Template naming conventions. View mode for fields, region for blocks etc.

Out of habit I'd use hook_theme_suggestions_HOOK_alter. I simply add my suggestions following some logic, done.

This hook allows any module or theme to provide alternative theme function or template name suggestions and reorder or remove suggestions provided by hook_theme_suggestions_HOOK() or by earlier invocations of this hook.

Then I see there's hook_theme_suggestions_HOOK which more or less does the same except for reordering or removing suggestions. But basically looking at its description it does exactly what that contrib module is supposed to do.

This hook allows modules to provide alternative theme function or template name suggestions.

And now I'm wondering which one of these hooks should be used at best? Is it totally up to us? Do there arise any disadvantages of using the *_alter hook? Do we better use the mere suggestion *_HOOK to allow other modules to intervene later in the process?

5
  • 1
    The linked module should try to add suggestions in hook_theme_suggestions_HOOK as long as it doesn't depend itself on existing suggestions, so that other modules still have the option to override these suggestions in an alter hook.
    – 4uk4
    Nov 29, 2018 at 21:26
  • @4k4 – To use the *_alter hook really only when I alter existing suggestions in the sense of changing them to something different? Like so?
    – leymannx
    Nov 29, 2018 at 21:32
  • 1
    Hooks don't have priorities, so having two hooks, one main hook and one alter tries to compensate this. Like having a priority system with only two levels. So if you add something on the second level there is no third level for other modules, unless they resort to unusual solutions like module weights.
    – 4uk4
    Nov 29, 2018 at 21:42
  • @4k4 – Would you mind making that a canonical answer? As I see hook_theme_suggestions_HOOK_alter to be recommended pretty often for simply adding new suggestions. At least I did recommend it on some few upvoted and accepted answers. Which I will update.
    – leymannx
    Nov 29, 2018 at 21:51
  • 1
    Don't think you need to update if the answers are for custom modules. The contrib module in the question is different, because it adds more general suggestions which others might want to alter in their custom modules.
    – 4uk4
    Nov 29, 2018 at 22:07

1 Answer 1

3

The linked module should try to add suggestions in hook_theme_suggestions_HOOK as long as it doesn't depend itself on existing suggestions, so that other modules still have the option to override these suggestions in an alter hook.

Hooks don't have priorities, which having two hooks, one main and one alter hook, is trying to compensate. This is like a priority system with only two levels. If you add something on the second level there is no third level for other modules, unless they resort to unusual solutions like module weights.

1
  • Hooks do have priorities, and alter hooks are executed after the other hooks. It's not possible to execute hooks after the equivalent alter hooks.
    – apaderno
    Nov 30, 2018 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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