When trying to add a preprocess function for field_comment_body, the existence of a preprocess function in a module causes the comment body to disappear.

Steps to reproduce:

  1. Install clean Drupal installation
  2. Create a content type
  3. Attach comments to this type
  4. Create node of the content type from #2
  5. Add comment to this page
    • Note that comment body is shown
  6. Create custom module
  7. Implement hook_preprocess_field__comment__comment_body():
    function HOOK_preprocess_field__comment__comment_body(&$vars) {
    
    }
    
  8. Enable custom module
  9. Go back to page created in step 4
    • Note that comment body is now missing

The mere existence of a preprocess function on a field should not prevent the rendering of the field.

Comments

Jaypan created an issue. See original summary.

jaypan’s picture

Title: Existence of preprocess function for comment body causes comment to disappear » Existence of preprocess function for comment body causes field to not render
tetranz’s picture

Assigned: Unassigned » tetranz

Confirmed on 8.3.x. Doing some debugging.

tetranz’s picture

I see the problem here but I don't think it has an easy answer.

We have two different things falling back to the same template.

When you implement hook_preprocess_field__comment__comment_body but do not provide a template, the following happens while the theme registry is being built:

It looks for the template field__comment__comment_body.html.twig.

When that is not found, it looks for a less specific template by slicing off the name at the double underscores. Therefore, it looks for field__comment.html.twig.

That does exist so it attempts to use it to render the body field but that's not what it is designed for. It's purpose is rendering the comment field of the node, i.e., the whole comment thread. It doesn't print anything for the body field.

I doubt if anything can really be done at this point given the naming rules. I guess the workaround is to copy field--test-long.html.twig to field--comment--comment-body.html.

tetranz’s picture

Assigned: tetranz » Unassigned
jaypan’s picture

Nice detective work!

Unfortunately that solution won't work form me, as I was attempting to come up with a module based solution to my issue, so that the change I was making (adding an attribute to the comment body's HTML wrapper) would persist when changing themes.

This is definitely a bug that needs to be fixed in future versions though.

tetranz’s picture

Yeah, I agree it's a bug. Someone a little deeper into the theming system might have some ideas.

If I'm understanding this correctly, it's a pity that the field type for "comments", which is defined in the comment module, has an id of "comment". It seems like it would have been better to call that "comments" to avoid conflict with the entity id of "comment".

The "conflict" happens in Registry::completeSuggestion but I don't see a way to avoid it.

But wait ... maybe the comment module could implement a hook to modify the registry to avoid this. I'll look into that.

jaypan’s picture

If I'm understanding this correctly, it's a pity that the field type for "comments", which is defined in the comment module, has an id of "comment". It seems like it would have been better to call that "comments" to avoid conflict with the entity id of "comment".

Yeah, that's what I think should happen in a future version.

tetranz’s picture

Assigned: Unassigned » tetranz
tetranz’s picture

Version: 8.2.x-dev » 8.3.x-dev
Status: Active » Needs review
Issue tags: +comment, +preprocess, +template
StatusFileSize
new1.25 KB

I think this will fix it. It's a bit of a bandaid but I think my logic is correct in that it will only catch this situation.

I think renaming the field would be very disruptive at this point.

tetranz’s picture

StatusFileSize
new1.25 KB
new651 bytes

Just removing an extra space.

tetranz’s picture

Assigned: tetranz » Unassigned
jaypan’s picture

Nice work, I'll give it a test in the next few days

tetranz’s picture

StatusFileSize
new1.25 KB
new498 bytes

Just noticed the docblock was wrong.

googletorp’s picture

I'm a bit unsure if this is a bug or not. You could argue, that if you implement the hook, you should also provide a suitable template suggestion. I have called for subsystem maintainer, so we can get feedback from some one who knows the code a bit better. I would like get a green light before we start implementing a solution as (s)he might have some good ideas for a solution.

If we do want to go ahead and make a solution, I have a few comments for the patch itself.

1. We need a test case which which should fail without and proposed fix and pass with the fix applied. You can do this by creating a test only patch.

2. hook_theme_registry_alter seems like the wrong approach here. It will make some assumptions about the template being used, and while in some cases it will do a good job at it, I can image a few cases where the behavior wont be what we want it to be. If we can't get it right for all cases, it will be better not to try to fix it IMO, as it will be easier to developers to track down, than this hook changing the theme registry in case they actually want to use `field__comment.html.twig` for the field (which might be the case in custom themes).

