Commit a53ec3d9 authored by alexpott's avatar alexpott

Issue #2560987 by tstoeckler: Active class (almost) always added to frontpage...

Issue #2560987 by tstoeckler: Active class (almost) always added to frontpage links for anonymous users, even on other pages
parent 0fa65eb9
......@@ -134,12 +134,17 @@ public static function setLinkActiveClass($html_markup, $current_path, $is_front
$search_key_current_path = 'data-drupal-link-system-path="' . $current_path . '"';
$search_key_front = 'data-drupal-link-system-path="<front>"';
// An active link's path is equal to the current path, so search the HTML
// for an attribute with that value.
$offset = 0;
// There are two distinct conditions that can make a link be marked active:
// 1. A link has the current path in its 'data-drupal-link-system-path'
// attribute.
// 2. We are on the front page and a link has the special '<front>' value in
// its 'data-drupal-link-system-path' attribute.
while (strpos($html_markup, $search_key_current_path, $offset) !== FALSE || ($is_front && strpos($html_markup, $search_key_front, $offset) !== FALSE)) {
$pos_current_path = strpos($html_markup, $search_key_current_path, $offset);
$pos_front = strpos($html_markup, $search_key_front, $offset);
// Only look for links with the special '<front>' system path if we are
// actually on the front page.
$pos_front = $is_front ? strpos($html_markup, $search_key_front, $offset) : FALSE;
// Determine which of the two values is the next match: the exact path, or
// the <front> special case.
......
......@@ -326,6 +326,42 @@ public function providerTestSetLinkActiveClass() {
5 => $front_special_link_active . ' ' . $front_path_link_active,
];
// Test cases to verify that links to the front page do not get the
// 'is-active' class when not on the front page.
$other_link = '<a data-drupal-link-system-path="otherpage">Other page</a>';
$other_link_active = '<a data-drupal-link-system-path="otherpage" class="is-active">Other page</a>';
$data['<front>-and-other-link-on-other-path'] = [
0 => $front_special_link . ' ' . $other_link,
1 => 'otherpage',
2 => FALSE,
3 => 'en',
4 => [],
5 => $front_special_link . ' ' . $other_link_active,
];
$data['front-and-other-link-on-other-path'] = [
0 => $front_path_link . ' ' . $other_link,
1 => 'otherpage',
2 => FALSE,
3 => 'en',
4 => [],
5 => $front_path_link . ' ' . $other_link_active,
];
$data['other-and-<front>-link-on-other-path'] = [
0 => $other_link . ' ' . $front_special_link,
1 => 'otherpage',
2 => FALSE,
3 => 'en',
4 => [],
5 => $other_link_active . ' ' . $front_special_link,
];
$data['other-and-front-link-on-other-path'] = [
0 => $other_link . ' ' . $front_path_link,
1 => 'otherpage',
2 => FALSE,
3 => 'en',
4 => [],
5 => $other_link_active . ' ' . $front_path_link,
];
return $data;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment