KnpLabs / KnpLabs/KnpMenu

Twig Template fails when a child name matches a item's method name

Open
#233 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
PHP
Stars
1.4k
Forks
189
PR merge metrics
No merged PRs in 30d

Description

When using the TwigRenderer, the current implementation of "knp_menu.html.twig" accesses to the methods of an MenuItem by using a property path syntax.

`{{ item.label }}` accesses the `$item->getLabel()` method.

Unfortunately, since ItemInterface extends \ArrayAccess, it is possible that a child name conflicts with a method name. Since \ArrayAccess has priority, it'll not call the intended methods.

For example, by adding a menu such as `$menu->addChild('label', []);`, in the twig template, `{{ item.label }}` will access `$item['label']` instead. It will return an ItemInterface object and fail with a "object cannot be converted to a string" error.

By adding a 'children' item, you could skip part of a menu because it uses the child's children instead of the item's children.

I guess that it should be safer to use method names in the twig template,such as `{{ item.getLabel() }}`, to ensure that the methods are called and not children items.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in knp_menu.html.twig and inspect the TwigRenderer property-path accesses for MenuItem methods. Reproduce menus with children named label and children, then verify rendering uses the intended item values without conversion errors or skipped menu sections.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.