diff --git a/src/Entity/FileEntity.php b/src/Entity/FileEntity.php index f4277d3..0d55db4 100644 --- a/src/Entity/FileEntity.php +++ b/src/Entity/FileEntity.php @@ -489,16 +489,6 @@ class FileEntity extends File implements FileEntityInterface { return empty($types) ? NULL : reset($types); } - /** - * {@inheritdoc} - */ - public function getCacheTags() { - // Assume that files are only embedded in other entities and don't have - // their own cache tags. - // @todo Make this configurable. - return []; - } - /** * {@inheritdoc} */ @@ -511,45 +501,4 @@ class FileEntity extends File implements FileEntityInterface { return parent::validate(); } - /** - * Invalidates an entity's cache tags upon save. - * - * @param bool $update - * TRUE if the entity has been updated, or FALSE if it has been inserted. - */ - protected function invalidateTagsOnSave($update) { - // An entity was created or updated: invalidate its list cache tags. (An - // updated entity may start to appear in a listing because it now meets that - // listing's filtering requirements. A newly created entity may start to - // appear in listings because it did not exist before.) - $tags = $this->getEntityType()->getListCacheTags(); - if ($update) { - // Files don't have their own cache tags, instead, we invalidate cache - // tags of entities that use that file. - foreach (\Drupal::service('file.usage')->listUsage($this) as $module => $module_references) { - foreach ($module_references as $type => $ids) { - if ($this->entityTypeManager()->hasDefinition($type)) { - $tags = Cache::mergeTags($tags, Cache::buildTags($type, array_keys($ids))); - } - } - } - } - Cache::invalidateTags($tags); - } - - /** - * Invalidates an entity's cache tags upon delete. - * - * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type - * The entity type definition. - * @param \Drupal\Core\Entity\EntityInterface[] $entities - * An array of entities. - */ - protected static function invalidateTagsOnDelete(EntityTypeInterface $entity_type, array $entities) { - $tags = $entity_type->getListCacheTags(); - // We only invalidate cache tags of entities using the file. If a file is - // deleted, we assume that it is no longer used. - Cache::invalidateTags($tags); - } - } diff --git a/src/Entity/FileEntityViewBuilder.php b/src/Entity/FileEntityViewBuilder.php index cba068d..27a6847 100644 --- a/src/Entity/FileEntityViewBuilder.php +++ b/src/Entity/FileEntityViewBuilder.php @@ -14,25 +14,4 @@ use Drupal\image\Entity\ImageStyle; */ class FileEntityViewBuilder extends EntityViewBuilder { - /** - * {@inheritdoc} - */ - protected function getBuildDefaults(EntityInterface $entity, $view_mode) { - $build = parent::getBuildDefaults($entity, $view_mode); - - // We suppress file entity cache tags, because they are almost exclusively - // embedded in other pages, except when viewed as a standalone page. To - // support cache invalidations on those, we pick the first cache tag from - // the references and add that. - // @todo Make this available as a method? - foreach (\Drupal::service('file.usage')->listUsage($entity) as $module => $module_references) { - foreach ($module_references as $type => $ids) { - if (\Drupal::entityTypeManager()->hasDefinition($type)) { - $build['#cache']['tags'] = Cache::mergeTags($build['#cache']['tags'], array($type . ':' . key($ids))); - break 2; - } - } - } - return $build; - } } diff --git a/tests/src/Functional/FileEntityCacheTagsTest.php b/tests/src/Functional/FileEntityCacheTagsTest.php index 366db9e..5652f3b 100644 --- a/tests/src/Functional/FileEntityCacheTagsTest.php +++ b/tests/src/Functional/FileEntityCacheTagsTest.php @@ -103,6 +103,7 @@ class FileEntityCacheTagsTest extends FileEntityTestBase { // Check cache tags. $contexts = ['languages:language_interface', 'user.permissions', 'theme', 'timezone', 'url.query_args:_wrapper_format', 'user.roles:anonymous', 'url.site']; $this->assertPageCacheContextsAndTags($node1->toUrl(), $contexts, [ + 'file:1', 'node:' . $node1->id(), 'node_view', 'rendered', @@ -112,6 +113,8 @@ class FileEntityCacheTagsTest extends FileEntityTestBase { 'http_response', ]); $this->assertPageCacheContextsAndTags($node2->toUrl(), $contexts, [ + 'file:1', + 'file:2', 'node:' . $node2->id(), 'node_view', 'rendered', @@ -121,6 +124,7 @@ class FileEntityCacheTagsTest extends FileEntityTestBase { 'http_response', ]); $this->assertPageCacheContextsAndTags($node3->toUrl(), $contexts, [ + 'file:2', 'node:' . $node3->id(), 'node_view', 'rendered',