From efc0e2e41df7be095b954e8cc9648b0ad3f3e8b9 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Sat, 14 Feb 2015 16:41:03 +0000
Subject: [PATCH] Issue #2403301 by lokapujya, penyaskito, Wim Leers: Menu item
 "active" class is not correctly added when using a view as the frontpage

---
 .../src/Controller/SystemController.php       | 14 ++++++++++----
 .../Unit/Controller/SystemControllerTest.php  | 19 +++++++++++++++++++
 2 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/core/modules/system/src/Controller/SystemController.php b/core/modules/system/src/Controller/SystemController.php
index f744936dfe99..73180a087618 100644
--- a/core/modules/system/src/Controller/SystemController.php
+++ b/core/modules/system/src/Controller/SystemController.php
@@ -363,13 +363,19 @@ public static function setLinkActiveClass(array $element, array $context) {
       $pos_current_path = strpos($element['#markup'], $search_key_current_path, $offset);
       $pos_front = strpos($element['#markup'], $search_key_front, $offset);
 
-      // Determine which of the two values matched: the exact path, or the
-      // <front> special case.
+      // Determine which of the two values is the next match: the exact path, or
+      // the <front> special case.
       $pos_match = NULL;
-      if ($pos_current_path !== FALSE) {
+      if ($pos_front === FALSE) {
         $pos_match = $pos_current_path;
       }
-      elseif ($context['front'] && $pos_front !== FALSE) {
+      elseif ($pos_current_path === FALSE) {
+        $pos_match = $pos_front;
+      }
+      elseif ($pos_current_path < $pos_front) {
+        $pos_match = $pos_current_path;
+      }
+      else {
         $pos_match = $pos_front;
       }
 
diff --git a/core/modules/system/tests/src/Unit/Controller/SystemControllerTest.php b/core/modules/system/tests/src/Unit/Controller/SystemControllerTest.php
index ab227a039309..4e6e79174898 100644
--- a/core/modules/system/tests/src/Unit/Controller/SystemControllerTest.php
+++ b/core/modules/system/tests/src/Unit/Controller/SystemControllerTest.php
@@ -305,6 +305,25 @@ public function providerTestSetLinkActiveClass() {
       2 => ['#markup' => '<a data-drupal-link-system-path="&lt;front&gt;" class="active">Once</a> <a data-drupal-link-system-path="&lt;front&gt;" class="active">Twice</a>'],
     ];
 
+    // Test cases to verify that the 'active' class is added when on the front
+    // page, and there are two different kinds of matching links on the page:
+    // - the matching path (the resolved front page path)
+    // - the special matching path ('<front>')
+    $front_special_link = '<a data-drupal-link-system-path="&lt;front&gt;">Front</a>';
+    $front_special_link_active = '<a data-drupal-link-system-path="&lt;front&gt;" class="active">Front</a>';
+    $front_path_link = '<a data-drupal-link-system-path="myfrontpage">Front Path</a>';
+    $front_path_link_active = '<a data-drupal-link-system-path="myfrontpage" class="active">Front Path</a>';
+    $data[] = [
+      0 => ['#markup' => $front_path_link . ' ' . $front_special_link],
+      1 => ['path' => 'myfrontpage', 'front' => TRUE, 'language' => 'en', 'query' => []],
+      2 => ['#markup' => $front_path_link_active . ' ' . $front_special_link_active],
+    ];
+    $data[] = [
+      0 => ['#markup' => $front_special_link . ' ' . $front_path_link],
+      1 => ['path' => 'myfrontpage', 'front' => TRUE, 'language' => 'en', 'query' => []],
+      2 => ['#markup' => $front_special_link_active . ' ' . $front_path_link_active],
+    ];
+
     return $data;
   }
 
-- 
GitLab