Skip to content

Commit

Permalink
Merge pull request #321 from localgovdrupal/fix/1.x/320-banners-loade…
Browse files Browse the repository at this point in the history
…d-unnecessarily

Performance improvements.
  • Loading branch information
finnlewis committed May 14, 2024
2 parents f5a2587 + a6e581d commit 0032959
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/Plugin/Block/AlertBannerBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ class AlertBannerBlock extends BlockBase implements ContainerFactoryPluginInterf
/**
* Current alert banners.
*
* @var \Drupal\localgov_alert_banner\Entity\AlertBannerEntity[]
* This is NULL until ::getCurrentAlertBanners() is called. That populates
* this property and re-uses the result on subsequent calls.
*
* @var ?\Drupal\localgov_alert_banner\Entity\AlertBannerEntity[]
*/
protected $currentAlertBanners = [];
protected $currentAlertBanners = NULL;

/**
* Constructs a new AlertBannerBlock.
Expand All @@ -61,7 +64,6 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->currentUser = $current_user;
$this->currentAlertBanners = $this->getCurrentAlertBanners();
}

/**
Expand Down Expand Up @@ -131,6 +133,7 @@ public function blockSubmit($form, FormStateInterface $form_state) {
* {@inheritdoc}
*/
public function build() {

// Fetch the current published banner.
$published_alert_banners = $this->getCurrentAlertBanners();

Expand All @@ -141,7 +144,7 @@ public function build() {

// Render the alert banner.
$build = [];
foreach ($this->currentAlertBanners as $alert_banner) {
foreach ($published_alert_banners as $alert_banner) {

// Only add to the build if it is visible.
// @see #154.
Expand All @@ -163,7 +166,14 @@ public function build() {
* Array of all published and visible alert banners.
*/
protected function getCurrentAlertBanners() {
$alert_banners = [];

// If this property is an array, it's been populated and we can re-use the
// results.
if (is_array($this->currentAlertBanners)) {
return $this->currentAlertBanners;
}

$this->currentAlertBanners = [];

// Get list of published alert banner IDs.
$types = $this->mapTypesConfigToQuery();
Expand Down Expand Up @@ -196,11 +206,11 @@ protected function getCurrentAlertBanners() {
$alert_banner = $this->entityTypeManager->getStorage('localgov_alert_banner')->load($alert_banner_id);
$is_accessible = $alert_banner->access('view', $this->currentUser);
if ($is_accessible) {
$alert_banners[] = $alert_banner;
$this->currentAlertBanners[] = $alert_banner;
}
}

return $alert_banners;
return $this->currentAlertBanners;
}

/**
Expand All @@ -221,7 +231,7 @@ protected function mapTypesConfigToQuery() : array {
*/
public function getCacheContexts() {
$contexts = [];
foreach ($this->currentAlertBanners as $alert_banner) {
foreach ($this->getCurrentAlertBanners() as $alert_banner) {
$contexts = Cache::mergeContexts($contexts, $alert_banner->getCacheContexts());
}
return Cache::mergeContexts(parent::getCacheContexts(), $contexts);
Expand Down

0 comments on commit 0032959

Please sign in to comment.