Problem/Motivation

Once #2381217: Views should set cache tags on its render arrays, and bubble the output's cache tags to the cache items written to the Views output cache is in, we expose the full chain of cache tags for a view.

Beside from that we have quite a complex mechanism to calculate the cache contexts used by a view, by iterating over all plugins.
One central piece though is missing still, taking into about node access or other kind of access mechanisms.

Proposed resolution

  • Allow views base tables to define view cache contexts. Core would probably just define cache_context.node_view_grants for node
  • Determine this information from entity types by default, so add an additional ContentEntityType::getListCacheContexsts()
  • Use that information both in views base tables as well as in list builder

Remaining tasks

User interface changes

API changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dawehner’s picture

Title: Allow base tables to define additional cache contexts » Allow views base tables and entity types to define additional cache contexts

.

Wim Leers’s picture

Wim Leers’s picture

#2099137-66: Entity/field access and node grants not taken into account with core cache contexts contains an initial EntityTypeInterface::getListCacheContexts() implementation. I prefer getting just that part in first, since I expect us to get stuck on tricky Views aspects here, as usual :) If you know how to make this happen, feel free to take the interdiff in that patch and reuse it here when rolling a patch for Views!

(I sadly don't know Views well enough to roll this patch, so I'm choosing to go for the other issue first because I do know how to make progress there.)

dawehner’s picture

Status: Active » Needs review
FileSize
13.2 KB

Not sure whether actually anything from views <-> cache contexts worked.

Status: Needs review » Needs work

The last submitted patch, 4: 2445743-4.patch, failed testing.

dawehner’s picture

Status: Needs work » Needs review
FileSize
13.46 KB
2.89 KB

I guess some earlier test coverage would have been nice ;)

Wim Leers’s picture

FileSize
13.46 KB

The current patch doesn't apply cleanly anymore (but it does apply with git apply -3), so straight reroll. Please don't credit me.


