Skip to content

Commit

Permalink
Merge pull request #152 from localgovdrupal/fix/150-cache-tags
Browse files Browse the repository at this point in the history
Move the cacheContexts and cacheTags to the alert banner entity
  • Loading branch information
andybroomfield committed Jul 5, 2021
2 parents 6b5d7a6 + 06ab96b commit 3e40ca8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 27 deletions.
23 changes: 0 additions & 23 deletions localgov_alert_banner.module
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* Contains localgov_alert_banner.module.
*/

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;

/**
Expand All @@ -24,28 +23,6 @@ function localgov_alert_banner_help($route_name, RouteMatchInterface $route_matc
}
}

/**
* Implements hook_entity_build_defaults_alter().
*/
function localgov_alert_banner_entity_build_defaults_alter(array &$build, EntityInterface $entity, $view_mode) {

// Add cache contexts for the alert banner.
if ($entity->bundle() == 'localgov_alert_banner') {

// Set a cache context based on the hide alert banner cookie token.
$build['#cache']['contexts'][] = 'cookies:hide-alert-banner-token';
$build['#cache']['contexts'][] = 'user.roles:anonymous';
$build['#cache']['contexts'][] = 'session';

// Set a cache context based on if on the front page.
$build['#cache']['contexts'][] = 'url.path.is_front';

// Get token and use as a cache tag.
$token = $entity->getToken();
$build['#cache']['tags'][] = 'localgov.alert.banner.token:' . $token;
}
}

/**
* Implements hook_preprocess_localgov_alert_banner().
*/
Expand Down
22 changes: 18 additions & 4 deletions src/Entity/AlertBannerEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,21 +392,35 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
*/
public function getCacheContexts() {

// Add front page context for front page classes.
$contexts = ['url.path.is_front'];

// Add cache contexts depending on the enabled visibility condition plugins.
if ($this->hasField('visibility') && !$this->get('visibility')->isEmpty()) {
$contexts = [];

$conditions_config = $this->get('visibility')->getValue()[0]['conditions'];

foreach ($conditions_config as $condition_id => $values) {
/** @var \Drupal\Core\Condition\ConditionInterface $condition */
$condition = $this->pluginManagerCondition->createInstance($condition_id, $values);
$contexts = Cache::mergeContexts($contexts, $condition->getCacheContexts());
}

$this->addCacheContexts($contexts);
}

return parent::getCacheContexts();
// Return cache contexts.
return Cache::mergeContexts(parent::getCacheContexts(), $contexts);
}

/**
* {@inheritdoc}
*/
public function getCacheTags() {

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

return Cache::mergeTags(parent::getCacheTags(), $cache_tags);
}

}

0 comments on commit 3e40ca8

Please sign in to comment.