diff --git a/core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php b/core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php index a68c3c8c6c..7448023141 100644 --- a/core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php +++ b/core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php @@ -2,6 +2,7 @@ namespace Drupal\taxonomy\Plugin\views\filter; +use Drupal\Core\Cache\Cache; use Drupal\Core\Entity\Element\EntityAutocomplete; use Drupal\Core\Form\FormStateInterface; use Drupal\taxonomy\Entity\Term; @@ -385,6 +386,21 @@ public function getCacheContexts() { /** * {@inheritdoc} */ + public function getCacheTags() { + $tags = parent::getCacheTags(); + $vocabulary = $this->vocabularyStorage->load($this->options['vid']); + if ($vocabulary) { + $tags = Cache::mergeTags($tags, $vocabulary->getCacheTags()); + } + // There is no vocabulary specific tag for 'taxonomy_term_list' so we add + // the generic tag here to catch any updates to terms. + $tags = Cache::mergeTags($tags, ['taxonomy_term_list']); + return $tags; + } + + /** + * {@inheritdoc} + */ public function calculateDependencies() { $dependencies = parent::calculateDependencies(); diff --git a/core/modules/taxonomy/tests/src/Functional/Views/TaxonomyIndexTidFilterTest.php b/core/modules/taxonomy/tests/src/Functional/Views/TaxonomyIndexTidFilterTest.php index 6fd1eba865..389778b92f 100644 --- a/core/modules/taxonomy/tests/src/Functional/Views/TaxonomyIndexTidFilterTest.php +++ b/core/modules/taxonomy/tests/src/Functional/Views/TaxonomyIndexTidFilterTest.php @@ -24,7 +24,7 @@ class TaxonomyIndexTidFilterTest extends TaxonomyTestBase { /** * {@inheritdoc} */ - public static $testViews = ['test_filter_taxonomy_index_tid__non_existing_dependency']; + public static $testViews = ['test_filter_taxonomy_index_tid__non_existing_dependency', 'test_filter_taxonomy_index_tid']; /** * @var \Drupal\taxonomy\TermInterface[] @@ -153,4 +153,21 @@ public function testPostUpdateFunction() { ], $view->getDependencies()); } + /** + * Tests that the cache tags for the chosen vocabulary are added. + */ + public function testGetCacheTags() { + /** @var \Drupal\views\Entity\View $view */ + $view = View::load('test_filter_taxonomy_index_tid'); + $view_executable = $view->getExecutable(); + $view_executable->initDisplay(); + $cache_metadata = $view_executable->getDisplay()->calculateCacheMetadata(); + + $expected_cache_tags = [ + 'config:taxonomy.vocabulary.tags', + 'taxonomy_term_list', + ]; + $this->assertEquals($expected_cache_tags, $cache_metadata->getCacheTags()); + } + }