This patch works perfectly! I'd RTBC, but I have one important question and a minor one.

  1. +++ b/core/modules/views/config/schema/views.data_types.schema.yml
    @@ -250,16 +250,6 @@ views_display:
    -    cache_metadata:
    
    +++ b/core/modules/views/config/schema/views.schema.yml
    @@ -114,6 +114,18 @@ views.view.*:
    +            cache_metadata:
    

    Why did this have to be moved? It's still at the display level though. Basically: I don't understand these config schemas and therefore don't understand why it's moved.

  2. +++ b/core/modules/views/src/Entity/View.php
    @@ -310,13 +310,14 @@ protected function addCacheMetadata() {
    -    foreach ($displays as $display_id => $display) {
    +    foreach (array_keys($displays) as $display_id) {
    +      $display =& $this->getDisplay($display_id);
    

    Oh heh… I guess this means that it never actually worked as expected?

Wim Leers’s picture

Oh, the IS also mentions as well as in list builder — which is indeed necessary. Do you want to do that in this issue, or in another?

dawehner’s picture

FileSize
16.59 KB
3.13 KB

Why did this have to be moved? It's still at the display level though. Basically: I don't understand these config schemas and therefore don't understand why it's moved.

Well, the reason is kinda simply, it used to be defined on the wrong place. It was placed under:

$view->display->default->display_options['cache_metadata']

.
The actual code though places it in:

$view->display->default->cache_metadata

. (yes that is pseudocode, and yes, it never worked :))

Oh heh… I guess this means that it never actually worked as expected?

It did not worked in multiple possible ways.

Oh, the IS also mentions as well as in list builder — which is indeed necessary. Do you want to do that in this issue, or in another?

Let's do it.

Wim Leers’s picture

Status: Needs review » Reviewed & tested by the community

Wonderful — thank you!

Fabianx’s picture

RTBC + 1

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 9: 2445743-9.patch, failed testing.

Wim Leers’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
16.56 KB

#2414539: Simplify schema definition for sequence broke this. Straight reroll. Please don't credit me.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php
@@ -378,6 +378,8 @@ public function testReferencedEntity() {
+    // @todo verify <EntityType>::getListCacheTags() are present, blocked on
+    //    https://www.drupal.org/node/2445761

@@ -385,6 +387,8 @@ public function testReferencedEntity() {
+    // @todo verify <EntityType>::getListCacheTags() are present, blocked on
+    //    https://www.drupal.org/node/2445761

The issue has landed.

Wim Leers’s picture

Right, yay! I'm happy to do this unless dawehner wants to roll it.

Wim Leers’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
18.1 KB
3.28 KB

Fixed those @todos.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 16: 2445743-16.patch, failed testing.

Wim Leers’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
19.44 KB
1.83 KB
Fabianx’s picture

Back to RTBC + 2, looks great!

dawehner’s picture

Status: Reviewed & tested by the community » Needs work

Working on a reroll + expanding test coverage on the frontpage.

dawehner’s picture

Status: Needs work » Needs review
FileSize
605 bytes
19.38 KB

This seems to be all which is needed.

Status: Needs review » Needs work

The last submitted patch, 21: 2445743-21.patch, failed testing.

dawehner’s picture

Status: Needs work » Needs review

Fixed the remaining failures.

dawehner’s picture

FileSize
771 bytes
20.14 KB

Fixed the remaining failures.

Fabianx’s picture

Status: Needs review » Reviewed & tested by the community

Oh, how nice to see your cache contexts, views :).

Back to RTBC!

Wim Leers’s picture

:) This is why I already added cache contexts in #2381217: Views should set cache tags on its render arrays, and bubble the output's cache tags to the cache items written to the Views output cache — because that issue was chiefly about cache tags, but in spirit it was about bubbling in general. So I used cache context merging already there. That's why #21 + #24 are both single-line test fixes. Great to see it come together like that :)

alexpott’s picture

Status: Reviewed & tested by the community » Fixed
diff --git a/core/modules/node/src/Tests/NodeListBuilderTest.php b/core/modules/node/src/Tests/NodeListBuilderTest.php
index beadf0e..d2e5218 100644
--- a/core/modules/node/src/Tests/NodeListBuilderTest.php
+++ b/core/modules/node/src/Tests/NodeListBuilderTest.php
@@ -6,6 +6,7 @@
  */
 
 namespace Drupal\node\Tests;
+
 use Drupal\simpletest\KernelTestBase;
 
 /**
@@ -26,7 +27,9 @@ protected function setUp() {
     $this->installEntitySchema('node');
   }
 
-
+  /**
+   * Tests that the correct cache contexts are set.
+   */
   public function testCacheContexts() {
     /** @var \Drupal\Core\Entity\EntityListBuilderInterface $list_builder */
     $list_builder = $this->container->get('entity.manager')->getListBuilder('node');
diff --git a/core/modules/system/src/Tests/Entity/EntityListBuilderTest.php b/core/modules/system/src/Tests/Entity/EntityListBuilderTest.php
index 851fed8..58da00f 100644
--- a/core/modules/system/src/Tests/Entity/EntityListBuilderTest.php
+++ b/core/modules/system/src/Tests/Entity/EntityListBuilderTest.php
@@ -56,6 +56,9 @@ public function testPager() {
     $this->assertRaw('Test entity 51', 'Test entity 51 is shown.');
   }
 
+  /**
+   * Tests that the correct cache contexts are set.
+   */
   public function testCacheContexts() {
     /** @var \Drupal\Core\Entity\EntityListBuilderInterface $list_builder */
     $list_builder = $this->container->get('entity.manager')->getListBuilder('entity_test');

Added missing code docblocks.

This issue addresses a major bug and is allowed per https://www.drupal.org/core/beta-changes. Committed a87aff8 and pushed to 8.0.x. Thanks!

  • alexpott committed a87aff8 on 8.0.x
    Issue #2445743 by dawehner, Wim Leers: Allow views base tables and...

  • alexpott committed 6dfbcdd on 8.0.x
    Revert "Issue #2445743 by dawehner, Wim Leers: Allow views base tables...
alexpott’s picture

Status: Fixed » Needs review

alexpott queued 24: 2445743-23.patch for re-testing.

Status: Needs review » Needs work

The last submitted patch, 24: 2445743-23.patch, failed testing.

dawehner’s picture

Status: Needs work » Needs review
FileSize
20.29 KB
2.03 KB

Oh damn, a commit conflict. This is a fix.

Fabianx’s picture

Status: Needs review » Reviewed & tested by the community

Back to RTBC, ouch on the conflict. Do we have a tag for potentially conflicting things?

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Let's try again. Committed 0fe1e7d and pushed to 8.0.x. Thanks!

This type of commit conflict is kind of avoidable unless patch author's decide to postpone one of the issues on the other or the rtbc retest gets in first. Which is one of the problems with a long rtbc queue.

  • alexpott committed 0fe1e7d on 8.0.x
    Issue #2445743 by dawehner, Wim Leers: Allow views base tables and...
Wim Leers’s picture

This unblocked #2381277: Make Views use render caching and remove Views' own "output caching" and #2099137: Entity/field access and node grants not taken into account with core cache contexts, thanks!

#34: we have the "Avoid commit conflicts" tag, but we generally use it only for patches that are very painful to reroll.

Status: Fixed » Closed (fixed)

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