From edaa5a5c1b43ad0cd7d9d754bf32437dfdd26133 Mon Sep 17 00:00:00 2001 From: Rupert Jabelman Date: Thu, 2 May 2024 11:20:34 +0100 Subject: [PATCH 1/2] Stopped block constructor loading entities. Changed getCurrentAlertBanners method so it can be called multiple times without re-loading banners. --- src/Plugin/Block/AlertBannerBlock.php | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/Plugin/Block/AlertBannerBlock.php b/src/Plugin/Block/AlertBannerBlock.php index 248e3e9..4abe7f3 100644 --- a/src/Plugin/Block/AlertBannerBlock.php +++ b/src/Plugin/Block/AlertBannerBlock.php @@ -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. @@ -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(); } /** @@ -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(); @@ -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. @@ -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(); @@ -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; } /** @@ -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); From a6e581dd1a66a97a76297ce09021a5c227f4e80c Mon Sep 17 00:00:00 2001 From: Rupert Jabelman Date: Thu, 2 May 2024 11:26:55 +0100 Subject: [PATCH 2/2] Code style. --- src/Plugin/Block/AlertBannerBlock.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Plugin/Block/AlertBannerBlock.php b/src/Plugin/Block/AlertBannerBlock.php index 4abe7f3..c8bdbb8 100644 --- a/src/Plugin/Block/AlertBannerBlock.php +++ b/src/Plugin/Block/AlertBannerBlock.php @@ -39,12 +39,12 @@ class AlertBannerBlock extends BlockBase implements ContainerFactoryPluginInterf /** * Current alert banners. * - * This is null until ::getCurrentAlertBanners() is called. That populates + * 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 = null; + protected $currentAlertBanners = NULL; /** * Constructs a new AlertBannerBlock.