1

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?

1 Answer 1

1

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.

1
  • Thank you! I should have known something was up when I searched the codebase and couldn't find any uses of 'base_hook'!
    – inertiahz
    Commented Mar 8, 2023 at 11:42

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.