Problem/Motivation
When you implement hook_views_invalidate_cache(), this hook is not invoked when a view is deleted.
In views.api.php you can find the following example hook that states that this hook is invoked when a view is enabled, disabled, created, updated or deleted. Which is not the case.
/**
* Allow modules to respond to the invalidation of the Views cache.
*
* This hook will fire whenever a view is enabled, disabled, created,
* updated, or deleted.
*
* @see views_invalidate_cache()
*/
function hook_views_invalidate_cache() {
\Drupal\Core\Cache\Cache::invalidateTags(['views']);
}
Steps to reproduce
Proposed resolution
Invoke the hook when a view is deleted.
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | 3268872-5-test-only.patch | 1.3 KB | jeroent |
Issue fork drupal-3268872
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
Comment #3
jeroentComment #4
jeroentComment #5
jeroentComment #6
spokje- MR fixes
hook_views_invalidate_cache()not being called when a view is deleted.- MR includes test to prove the above
- Issue contains test-only patch with the above test that fails without the changes in the MR
- Code changes look good to me
- MR makes TestBot happy
RTBC for me.
Comment #7
spokjeRe-queue-ed a test on the MR so that will be the latest test known to Testbot and prevent TestBot from retesting the fail-patch every two days.
INSTA-EDIT (should really drink more coffee before starting hitting the "Save"-button on d.o. issues): The fail-patch Custom Commands failures on
10.0.x-devwith PHP8.0 are because that branch only supports PHP8.1Comment #8
jeroentThanks for the review @Spokje!
It's strange that the first commit, which contains only the test changes, passed the tests and the patch is failing.
Comment #9
spokjeAs far as I can see, the first commit on the MR already contained the actual fix incore/modules/views/src/Entity/View.php, thus making it pass: https://git.drupalcode.org/project/drupal/-/merge_requests/1956/diffs?diff_head=trueThat _is_ weird: https://git.drupalcode.org/project/drupal/-/merge_requests/1956/diffs?di...
Comment #11
spokjeFail test gonna fail, back to RTBC whilst we await the MR being retested.
Comment #12
alexpottWe need to consider what to do with views_view_delete() - that has logic to only rebuild routes if the view has routed displays. We can wither choose to remove it - or we can choose to not call views_invalidate_cache() and call
from
views_view_delete()I'm not sure what's best here.
Comment #14
jeroentI rebased the MR to 9.5.x.
Comment #15
jeroentI was wondering, since
views_invalidate_cache()contains some code to clear the block cache and rebuild the views blocks, that when you delete a view, the blocks on the block layout page weren't updated.But that seems to work fine, so both solutions probably work.
I updated the MR and added the call to
$module_handler->invokeAll('views_invalidate_cache');inviews_view_delete().I'm actually also not sure what the best approach is here. Having 1 function that contains this logic, or copying a part of the logic for performance reasons when saving a view...
Comment #16
lendude@JeroenT asked for my 2ct, so here we go:
I'm not a fan of having entity hooks in the module that implements said entity, so getting rid of views_view_delete() sounds good to me (even if @alexpott and @dawehner apparently likeed this solution more in #2596345: Deleting a view should rebuild the routes ;) ). And calling a hook from inside a hook I also don't like, and invoking a hook from two different places I also don't like.
I would prefer just the added overhead of the extra rebuilding by adding
views_invalidate_cacheto the postDelete method, since that would be the clearer/easier to read/easier to maintain code path. And maybe get a follow up to make it possible to maybe not invalidate everything inviews_invalidate_cachebut add a parameter to indicate if route rebuilding is needed. Something like that?Comment #17
spokjeSurprised nobody have tagged this before now.
Comment #19
smustgrave commentedThis issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.
At this time we will need a D10 MR also.
The MR 1956 also needs to be rerolled.
Comment #22
jeroentI was struggling with rebasing 10.1.x. on the MR, so I created a new one.
Comment #23
smustgrave commentedAdding this to my list to review.
But yes it's usually easier to just make a separate MR for 10.1. Get all kinds of merge conflicts if I try to update a 9.5.x one.
Comment #24
smustgrave commentedTested MR 3332
Not sure how to manually trigger but using the test case without the fix I did get a failure
Failed asserting that false is true.Only nit was for the comment. WIll need in review if anyone doesn't think it's important.
Comment #25
smustgrave commentedComment #26
catchThe only way is to upload a test-only version of the MR as a patch at the moment.
A follow-up to make views_invalidate_cache() more precise sounds good, but I think this is fine as it is.
Committed/pushed to 10.1.x, thanks!
Comment #27
jeroent@catch,
I think something went wrong with the commit?
Comment #29
catchOoops, forgot to push I think. Should be there now!