Fix undefined method setAttributes() in menu--main.html.twig - #1063
Conversation
The attributes variable is a Drupal\Core\Template\Attribute object and has no setAttributes() method. Replaced with addClass() as appropriate.
There was a problem hiding this comment.
Pull request overview
This PR fixes a Twig runtime error in the custom server_theme main menu template by replacing an invalid attribute API call with the correct Drupal/Twig Attribute method, aligning desktop and mobile menu rendering behavior.
Changes:
- Replace the non-existent
item.attributes.setAttributes('class', ...)call withitem.attributes.addClass(...)in themenu_links_desktopmacro. - Ensure menu item classes are appended (not overwritten), matching the existing
menu_links(mobile) macro behavior.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Hi @amitaibu, just a gentle ping on this PR. The Copilot review confirmed the fix for the Twig runtime error in menu--main.html.twig. Whenever you have a moment, could you please take a look or merge it? Thanks. |
|
Sorry, I thought I'd already marked it for review by @bboro . Did it now 😄 |
bboro
left a comment
There was a problem hiding this comment.
I think the original intent was setAttribute (without s at the end 😅). But this works too and is semantically better.
What
Replaces the non-existent
Attribute::setAttributes()call withAttribute::addClass()in themenu_links_desktopmacro ofmenu--main.html.twig.Why
addClass()and notsetAttribute('class', ...)addClass()appends to any classes already present onitem.attributes, whereassetAttribute('class', ...)would overwrite the whole attribute, wiping out anything set by other code paths.This also brings
menu_links_desktopin line with the mobile macromenu_links, which already usesaddClass()correctly for the same purpose — the two macros are now consistent.