Problem/Motivation

If there are N menu links that link to the same path, and that path is active, then each of them get N active classes added.

Proposed resolution

Ensure we find links with the active paths *once*, and then handle the <front> special case *once*. Hence reducing the maximum number of loop iterations to two, instead of N.

Remaining tasks

  1. Review

User interface changes

None

API changes

None

Original report by @cmatter

Multiple Menulinks to Frontpage make a lot of class atributes "active" only when you are not logged in.

<a href="/" data-drupal-link-system-path="<front>" class="active active active active active active active active active active active active active active active active active active active active active active active active active active active active active active active active active active active active active">Abo</a>

Comments

cmatter’s picture

Issue summary: View changes
Status: Needs work » Active
idebr’s picture

Title: multiple Menulinks to <front> add repeated dublicate classe "active" » Active menu links with identical paths get a duplicate "active" class for every active menu link
Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new1023 bytes
new10.7 KB
new12.53 KB

Heh, that is hilarious :)

Added a fix that checks if the class has been added before. I wonder though why each link gets processed multiple times.

Markup before:

Markup after:

berdir’s picture

Issue tags: +Needs tests

We need some tests for this, also wondering if there would be a better way to manage those classes than a string, might make this easier?

wim leers’s picture

Component: menu system » system.module
Priority: Normal » Major
Issue summary: View changes
Issue tags: -Needs tests +Funniest bug of the month, +Performance
StatusFileSize
new965 bytes
new2.91 KB

Tests, and a better fix, that fixes the performance problem that this uncovered, and which the patch in #2 didn't address. No interdiff, because different patch.

Updated IS, to clarify the problem, and the solution in this patch.

wim leers’s picture

Oh, and if that patch is confusing: $type_match was never used in HEAD; hence I only remove it without removing its usages: there were no usages :). I suspect I actually originally planned to do something similar to #4, but then forgot, and nobody noticed and it got committed anyway.

The last submitted patch, 4: 2412241-4-test_only.patch, failed testing.

wim leers’s picture

StatusFileSize
new2.91 KB

Reuploading the #4 patch, because testbot failed to detect it…

Status: Needs review » Needs work

The last submitted patch, 7: 2412241-4.patch, failed testing.

wim leers’s picture

Status: Needs work » Needs review
StatusFileSize
new3.51 KB
new965 bytes
new2.54 KB

#4's test was fine, but its implementation changes were utterly wrong. This should be much better.

Status: Needs review » Needs work

The last submitted patch, 9: 2412241-8-test_only.patch, failed testing.

wim leers’s picture

Status: Needs work » Needs review

I uploaded them in the wrong order, but the test result speaks for itself. Back to NR.

idebr’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new10.02 KB
new9.78 KB

The updated patch works as advertised, the issue summary update correctly reflects the content of the patch and the feedback of Berdir in #3 has been addressed. Setting this to RTBC.

Markup before:

Markup after:

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/system/src/Controller/SystemController.php
@@ -400,9 +397,11 @@ public static function setLinkActiveClass(array $element, array $context) {
+      $is_active = !in_array('active', explode(' ', $node->getAttribute('class')));

This line reads really weird because if the class has the active class $is_active equals FALSE. I think we should change the variable name to $add_active. Also I think we should store the result of $node->getAttribute('class') in a variable since we have to do this again later.

idebr’s picture

Status: Needs work » Needs review
StatusFileSize
new965 bytes
new1.92 KB
new4.23 KB

Thanks for the feedback, alexpott. I have updated the patch accordingly.

The last submitted patch, 14: 2412241-14.fail_.patch, failed testing.

berdir’s picture

Status: Needs review » Reviewed & tested by the community

I think that addresses @alexpott's feedback correctly, agreed that $add_active is a better name.

wim leers’s picture

+1

alexpott’s picture

Title: Active menu links with identical paths get a duplicate "active" class for every active menu link » Active menu links with identical paths get a duplicate "active" class for every active menu link
Status: Reviewed & tested by the community » Fixed

This issue addresses a major bug and is allowed per https://www.drupal.org/core/beta-changes. Committed a5bbb06 and pushed to 8.0.x. Thanks!

  • alexpott committed a5bbb06 on 8.0.x
    Issue #2412241 by idebr, Wim Leers: Active menu links with identical...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.