Skip to content

Fix undefined method setAttributes() in menu--main.html.twig - #1063

Merged
bboro merged 1 commit into
Gizra:mainfrom
wombatbuddy:fix/undefined-method-setattributes
Aug 25, 2026
Merged

Fix undefined method setAttributes() in menu--main.html.twig#1063
bboro merged 1 commit into
Gizra:mainfrom
wombatbuddy:fix/undefined-method-setattributes

Conversation

@wombatbuddy

Copy link
Copy Markdown
Contributor

What
Replaces the non-existent Attribute::setAttributes() call with Attribute::addClass() in the menu_links_desktop macro of menu--main.html.twig.

- <li{{ item.attributes.setAttributes('class', item_classes) }}>
+ <li{{ item.attributes.addClass(item_classes) }}>

Why addClass() and not setAttribute('class', ...)
addClass() appends to any classes already present on item.attributes, whereas setAttribute('class', ...) would overwrite the whole attribute, wiping out anything set by other code paths.

This also brings menu_links_desktop in line with the mobile macro menu_links, which already uses addClass() correctly for the same purpose — the two macros are now consistent.

The attributes variable is a Drupal\Core\Template\Attribute
object and has no setAttributes() method. Replaced with
addClass() as appropriate.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 with item.attributes.addClass(...) in the menu_links_desktop macro.
  • 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.

@wombatbuddy

Copy link
Copy Markdown
Contributor Author

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.

@amitaibu
amitaibu requested a review from bboro August 25, 2026 08:28
@amitaibu

Copy link
Copy Markdown
Member

Sorry, I thought I'd already marked it for review by @bboro . Did it now 😄

@bboro bboro left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the original intent was setAttribute (without s at the end 😅). But this works too and is semantically better.

@bboro
bboro merged commit 886a0eb into Gizra:main Aug 25, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants