Skip to content

Commit

Permalink
Merge pull request #155 from localgovdrupal/fix/154-cache-context-con…
Browse files Browse the repository at this point in the history
…ditons
  • Loading branch information
andybroomfield committed Jul 16, 2021
2 parents a0f2c7d + ab0a712 commit c1ce09c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
12 changes: 12 additions & 0 deletions src/Entity/AlertBannerEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,18 @@ public function getCacheContexts() {
$conditions_config = $this->get('visibility')->getValue()[0]['conditions'];

foreach ($conditions_config as $condition_id => $values) {

// Skip adding contexts for banners that arn't visible.
// @see #154
// Show banner on restricted pages, skip when hidden.
if (!$this->isVisible() && $values['negate'] == 0) {
continue;
}
// Hide banner on restricted pages, skip when visible.
if ($this->isVisible() && $values['negate'] == 1) {
continue;
}

/** @var \Drupal\Core\Condition\ConditionInterface $condition */
$condition = $this->pluginManagerCondition->createInstance($condition_id, $values);
$contexts = Cache::mergeContexts($contexts, $condition->getCacheContexts());
Expand Down
16 changes: 10 additions & 6 deletions src/Plugin/Block/AlertBannerBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,13 @@ public function build() {
// Render the alert banner.
$build = [];
foreach ($this->currentAlertBanners as $alert_banner) {
$build[] = $this->entityTypeManager->getViewBuilder('localgov_alert_banner')
->view($alert_banner);

// Only add to the build if it is visible.
// @see #154.
if ($alert_banner->isVisible()) {
$build[] = $this->entityTypeManager->getViewBuilder('localgov_alert_banner')
->view($alert_banner);
}
}
return $build;
}
Expand Down Expand Up @@ -159,12 +164,11 @@ protected function getCurrentAlertBanners() {
}
$published_alert_banners = $published_alert_banner_query->execute();

// Load alert banners and check they're visible.
// Load alert banners and add all.
// Visibility check happens in build, so we get cache contexts on all.
foreach ($published_alert_banners as $alert_banner_id) {
$alert_banner = $this->entityTypeManager->getStorage('localgov_alert_banner')->load($alert_banner_id);
if ($alert_banner->isVisible()) {
$alert_banners[] = $alert_banner;
}
$alert_banners[] = $alert_banner;
}

return $alert_banners;
Expand Down

0 comments on commit c1ce09c

Please sign in to comment.