-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix \Knp\Menu\MenuItem @label issue #6031
Conversation
Thank you @maxplatonov! |
Do you think this behaviour should be reported to twig repository ? IMHO I expect |
Hello @VincentLanglet, I suppose it would be wrong to expect that. As per Twig (2, 3) documentation:
⬇️ This is where \ArrayAccess comes into play.
⬆️ https://github.com/twigphp/Twig/blob/2.x/src/Extension/CoreExtension.php#L1340-L1346
IMHO Twig's behaviour may change in the future, I would rely on the getter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you want to add a test @phansys ?
I think a functional test should be enough to cover the exposed issue. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @maxplatonov.
I've left a comment about the test, since I'd like to focus the effort on ensuring the issue is solved with our real codebase.
Let me try to add a functional test @maxplatonov to see how hard it is, I'll make you a PR if I'll achieve something. |
Thanks, @franmomu ! |
I've just did the PR adding the test, it actually shows that there is another place to change it in And I guess it means that it will fail in the KnpMenu library. |
Hello @franmomu, |
Could you please rebase your PR and fix merge conflicts? |
## Description: \Knp\Menu\MenuItem in knplabs/knp-menu-bundle implements \ArrayAccess and defines its offsetGet method as follows: ```php /** * Implements ArrayAccess */ public function offsetGet($name) { return $this->getChild($name); } public function getChild($name) { return isset($this->children[$name]) ? $this->children[$name] : null; } ``` It has a property @Label with its getter: ```php /** * Label to output, name is used by default * * @var string|null */ protected $label; public function getLabel() { return (null !== $this->label) ? $this->label : $this->name; } ``` ## Problem : If a MenuItem object has a child with the key `label`, the twig expression `menu.label` will trigger the `offsetGet` method of the \ArrayAccess and return the child instead of the `label` property of the MenuItem object. ## Solution: Call the getter of the `label` property directly: ```twig menu.getLabel() ```
This reverts commit ea6fdea.
Hello, @franmomu, Could you please review? I rebased the branch on the upstream. I had to restore the Thank you. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! You could do a PR to fix it in KnpMenu as well if you feel like doing it!
Co-authored-by: Javier Spagnoletti <[email protected]>
Co-authored-by: Javier Spagnoletti <[email protected]>
Co-authored-by: Javier Spagnoletti <[email protected]> Co-authored-by: Fran Moreno <[email protected]>
@maxplatonov Please rebase 3.x in order to have working tests. |
@@ -21,11 +21,17 @@ | |||
|
|||
final class ModelManager implements ModelManagerInterface | |||
{ | |||
private $repository; | |||
/** | |||
* @var array array<class-string, RepositoryInterface> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this annotation right? (array array
)
@@ -119,8 +119,9 @@ | |||
{% endblock %} | |||
|
|||
{% block label %} | |||
{{- | |||
item.label|trans( | |||
{# We use method accessor instead of ".label" since `item` implements `ArrayAccess` and could have a property called "label".#} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{# We use method accessor instead of ".label" since `item` implements `ArrayAccess` and could have a property called "label".#} | |
{# We use method accessor instead of ".label" since `item` implements `ArrayAccess` and could have a property called "label". #} |
@@ -47,4 +47,5 @@ | |||
{% endapply %} | |||
{% endblock %} | |||
|
|||
{% block label %}{% if is_link is defined and is_link %}{{ icon|default|raw }}{% endif %}{% if options.allow_safe_labels and item.extra('safe_label', false) %}{{ item.label|raw }}{% else %}{{ item.label|trans({}, translation_domain|default('messages')) }}{% endif %}{% endblock %} | |||
{# We use method accessor instead of ".label" since `item` implements `ArrayAccess` and could have a property called "label".#} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{# We use method accessor instead of ".label" since `item` implements `ArrayAccess` and could have a property called "label".#} | |
{# We use method accessor instead of ".label" since `item` implements `ArrayAccess` and could have a property called "label". #} |
@@ -96,7 +96,8 @@ file that was distributed with this source code. | |||
{% endif %} | |||
|
|||
{%- set translation_domain = menu.extra('translation_domain', 'messages') -%} | |||
{%- set label = menu.label -%} | |||
{# We use method accessor instead of ".label" since `menu` implements `ArrayAccess` and could have a property called "label".#} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{# We use method accessor instead of ".label" since `menu` implements `ArrayAccess` and could have a property called "label".#} | |
{# We use method accessor instead of ".label" since `menu` implements `ArrayAccess` and could have a property called "label". #} |
|
||
final class PR6031Test extends WebTestCase | ||
{ | ||
public function testLabelInShowAction(): void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this test is not performing any direct assertion against the label, we should add a description explaining how it's related.
@maxplatonov Hi ! Do you have some time to finish this PR ? :) |
Could you please rebase your PR and fix merge conflicts? |
Subject
I am targeting this branch, because it is a bug fix.
Changelog
Description:
\Knp\Menu\MenuItem in knplabs/knp-menu-bundle implements \ArrayAccess and defines its offsetGet method as follows:
It has a property
$label
with its getter:Problem :
If a MenuItem object has a child with the key
label
, the twig expressionmenu.label
will trigger theoffsetGet
method of the \ArrayAccess and return the child instead of the$label
property of the MenuItem object.This would result in Catchable Fatal Error: Object of class Knp\Menu\MenuItem could not be converted to string.
Solution:
Call the getter of the
$label
property directly: