Score:1

Module template for a comment field is not triggering comment_preprocess_field

ng flag

I have a custom module in which I am trying to override a comment field's template in Drupal 9.3.

In my module file I have defined the hook_theme to override the core template in use. (comment.html.twig), which works, but it is not triggering the comment_preprocess_field hook in comment.module which then assigns the comment variables to the template.

My module file hook_theme is:

function MODULE_theme() {
  return [
    'field__field_FIELD_NAME' => [
      'base_hook' => 'field',
    ]
  ];
}

This works and my custom template is displayed, but it is missing the comments variables passed from comment_preprocess_field.

If I place the template in my theme, the comment_preprocess_field hook runs and the variables are added to the template, but not when done from a custom module.

Does anyone know how to get the hook comment_preprocess_field to run when overriding a comment field template in a module?

Score:1
cn flag

Use base hook, instead of base_hook (no underscore):

function MODULE_theme() {
  return [
    'field__field_FIELD_NAME' => [
      'base hook' => 'field',
    ]
  ];
}

That should invoke the preprocess as normal. You'll need to rebuild caches after making the change.

inertiahz avatar
ng flag
Thank you! I should have known something was up when I searched the codebase and couldn't find any uses of 'base_hook'!
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.