-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move the cacheContexts and cacheTags to the alert banner entity #152
Conversation
Fix #150 - Remove the session and user role contexts - Move cache contexts to alert banner entity - Move cache tags to alert banner entity
Initial fix. I was a little confused at the cacheContexts in the alertbanner entity. Should I be adding them via |
Code looks good Andy. I will try it and get back to you. Thanks a lot for the prompt action :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@andybroomfield I've made a few comments. I'm not an expert on Drupal cache contexts and tags, so am happy to be overidden on some of the suggested changes. But having thought it through I'm not sure when I originally added code to the getCacheContexts
method it was the best place for it. It may be better being called by the constructor. Happy to discuss further.
src/Entity/AlertBannerEntity.php
Outdated
@@ -392,9 +392,12 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { | |||
*/ | |||
public function getCacheContexts() { | |||
|
|||
$contexts = []; | |||
$contexts = Cache::mergeContexts($contexts, ['url.path.is_front']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this line is necessary. Cache contexts are just strings so you can initialise it with $contexts = ['url.path.is_front'];
src/Entity/AlertBannerEntity.php
Outdated
@@ -406,7 +409,21 @@ public function getCacheContexts() { | |||
$this->addCacheContexts($contexts); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we're setting cache contexts outside the visibility field check I think this line should be moved out of the check block. Although this might not be needed at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although this might not be needed at all.
Seem to be correct.
src/Entity/AlertBannerEntity.php
Outdated
|
||
// Get token and use as a cache tag. | ||
$token = $this->getToken(); | ||
$cache_tags = Cache::mergeTags($cache_tags, ['localgov.alert.banner.token:' . $token]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be simplified to $cache_tags = ['localgov.alert.banner.token:' . $token];
Hi Andy, The In contrast, when I changed the visibility rule to only display that alert on the homepage, there were only two cache keys in the cache_render table irrespective of how many pages I visited. That's good news :) Given all this, I think I can live with PR. We are not even using any Visibility rule in our site given that this is so new :) |
By the way, I noticed something very odd about the Visibility rule. It is showing up as part of the Alert banner! I have attached a screenshot: Possibly caused by this piece of config |
@Adnan-cds I noticed that and thought I upgraded wrong so removed it in managed display. |
Thanks Andy :) |
a68d4d4
to
a8b59b0
Compare
Have now updated this branch. |
…h parent - This follows what Drupal core / modules do
a8b59b0
to
06ab96b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am happy with what I am seeing.
@stephen-cox are you happy with this change also, as its your request that is blocking? |
Fix #150