Skip to content
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

Merged
merged 3 commits into from
Jul 5, 2021

Conversation

andybroomfield
Copy link
Contributor

Fix #150

  • Remove the session and user role contexts
  • Move cache contexts to alert banner entity
  • Move cache tags to alert banner entity

Fix #150

- Remove the session and user role contexts
- Move cache contexts to alert banner entity
- Move cache tags to alert banner entity
@andybroomfield
Copy link
Contributor Author

Initial fix.
Let me know if this works for you @Adnan-cds.

I was a little confused at the cacheContexts in the alertbanner entity. Should I be adding them via $this->addCacheContexts($contexts); or merging them with the parent tags as in return Cache::mergeContexts(parent::getCacheContexts(), $contexts);

@Adnan-cds
Copy link
Contributor

Code looks good Andy. I will try it and get back to you. Thanks a lot for the prompt action :)

Copy link
Member

@stephen-cox stephen-cox left a 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.

@@ -392,9 +392,12 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
*/
public function getCacheContexts() {

$contexts = [];
$contexts = Cache::mergeContexts($contexts, ['url.path.is_front']);
Copy link
Member

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'];

@@ -406,7 +409,21 @@ public function getCacheContexts() {
$this->addCacheContexts($contexts);
Copy link
Member

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.

Copy link
Contributor

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 Show resolved Hide resolved

// Get token and use as a cache tag.
$token = $this->getToken();
$cache_tags = Cache::mergeTags($cache_tags, ['localgov.alert.banner.token:' . $token]);
Copy link
Member

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];

src/Entity/AlertBannerEntity.php Show resolved Hide resolved
@Adnan-cds
Copy link
Contributor

Hi Andy,
I have finished my testing. It was doing great until I added a Visibility condition that says "Do not display this alert on the homepage". After that, every page started generating a cache entry in the cache render table. Example cache key: entity_view:block:localgov_alert_banner:[languages:language_interface]=en:[theme]=localgov_theme:[url.path]=/1524:[user.permissions]=71...

The url.path cache context may be coming from the condition_field module. In which case we probably can't do much.

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 :)

@Adnan-cds
Copy link
Contributor

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:
screenshot-alert-banner-with-visibility-rule

Possibly caused by this piece of config

@andybroomfield
Copy link
Contributor Author

@Adnan-cds I noticed that and thought I upgraded wrong so removed it in managed display.
I'd start a new PR that moves that config to disabled

@Adnan-cds
Copy link
Contributor

I'd start a new PR that moves that config to disabled

Thanks Andy :)

@andybroomfield
Copy link
Contributor Author

Have now updated this branch.
Following @Adnan-cds suggestion, we're using the method that Drupal core and other modules use, which is to merge the context provided by the alert banner entity with that provided by the parent::getCacheContexts. This should still allow other modules to add custom cache contexts and tags as they will still be retrived from entityBase.php

…h parent

- This follows what Drupal core / modules do
Copy link
Contributor

@Adnan-cds Adnan-cds left a 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.

@andybroomfield
Copy link
Contributor Author

@stephen-cox are you happy with this change also, as its your request that is blocking?

@andybroomfield andybroomfield merged commit 3e40ca8 into 1.x Jul 5, 2021
@andybroomfield andybroomfield mentioned this pull request Jul 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Do we need the "session" cache context?
3 participants