Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvements. #321

Merged
merged 2 commits into from
May 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading