Problem/Motivation

See views_theme_suggestions_node_alter(), and views_theme_suggestions_comment_alter(), also the code in views_preprocess_node() and views_preprocess_comment() actually.

That does stuff on $node->view. Which is assigned by views when displaying a node. But there is no cache context for this, can't really be, so it is not cached correctly.

Related to #2528314: template_preprocess_node() does not add cacheability metadata as well.

Proposed resolution

The only thing I can think of is to implement hook_entity_build_defaults_alter() and there, based on the view id and display, define a cache key. The obvious downside is that it results in more cache variations.

I'm also not 100% sure that it is completely accurate then. hook_entity_build_defaults_alter() has some limitations, if dynamic page cache doesn't know about those conditions. But that already has to know about the view/display, if not, you already have a problem.

I think as a long term solution, we should deprecate $node->view and everything that depends on it, but there is no way to make that obvious in something like twig theme suggestions debug output, so people will continue to use it.

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork drupal-2728419

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Berdir created an issue. See original summary.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

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

catch’s picture

Should definitely be deprecated.

Also do we have to consider theme suggestions part of the API, or could we potentially remove one in a minor release? Or we could move it to Stable, which ought to be empty in 9.x so counts as deprecated in that sense.

Adding another related issue that's bad for cacheability.

berdir’s picture

Discussed with @dawehner. Using hook_entity_build_defaults_alter(), we could add a cache key based on the view. However, that can result in a lot more cache variations for every rendered node. 10 views that show the same node result in $existing_cache_variations (per view mode, per language, per permissions hash) * 10.

Maybe we can have a setting, to enable this, and we only expose the suggestions and the cache key if enabled. Existing sites have it enabled, new ones have it disabled. We show a UI to disable it for sites that don't want it?

catch’s picture

The issue is partly that the cacheability is only affected if the suggestion is used, not just because it exists. However because theme suggestions end up just as strings and template names that makes this tricky (both for applying cacheability there and deprecating).

dawehner’s picture

Conceptually I totally agree that we should NOT have those suggestions in the first place.

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

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.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.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.

dawehner’s picture

What about throwing deprecations now in case we detect them?

gaëlg’s picture

