(I honestly don't know if this belongs in Views, Taxonomy, or Cache...)

If a view has an exposed term filter, the options are not updated when new terms are added to the vocabulary. Similar results when deleting or rearranging terms.

Steps to reproduce:

  1. drush si standard --account-pass=admin -y
  2. Ensure page caching is enabled (eg: remove settings.local.php)
  3. Log in and create a view with a page display (path: /test)
  4. Add a "Has taxonomy term" filter, select the "Tags", "Dropdown", and "Show hierarchy in dropdown" options, click "Apply"
  5. Tick the exposed filter option, click Apply, followed by Save
  6. Navigate to the /test, there no options in the filter dropdown as expected
  7. Navigate to /admin/structure/taxonomy/manage/tags/add and add a new term
  8. Navigate to /test (or refresh the tab, if you still have it up)

The newly added term does not appear. The new term appears after clearing caches.

CommentFileSizeAuthor
#70 2900248-70.patch15.17 KBlendude
#70 interdiff-2900248-63-70.txt3.99 KBlendude
#63 interdiff-2900248-59-63.txt4.21 KBmohit_aghera
#63 2900248-63.patch14.94 KBmohit_aghera
#59 2900248-59.patch11.97 KBranjith_kumar_k_u
#58 2900248-nr-bot.txt144 bytesneeds-review-queue-bot
#57 2900248-57.patch11.93 KB_utsavsharma
#57 interdiff_56-57.txt639 bytes_utsavsharma
#56 2900248-56.patch11.94 KBrp7
#54 reroll_diff_48_53.txt2.64 KBlammensj
#54 2900248-53.patch11.93 KBlammensj
#48 interdiff-2900248-44-48.txt7.6 KBvakulrai
#48 2900248-48.patch12.03 KBvakulrai
#46 2900248-46.patch11.94 KBvakulrai
#46 interdiff-2900248-44-46.txt7.51 KBvakulrai
#44 interdiff-2900248-36-44.txt1.3 KBvakulrai
#44 2900248-44.patch5.07 KBvakulrai
#36 interdiff-2900248-33-36.txt8.21 KBmohit_aghera
#36 2900248-36.patch11.53 KBmohit_aghera
#36 test-only-2900248-36.patch8.21 KBmohit_aghera
#33 interdiff_31_33.txt1.84 KBanmolgoyal74
#33 2900248-33.patch3.32 KBanmolgoyal74
#31 2900248-31.patch3.31 KBhardik_patel_12
#30 interdiff-27-30.txt832 bytesbkosborne
#30 2900248-30.patch3.31 KBbkosborne
#27 interdiff-26-27.txt741 bytesbkosborne
#27 2900248-27.patch2.49 KBbkosborne
#25 2900248-25.patch2.64 KBmrinalini9
#20 2900248-17.patch2.62 KBkrzysztof domański
#20 interdiff-17-20.txt1.56 KBkrzysztof domański
#17 2900248-16.patch2.58 KBreszli
#16 2900248-16.patch2.58 KBjelle_s
#9 2900248-9.patch2.66 KBlendude
#9 interdiff-2900248-7-9.txt1.24 KBlendude
#7 2900248-7.patch2.42 KBlendude
#7 interdiff-2900248-4-7.txt2.01 KBlendude
#4 2900248-4.patch973 byteslendude
#4 interdiff-2900248-3-4.txt691 byteslendude
#3 2900248-3.patch792 byteslendude

Issue fork drupal-2900248

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

mikeker created an issue. See original summary.

mikeker’s picture

Title: Exposed term filter is not updated when new terms are added (caching issue?) » Exposed term filter is not updated when terms are added, deleted, or rearranged (caching issue?)
Issue summary: View changes

Updated to include deleting and rearranging terms.

lendude’s picture

Status: Active » Needs review
Issue tags: +Needs tests
StatusFileSize
new792 bytes

@mikeker nice find.

This adds the right cachetag when I export the View, but the tag never gets hit when I add a tag...it doesn't even exist in the cachetags table. But this works if I manually invalidate the tag in the database.

Cache tag:

page_1:
display_plugin: page
id: page_1
display_title: Page
position: 1
display_options:
display_extenders: { }
path: test-tags
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url
- url.query_args
- user
- 'user.node_grants:view'
tags:
- 'config:taxonomy.vocabulary.tags'

So I think there might be an issue in Taxonomy too that needs to get fixed before this patch has any effect.

lendude’s picture

StatusFileSize
new691 bytes
new973 bytes

Did some more digging and that tag only get invalidated when you change the actual config of the vocabulary. Apparently adding or changing terms doesn't fall under that.

There doesn't seem to be a vocabulary specific version of the 'taxonomy_term_list' tag, so the only option here would be to add that. Seems like overkill but better then looking at old content I'd guess.

The last submitted patch, 3: 2900248-3.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 4: 2900248-4.patch, failed testing. View results

lendude’s picture

Component: views.module » taxonomy.module
Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new2.01 KB
new2.42 KB

Now with tests and a check if the vocab exists.

lendude’s picture

Issue tags: +VDC

Forgot to tag VDC

lendude’s picture

StatusFileSize
new1.24 KB
new2.66 KB

Now with proper Cache::mergeTags use.

dawehner’s picture

  1. +++ b/core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php
    @@ -385,6 +386,21 @@ public function getCacheContexts() {
    +    // 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;
    

    I think this generic list cache tag is totally fine, especially because terms don't change constantly.

  2. +++ b/core/modules/taxonomy/tests/src/Functional/Views/TaxonomyIndexTidFilterTest.php
    @@ -153,4 +153,21 @@ public function testPostUpdateFunction() {
     
    +  /**
    +   * 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());
    +  }
    +
    

    I think we should have a test which fails without the fix but more from a user point of view, aka. ensure that the terms are always up to date. Do you think this makes sense?

mikeker’s picture

Came across this issue with another project recently and it seemed familiar... :)

I think this generic list cache tag is totally fine, especially because terms don't change constantly.

For the initial fix, I agree. But I'll add a followup to do this on a vocabulary-by-vocabulary basis. Sites that allow freetagging of content (for example) are going to be constantly blowing away their entire taxonomy_term_list cache which will affect other larger vocabularies on that site.

I think we should have a test which fails without the fix but more from a user point of view, aka. ensure that the terms are always up to date. Do you think this makes sense?

Not sure I follow you on this, @dawehner... Do you mean a test that verifies the results of steps #6 and #8 in this IS?

mikeker’s picture

Issue summary: View changes

Minor typo fixes in the IS.

mikeker’s picture

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.

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.

jelle_s’s picture

StatusFileSize
new2.58 KB

Rerolled patch #9

reszli’s picture

StatusFileSize
new2.58 KB

#16 worked fine for me on 8.7
submitting same file to test against 8.7.x-dev

tvalimaa’s picture

Hi, when I added this patch I got watchdog flood problem "More than one Taxonomy terms on node" as relationship for "Term" exposed filter results in Trying to get property of non-object in Drupal\views\ManyToOneHelper->addTable() (issue #2882076). Also I try issue 2882076 patch but that didn't fix watchdog flood problem. After I take this (patch 16) patch off that watchdog flood problem when gone so for me I can't use this patch

So I got this issue and https://www.drupal.org/project/drupal/issues/2882076

krzysztof domański’s picture

Version: 8.6.x-dev » 8.9.x-dev
StatusFileSize
new1.56 KB
new2.62 KB

We can now add a vocabulary specific 'taxonomy_term_list:BUNDLE' tag. See Added an ENTITY_TYPE_list:BUNDLE cache tag.

Limitation: A new ENTITY_TYPE_list:BUNDLE cache tag has been added to 8.9.x-dev.

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

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now 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.

krzysztof domański’s picture

Status: Needs review » Needs work
Issue tags: -VDC +Needs reroll
krzysztof domański’s picture

Issue tags: +VDC
mrinalini9’s picture

Assigned: Unassigned » mrinalini9
mrinalini9’s picture

Assigned: mrinalini9 » Unassigned
Status: Needs work » Needs review
StatusFileSize
new2.64 KB

Rerolled patch for 9.1.x, please review.

krzysztof domański’s picture

Issue tags: -Needs reroll
bkosborne’s picture

StatusFileSize
new2.49 KB
new741 bytes

+1 to this. I removed that comment that is no longer relevant. Patch is against 9.1.x.

bkosborne’s picture

Status: Needs review » Reviewed & tested by the community

BTW, Wim said in #18 he suspects #3067937: Views Exposed Filter Block not inheriting the display handlers cache tags, causing filter options not to appear may fix this. It doesn't I tried it. That issue is separate and relates just to exposed filters in a separate block.

My patch didn't change any code, just removed a comment that was irrelevant, so I feel comfortable RTBCing this.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs update path

This change results in views configuration. This means we need an update path for existing views. For example if you save a view which uses the TaxonomyIndexTid after applying this patch you get the following changes:

diff --git a/views.view.frontpage.yml b/views.view.frontpage.yml
index 732ecd6..1f37621 100644
--- a/views.view.frontpage.yml
+++ b/views.view.frontpage.yml
@@ -307,7 +307,9 @@ display:
         - 'user.node_grants:view'
         - user.permissions
       max-age: -1
-      tags: {  }
+      tags:
+        - 'config:taxonomy.vocabulary.tags'
+        - 'taxonomy_term_list:tags'
   feed_1:
     display_plugin: feed
     id: feed_1
@@ -344,7 +346,9 @@ display:
         - 'user.node_grants:view'
         - user.permissions
       max-age: -1
-      tags: {  }
+      tags:
+        - 'config:taxonomy.vocabulary.tags'
+        - 'taxonomy_term_list:tags'
   page_1:
     display_options:
       path: node
@@ -362,4 +366,6 @@ display:
         - 'user.node_grants:view'
         - user.permissions
       max-age: -1
-      tags: {  }
+      tags:
+        - 'config:taxonomy.vocabulary.tags'
+        - 'taxonomy_term_list:tags'

Fortunately since this is calculated on save I don't think we don't have to do anything with views provided modules - although it might be nice to somehow tell them we have to export. We have ViewsConfigEntityUpdater for this sort of thing. Not sure. Anyhow we can start by adding a post update to find views that use this filter and re-save them.

bkosborne’s picture

Status: Needs work » Needs review
StatusFileSize
new3.31 KB
new832 bytes

Ah yes, that makes sense. Here's the update hook. Maybe it would be better to only find views that are using the tid filter and save those only? Dunno if that's over optimizing.

hardik_patel_12’s picture

StatusFileSize
new3.31 KB

Last patch failed to apply re-rolling for 9.1.x , kindly review.

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.

anmolgoyal74’s picture

StatusFileSize
new3.32 KB
new1.84 KB

Re-rolled for 9.2.x
Also fixed the CS issues and a typo.

lendude’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

I think we still need to address @dawehner's point in #10, we should add a functional test for this, to see that this actually leads to the wanted behaviour.

Not sure if the update path needs testing, but I'm guessing it does even though it's pretty trivial

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.

mohit_aghera’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new8.21 KB
new11.53 KB
new8.21 KB

@Lendude
- I've added a simple test case to validate the options in the views exposed form.
Test only patch is failing on local and it is passing with patch.

The last submitted patch, 36: test-only-2900248-36.patch, failed testing. View results

swentel’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me.

The last submitted patch, 36: test-only-2900248-36.patch, failed testing. View results

The last submitted patch, 36: test-only-2900248-36.patch, failed testing. View results

The last submitted patch, 36: test-only-2900248-36.patch, failed testing. View results

The last submitted patch, 36: test-only-2900248-36.patch, failed testing. View results

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php
    @@ -407,6 +408,19 @@ public function getCacheContexts() {
    +    $tags = Cache::mergeTags($tags, ['taxonomy_term_list:' . $this->options['vid']]);
    

    I think this might be one of the first usages of #2145751: Introduce ENTITY_TYPE_list:BUNDLE cache tag and add it to single bundle listing in core - nice!

  2. +++ b/core/modules/taxonomy/tests/src/Functional/Views/TaxonomyIndexTidUiTest.php
    @@ -258,4 +258,20 @@ public function testExposedUnpublishedFilterOptions() {
    +    $this->drupalGet('test-taxonomy-exposed-filter');
    +    $this->assertSession()->optionExists('edit-tid', 'Term 1.0');
    +    $term = Term::create([
    +      'vid' => 'tags',
    +      'name' => "Test Term",
    +      'parent' => 0,
    +    ]);
    +    $term->save();
    +    $this->drupalGet('test-taxonomy-exposed-filter');
    +    $this->assertSession()->optionExists('edit-tid', 'Test Term');
    

    One thing I'm wondering is should we only be adding the new cache tags if the filter is exposed. If the filter not exposed I don't think this new cache tag gives us anything apart from more invalidations that are unnecessary - since without the filter being exposed there is no list of taxonomy on the resulting view.

  3. +++ b/core/modules/views/views.post_update.php
    @@ -45,6 +45,18 @@ function views_post_update_field_names_for_multivalue_fields(&$sandbox = NULL) {
    +  // Re-save all existing views to force cache tags to be recalculated. This will
    +  // add the missing taxonomy cache tag for views using the taxonomy filter.
    +  $views = \Drupal::entityTypeManager()->getStorage('view')->loadMultiple();
    +  foreach ($views as $view) {
    +    $view->save();
    +  }
    

    This needs to use the \Drupal\Core\Config\Entity\ConfigEntityUpdater to do this as a batch and this should only be re-saving views that use the filter (or a filter that extends it).

vakulrai’s picture

Status: Needs work » Needs review
StatusFileSize
new5.07 KB
new1.3 KB

updating tests for point 3 , raised by @alexpott in #43.

Kindly review.

lendude’s picture

Status: Needs review » Needs work

@vakulrai thanks for looking at this

  • The test view seems to be missing from your patch, so we need to add that back in
  • The update now only checks if there is an exposed filter before running the update, it would be better if it checked if the view is using the taxonomy filter or a plugin extending the taxonomy filter before flagging it for an update
vakulrai’s picture

Status: Needs work » Needs review
StatusFileSize
new7.51 KB
new11.94 KB

Thanks @Lendude for pointing out the missed test file, I mistakenly uploaded the wrong patch.
As suggested I have added one more condition to the post_update, to check if the expose filter is using "taxonomy_index_tid" plugin.

Thanks.

Status: Needs review » Needs work

The last submitted patch, 46: 2900248-46.patch, failed testing. View results

vakulrai’s picture

StatusFileSize
new12.03 KB
new7.6 KB

Added a condition to prevent unwanted warnings in the loop. Please consider this patch.

Thanks.

vakulrai’s picture

Status: Needs work » Needs review
lendude’s picture

Status: Needs review » Needs work

@vakulrai thank you, nice progress, couple of things that I see still:

#43.2 still needs to be addressed, we only want to add this tag if the filter is exposed

#43.3 is partially addressed now, but checking for the plugin ID means we miss all plugins that extend the taxonomy filter, but we also need to update those.

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.

kreatil’s picture

I came across this issue when searching for a resolution of exposed filters being cached although caching is diabled in their underlying views. In my case, I use exposed filters for building search pages (search_api in combination with solr_api_solr). I place these exposed filters as blocks on certain pages.

Sometimes it happens that after resetting all caches at night, these exposed filters are not rendered. Whatever the reason (perhaps system overload). They are then missing on all pages until the next "hard reset" of caches: drush cr. I have actually checked several times that the caching of the underlying views is deactivated.

I am not quite sure about this, but maybe the cause of the problem described here is deeper?

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.

lammensj’s picture

StatusFileSize
new11.93 KB
new2.64 KB

I rerolled the patch for Drupal 9.4.

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.

rp7’s picture

StatusFileSize
new11.94 KB

Rerolled #54 so that it applies cleanly to 9.5.x

_utsavsharma’s picture

Status: Needs work » Needs review
StatusFileSize
new639 bytes
new11.93 KB

Fixed CCF for #56.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new144 bytes

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

ranjith_kumar_k_u’s picture

StatusFileSize
new11.97 KB

Re-rolled #57

ranjith_kumar_k_u’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: -Needs update path +Needs update path tests, +Needs Review Queue Initiative

Taking a look at the patch I see the upgrade path but no tests for it.

devkinetic’s picture

I have a view using database search api that has this issue. Adding/removing terms require a cache flush for them to apply within the exposed filter. This patch doesn't help as it doesn't add cache tags based on taxonomy within the DB search. May be a new issue, but the result is the same.

mohit_aghera’s picture

Status: Needs work » Needs review
Issue tags: -Needs update path tests
StatusFileSize
new14.94 KB
new4.21 KB

- Adding a test case for validating config save of views.
- Resolving merge conflicts as patch was not getting applied on 10.1.x head.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Upgrade tests look good, and fail locally.

  • catch committed d7bd8ac4 on 10.1.x
    Issue #2900248 by Lendude, vakulrai, mohit_aghera, bkosborne, Krzysztof...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed d7bd8ac and pushed to 10.1.x. Thanks!

catch credited luenemann.

catch’s picture

Status: Fixed » Needs work

@luenemann pointed out that the patch didn't address #50 and the update may not catch all cases. I think rather than copying more logic to detect when the view needs to be updated we could instead just load and save every view in the update. Reverted so we can re-commit with that change.

  • catch committed c8beb5de on 10.1.x
    Revert "Issue #2900248 by Lendude, vakulrai, mohit_aghera, bkosborne,...
lendude’s picture

Status: Needs work » Needs review
StatusFileSize
new3.99 KB
new15.17 KB

Made the update hook save all Views and added the 'only when exposed' option to the adding of tags and added test coverage for that.

Let's see what this does.

Local update testing seems to be broken for me, so let's see what the bot thinks....

Status: Needs review » Needs work

The last submitted patch, 70: 2900248-70.patch, failed testing. View results

dww’s picture

Issue tags: +Bug Smash Initiative

Saw this in Slack, tagging to be smashed. 😉 I hope to look more closely soon...

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.

erichhaemmerle’s picture

I am on 10.1.2 and I have this same issue. I have some exposed filters that are taxonomies. If I add or edit a content type that has an entity reference to one of these taxonomies, the taxonomy does not show up in my exposed filter as a choice unless I clear the cache. I just applied the patch in #70, but it did not fix the issue. Do I need to apply the patch from #70 AND #63?

devkinetic’s picture

I haven't been able to work around this issue yet, but I'm still on D9. I ended up installing https://www.drupal.org/project/rebuild_cache_access and instructing my authors of the one and only time to use it, and to schedule that during non peak hours.

I will be on D10 within the next month or so, and can certainly test this again at that point. I'd love to be able to remove that button.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.