From 993e8b8532048f2edcea38f2aa07cdc2a90eb3d9 Mon Sep 17 00:00:00 2001 From: Alexey Date: Wed, 29 May 2024 17:31:29 +0200 Subject: [PATCH] [DS-1403] Add a fix for better processing alerts on the home page --- openy_node_alert.services.yml | 2 +- src/Service/AlertManager.php | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/openy_node_alert.services.yml b/openy_node_alert.services.yml index c3a1de4..013e379 100644 --- a/openy_node_alert.services.yml +++ b/openy_node_alert.services.yml @@ -1,6 +1,6 @@ services: openy_node_alert.alert_manager: class: 'Drupal\openy_node_alert\Service\AlertManager' - arguments: ['@entity_type.manager', '@current_user', '@path_alias.manager', '@path.matcher'] + arguments: ['@entity_type.manager', '@current_user', '@path_alias.manager', '@path.matcher', '@config.factory'] tags: - { name: service_collector, tag: alert_builder, call: addBuilder } diff --git a/src/Service/AlertManager.php b/src/Service/AlertManager.php index bd3cae1..107ac80 100644 --- a/src/Service/AlertManager.php +++ b/src/Service/AlertManager.php @@ -2,6 +2,7 @@ namespace Drupal\openy_node_alert\Service; +use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Database\Database; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; @@ -68,6 +69,13 @@ class AlertManager { */ protected $connection; + /** + * The alias of the homepage. + * + * @var string + */ + protected $homePageAlias; + /** * Constructs the Alert manager. * @@ -78,12 +86,15 @@ public function __construct( AccountProxyInterface $current_user, AliasManagerInterface $alias_manager, PathMatcherInterface $path_matcher, + ConfigFactoryInterface $config_factory ) { $this->nodeStorage = $entity_type_manager->getStorage('node'); $this->currentUser = $current_user; $this->aliasManager = $alias_manager; $this->pathMatcher = $path_matcher; $this->connection = Database::getConnection(); + $page_front = $config_factory->get('system.site')->get('page.front'); + $this->homePageAlias = $alias_manager->getAliasByPath($page_front); } /** @@ -303,7 +314,7 @@ private function checkVisibility(NodeInterface $alert, NodeInterface $node) { $current_path = $current_path ? mb_strtolower($current_path) : $current_path; // Check all values from the field "alert_visibility_pages". foreach ($pages as $page) { - $page_path = ($page === '' || $page === '/') ? '' : '/' . ltrim($page, '/'); + $page_path = ($page === '' || $page === '/') ? $this->homePageAlias : '/' . ltrim($page, '/'); $is_path_match = $this->pathMatcher->matchPath($current_path, $page_path); if ($is_path_match) { // If this path matches at least one of the visibility pages,