Yes, if node--view--* templates are "broken" (as Berdir said in https://drupal.stackexchange.com/questions/222785/view-uses-the-wrong-te...), they should not be suggested to themers.
It's even more problematic because these suggestions actually work when there is no cache (typically on a local dev instance).

Everybody seems to agree on this, right ? But how do we deprecate a template suggestion?
Should be just add a "(deprecated)" notice in the Twig debug HTML comment for that suggestion?
Or maybe something clearer to explain that it's already broken (and not just meaning "won't work in D9")?
Or simply remove that suggestion from the HTML comment added by the Twig debug mode? (looks like the best option to me)

Are there other parts of the core code which need to be updated, like PHPDoc maybe? I can to try to work on this if it's quite simple.

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.

2pha’s picture

Wow, this screwed my head for hours today, templates working on local, but not on acquia dev... Ahhh

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

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should 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.

hawkeye.twolf’s picture

Version: 8.6.x-dev » 8.8.x-dev
StatusFileSize
new632 bytes

Might #2511548: Add a "context" array variable to all theme hooks and "#context" array property to all elements to provide optional contextual data open up a path to resolve this issue?

Alternatively, we could consider a different approach than the render cache key method proposed above. The actual node object gets pulled from \Drupal\Core\Cache\MemoryCache\MemoryCache, via EntityStorageBase::getFromStaticCache(). If we sidestep that cache by cloning the node object (or somehow purging the node from static cache before \Drupal\views\Plugin\views\query\Sql::loadEntities() loads it), this issue goes away.

Shall we consider the potential performance implications and/or side effects of such a change? See attached patch.

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

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should 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.

kristen pol’s picture

Version: 8.9.x-dev » 9.1.x-dev
Status: Active » Needs review

Patch from #14 applies cleanly to 9.1.x branch.

Status: Needs review » Needs work

The last submitted patch, 14: 2728419-14.patch, failed testing. View results

berdir’s picture

I don't understand how #14 would do anything to resolve this issue, this is about render cache conflicts.

IMHO, the only way forward is to deprecate this feature and display a warning if someone relies on this template and remove it in D10.

catch’s picture

Status: Needs work » Active

Agreed with #18 - we should deprecate the theme suggestions.

hawkeye.twolf’s picture

I don't understand how #14 would do anything to resolve this issue, this is about render cache conflicts.

@berdir, I gave a little background in comment #14. I believe you would find that it solves the issue. Now, that's not to say it's a desirable solution, but it would be worth discussing the reasons why.

berdir’s picture

I've read #14 and i don't see how that helps. The problem is that the logic in views_theme_suggestions_node_alter() is not reflected in the cache information, and cloning the node object doesn't change anything about how that works. Nor does adding more context. For render caching, all information must be known derivable (cache contexts) before the build process begins. This is too late.

What would help is implementing layout_builder_entity_build_defaults_alter() and whenever there is a view, add that information to the cache keys. But in my opinion, the cost of that is simply not worth it, because we'd end up with possibly a lot more cache variations when they are actually not needed for 99% of the sites. That's why I think this feature should be deprecated, we have view modes to have different versions of rendered nodes.

hawkeye.twolf’s picture

> "I've read #14 and i don't see how that helps"

@berdir, I know it's not very intuitive, but like I said, I believe you would find that it actually works. My request is that before we deprecate functionality as being too hard to solve, we actually debate the merits of the solution we do have.

hawkeye.twolf’s picture

Also, I should be clear about what specifically #14 fixes: The views-based entity theme suggestions (e.g., node--view--*). It may very well create other problems or fail to solve the underlying issue, but #14 does make the templates get picked up.

krzysztof domański’s picture

Based on several core contributors these theme suggestions should be deprecated so we need #3159050: Allow deprecating theme suggestions.

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.

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.

amanire’s picture

+1 to deprecate via #3159050, speaking as someone who just sunk a good chunk of time into an approach relying on node--view--* templates in Drupal 8.

In the meantime, can we revise the "Views template files" documentation in views.theme.inc?
https://api.drupal.org/api/drupal/core%21modules%21views%21views.theme.i...

neclimdul’s picture

Title: Node/comment views-based theme suggestions have no cacheability metadata » [PP-1] Node/comment views-based theme suggestions have no cacheability metadata
Status: Active » Postponed

If i'm reading hawkeye's comment in 14 right, he's saying that triggering the clone causes something in the render process to happen different which i guess is populating context's through render wrapping or something. I can't confirm this to be true, that's a very complicated series of events. Assuming it does work, I think the complication kinda provides an answer to the merits of the approach though. Possible performance impacts aside (purposely breaking static caches sort of proves this is going to be an issue), that would be super hard to test, nearly impossible to rationalize, and super brittle in the real world. It seems like changes by modules or customization could easily trigger a break in the careful stacking of processes.

I'm definitely for deprecating. Two reasons

1. This a little landmine waiting to destroy a developers deploy day.
2. I'm not sure the suggestion even makes sense. In the larger picture, the suggestion is applying another display mode on top of display modes. This means on top of the caching problems documented in this issue, there are underlying logic problems with managing fields and stuff. It seems like you want to use node rendering you should probably be using display/view modes for the different renders otherwise you should use one of the other views template methods.

Going to mark this PP on getting #3159050: Allow deprecating theme suggestions since it seems like that's a requirement for this going in and bumping the priority on that since its now blocking a Major.

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.

damontgomery’s picture

This should be removed if it doesn't work and can be added back in if a working version is found.

I just spent hours on this, because it's a suggestion and the suggestion is broken.

Multiple display modes with the same view on the same page all get the second "current_display".

Multiple views that display the same node entities get the second display too.

- Create two views that create blocks that render the same entity type in two unique view modes
- Place one of each block on a layout builder page
- Immediately after the second is placed, the first uses the seconds view mode

I tried this with different display modes on the same view and different views.

gaëlg’s picture

Issue tags: +TX (Themer Experience)

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.

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.

gbirch’s picture

This issue just destroyed my day, FWIW, in Drupal 10.3 no less.
I have added a bug report with suggested revisions to the documentation as a temporary fix. https://www.drupal.org/project/drupal/issues/3479114

catch’s picture

Title: [PP-1] Node/comment views-based theme suggestions have no cacheability metadata » Node/comment views-based theme suggestions have no cacheability metadata
Status: Postponed » Active

berdir’s picture

Title: Node/comment views-based theme suggestions have no cacheability metadata » Deprecate node/comment views-based theme suggestions and variables
Status: Active » Needs review

Finally, we can start to get rid of this.

berdir’s picture

Here's a question: Do we need BC test coverage for a feature that never had any test coverage in the first place?

We have test coverage for the theme suggestion deprecation feature, this would essentially just test that we have deprecated the right suggstions.

To test manually, all you have to do is visit frontpage on standard install profile after creating an article with twig debug on to see this:

 FILE NAME SUGGESTIONS:
   ▪️ node--view--frontpage--page-1.html.twig (deprecated)
   ▪️ node--view--frontpage.html.twig (deprecated)
   ▪️ node--1--teaser.html.twig
   ▪️ node--1.html.twig
   ▪️ node--article--teaser.html.twig
   ▪️ node--article.html.twig
   ✅ node--teaser.html.twig
   ▪️ node.html.twig
catch’s picture

I think it's OK to just do the deprecation here as long as we have generic coverage of the deprecation mechanism.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative
StatusFileSize
new216.66 KB

test

I use to be the person that assumed everything needs tests and then it ended up just being a ton of removals later on. As mentioned the functionality of deprecating theme suggestions is covered. I've attached a screenshot showing this doing as expected.

  • catch committed 1db458ee on 11.x
    Issue #2728419 by berdir, hawkeye.twolf, catch, neclimdul: Deprecate...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 11.x, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.