Skip to content

Commit

Permalink
Move the visible check to alert banner manager
Browse files Browse the repository at this point in the history
Fix #354
  • Loading branch information
andybroomfield committed Jul 27, 2024
1 parent 45659ca commit f621db9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
12 changes: 11 additions & 1 deletion src/AlertBannerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function getCurrentAlertBanners(array $options): array {
// Set default options.
$default_options = [
'type' => [],
'check_visible' => FALSE,
];
$options = array_merge($default_options, $options);

Expand Down Expand Up @@ -71,7 +72,7 @@ public function getCurrentAlertBanners(array $options): array {
->execute();

// Load alert banners and add all.
// Visibility check happens in block build, so we get cache contexts on all.
// Visibility check happens separately, so we get cache contexts on all.
if (!empty($published_alert_banners)) {
foreach ($alert_banner_storage->loadMultiple($published_alert_banners) as $alert_banner) {
$alert_banner = $this->entityRepository->getTranslationFromContext($alert_banner);
Expand All @@ -83,6 +84,15 @@ public function getCurrentAlertBanners(array $options): array {
}
}

// Check visibility if specified.
// Should only be when banners are being displayed.
// @see #154.
if ($options['check_visible']) {
$current_alert_banners = array_filter($current_alert_banners, function ($alert_banner) {
return $alert_banner->isVisible();
});
}

return $current_alert_banners;
}

Expand Down
11 changes: 4 additions & 7 deletions src/Plugin/Block/AlertBannerBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public function build() {

$options = [
'type' => $this->mapTypesConfigToQuery(),
'check_visible' => TRUE,
];

// Fetch the current published banner.
Expand All @@ -152,13 +153,8 @@ public function build() {
// Render the alert banner.
$build = [];
foreach ($published_alert_banners as $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);
}
$build[] = $this->entityTypeManager->getViewBuilder('localgov_alert_banner')
->view($alert_banner);
}
return $build;
}
Expand All @@ -183,6 +179,7 @@ public function getCacheContexts() {
$contexts = [];
$options = [
'type' => $this->mapTypesConfigToQuery(),
'check_visible' => FALSE,
];
foreach ($this->alertBannerManager->getCurrentAlertBanners($options) as $alert_banner) {
$contexts = Cache::mergeContexts($contexts, $alert_banner->getCacheContexts());
Expand Down

0 comments on commit f621db9

Please sign in to comment.