core/lib/Drupal/Core/Entity/EntityType.php | 18 ++++++++++++++++-- core/lib/Drupal/Core/Entity/EntityTypeInterface.php | 11 +++++++++++ core/modules/node/src/Entity/Node.php | 1 + .../src/Tests/Entity/EntityCacheTagsTestBase.php | 4 ++++ .../src/Controller/EntityTestController.php | 5 ++++- 5 files changed, 36 insertions(+), 3 deletions(-) diff --git a/core/lib/Drupal/Core/Entity/EntityType.php b/core/lib/Drupal/Core/Entity/EntityType.php index b23cd9d..727b155 100644 --- a/core/lib/Drupal/Core/Entity/EntityType.php +++ b/core/lib/Drupal/Core/Entity/EntityType.php @@ -203,11 +203,18 @@ class EntityType implements EntityTypeInterface { protected $field_ui_base_route; /** + * The list cache contexts for this entity type. + * + * @var string[] + */ + protected $list_cache_contexts = []; + + /** * The list cache tags for this entity type. * - * @var array + * @var string[] */ - protected $list_cache_tags = array(); + protected $list_cache_tags = []; /** * Constructs a new EntityType. @@ -695,6 +702,13 @@ public function getGroupLabel() { /** * {@inheritdoc} */ + public function getListCacheContexts() { + return $this->list_cache_contexts; + } + + /** + * {@inheritdoc} + */ public function getListCacheTags() { return $this->list_cache_tags; } diff --git a/core/lib/Drupal/Core/Entity/EntityTypeInterface.php b/core/lib/Drupal/Core/Entity/EntityTypeInterface.php index 49302d3..c33c6ec 100644 --- a/core/lib/Drupal/Core/Entity/EntityTypeInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityTypeInterface.php @@ -656,6 +656,17 @@ public function getUriCallback(); public function setUriCallback($callback); /** + * The list cache contexts associated with this entity type. + * + * Enables code listing entities of this type to ensure that rendered listings + * are varied as necessary, typically to ensure users of role A see other + * entities listed as users of role B. + * + * @return string[] + */ + public function getListCacheContexts(); + + /** * The list cache tags associated with this entity type. * * Enables code listing entities of this type to ensure that newly created diff --git a/core/modules/node/src/Entity/Node.php b/core/modules/node/src/Entity/Node.php index 13ad744..85e1bb3 100644 --- a/core/modules/node/src/Entity/Node.php +++ b/core/modules/node/src/Entity/Node.php @@ -45,6 +45,7 @@ * revision_table = "node_revision", * revision_data_table = "node_field_revision", * translatable = TRUE, + * list_cache_contexts = { "node_view_grants" }, * entity_keys = { * "id" = "nid", * "revision" = "vid", diff --git a/core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php b/core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php index 8999167..36383c0 100644 --- a/core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php +++ b/core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php @@ -378,6 +378,8 @@ public function testReferencedEntity() { $this->pass("Test empty listing.", 'Debug'); + // @todo verify ::getListCacheTags() are present, blocked on + // https://www.drupal.org/node/2445761 // Prime the page cache for the empty listing. $this->verifyPageCache($empty_entity_listing_url, 'MISS'); // Verify a cache hit, but also the presence of the correct cache tags. @@ -385,6 +387,8 @@ public function testReferencedEntity() { $this->pass("Test listing containing referenced entity.", 'Debug'); + // @todo verify ::getListCacheTags() are present, blocked on + // https://www.drupal.org/node/2445761 // Prime the page cache for the listing containing the referenced entity. $this->verifyPageCache($nonempty_entity_listing_url, 'MISS'); // Verify a cache hit, but also the presence of the correct cache tags. diff --git a/core/modules/system/tests/modules/entity_test/src/Controller/EntityTestController.php b/core/modules/system/tests/modules/entity_test/src/Controller/EntityTestController.php index bf5fa29..1d6e4e8 100644 --- a/core/modules/system/tests/modules/entity_test/src/Controller/EntityTestController.php +++ b/core/modules/system/tests/modules/entity_test/src/Controller/EntityTestController.php @@ -162,6 +162,7 @@ public function listEntitiesAlphabetically($entity_type_id) { '#items' => $labels, '#title' => $entity_type_id . ' entities', '#cache' => [ + 'contexts' => $entity_type_definition->getListCacheContexts(), 'tags' => $cache_tags, ], ]; @@ -182,11 +183,13 @@ public function listEntitiesAlphabetically($entity_type_id) { * A renderable array. */ public function listEntitiesEmpty($entity_type_id) { + $entity_type_definition = $this->entityManager()->getDefinition($entity_type_id); return [ '#theme' => 'item_list', '#items' => [], '#cache' => [ - 'tags' => $this->entityManager()->getDefinition($entity_type_id)->getListCacheTags(), + 'contexts' => $entity_type_definition->getListCacheContexts(), + 'tags' => $entity_type_definition->getListCacheTags(), ], ]; }