3. When implementing hooks, documentation should be Implement hook_foo(). (you are missing (). in your patch.)

tetranz’s picture

Thanks googletorp. I agree with your comments. It's a bit of a weird "bug" so let's wait for feedback from the maintainer.

jaypan’s picture

You could argue, that if you implement the hook, you should also provide a suitable template suggestion.

This is where I would disagree - as shown by the exact situation with which I came to this issue. There is no need to add a template, as it would be exactly the the same as the default template. Preprocess hooks are executed in both in modules and themes, not just themes, and templates won't be picked up in modules. So the argument that a template should also be provided is out of sync with the logic of allowing preprocess hooks in modules.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

markhalliwell’s picture

Version: 8.5.x-dev » 8.6.x-dev
Status: Needs review » Needs work
Issue tags: -comment, -preprocess, -template, -Needs subsystem maintainer review +Needs tests, +preprocess functions, +template suggestions
Related issues: +#939462: Specific preprocess functions for theme hook suggestions are not invoked

I'm a bit unsure if this is a bug or not.

It's most definitely a bug. One that should have been squashed by #939462: Specific preprocess functions for theme hook suggestions are not invoked. So not sure if something else has been committed that caused a regression or there just wasn't enough tests in the first place. But I have noticed a similar issue lately while working on other things.

hook_theme_registry_alter seems like the wrong approach here

Indeed. This is not a problem that is unique to just field comments. Any "fix" will likely need to be in the theme registry itself or ThemeManager::render. For this reason, I'm setting this issue to CNW.

I don't really have the time to take an in-depth look at this as it will likely require a lot of step-through debugging.

I'll leave the title for now as I'm not sure what a more accurate one would be ATM.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

guypaddock’s picture

Title: Existence of preprocess function for comment body causes field to not render » Declaring a preprocess function for a theme hook suggestion forces base template to be used at render time

Updating the issue title to be more general -- this affects more than just comments.

We just spent several hours trying to track down why one of the 16 fields in our content type (e.g. "my_type") is using base field.html.twig template while the rest of the fields are using the correct theme template of field--node--my-type.html.twig, despite the fact that our code does not force the template on this field in any way. The only thing different about this field was that it has a pre-process function in module code to fix-up the URL that's used for a taxonomy link.

After stepping through theme function suggestion resolution code in \Drupal\Core\Theme\ThemeManager::render(), I could see that there is a theme function in the theme registry that corresponds with the pre-process function, but the entry is forcing the use of the base theme implementation (field.html.twig) instead of the more-specific template. If we remove the pre-process hook and rebuild registries, the correct template gets used. This is def. a bug in core.

guypaddock’s picture

StatusFileSize
new48.97 KB

This issue actually looks worse than I thought... it looks like Drupal binds the template for the pre-process hook to one from Seven/Classy instead of even using the field template in our theme or its base theme (Radix). (Below, the template should be "field--node--my_type" not "field").

The wrong template and theme are bound to the render element in the theme registry when the preprocess hook is declared.

Luckily, there is a workaround:

/**
 * Implements hook_theme().
 */
function my_theme_theme($existing, $type, $theme, $path) {
  return [
    // BUGBUG: This is a workaround for DDO-2866208. We have to force Drupal to
    // use the correct theme template because it ignores template suggestions
    // when a pre-process hook is present.
    'field__node__field_my_type_collections__my_type' => [
      'template'  => 'field/field--node--my_type',
      'base hook' => 'field',
    ],
  ];
}

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

kle’s picture

As described, the bad guy is Registry::postProcessExtension($cache, $this->theme).
but I'm not able to write a bugfix :-(
Here is one more example which shows, that this bug is really annoying:

  1. add Nodetype "blog"
  2. add viewmode "hero"
  3. render a blog with viewmode hero
  4. use template node--blog--full.html.twig -> works fine
  5. use template node--hero.html.twig -> works fine
  6. add HOOK_preprocess_node__blog()
  7. now Registry::postProcessExtension() adds $cache['node__blog'] with values:
    • 'theme path' => 'core/themes/stable' // this is the base theme - not my theme !
    • 'template' => 'node' // typically it would be 'node--blog'
  8. Instead of node--hero.html.twig this tpl is now chosen: node.html.twig from base theme
    Why? Because the theme-suggestion "node--blog" comes before "node--hero" and "node--blog" has the bad entry from above...

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.