Problem/Motivation
Clicking edit icon on menu item in a page's (contrib) tb_megamenu menu block results in progress arrow continuously and browser console show AJAX call had returned HTTP 500. Expanding this shows exception text: 'Object of type ...Entity\Menu cannot be printed.'
Steps to reproduce
Clicking edit icon on menu item in a page's menu block.
To reproduce standalone:
Set up hook_theme() in a test .module and twig template as described in https://www.drupal.org/docs/develop/theming-drupal/twig-in-drupal/create...
Add to test module Controller:
use Drupal\system\Entity\Menu;
....
$m = new Menu(['id'=>'mymenuid', 'label'=>'mymenulabel'], 'menu');
return [
'#theme' => 'my_template',
'#test_var' => $m,
];
Calling controller will give the:
Exception: Object of type Drupal\system\Entity\Menu cannot be printed. in Drupal\Core\Template\TwigExtension->escapeFilter() (line 459 of core/lib/Drupal/Core/Template/TwigExtension.php).
__TwigTemplate_570b34ddc31fd2054d7b637ea3a5d243->doDisplay() (Line: 388)
....Proposed resolution
The Exception is thrown in core/lib/Drupal/Core/Template/TwigExtension.php. This filter looks for a toString() etc method on the Menu entity (in core/modules/system/src/Entity/Menu.php)
Resolve by adding a simple toString() method to Menu entity, returning the label or id property (see patch).
Rerun test above, and you should see
Test twig template!
test_var: mymenulabelRemaining tasks
Apply patch to current and future branches
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | menu_issue.png | 79.39 KB | jons |
| #3 | menu.patch | 378 bytes | jons |
Issue fork drupal-3508884
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
cilefen commentedCan we get the exception message and the stack trace added to this report? 🙏
I am adding the "needs steps to reproduce" tag because the current steps are a common action.
Comment #3
jons commentedThe error text is seen in the AJAX response and has no stack trace and is similar in the php error log:
"The website encountered an unexpected error. Try again later.
Exception: Object of type Drupal\system\Entity\Menu cannot be printed. in Drupal\Core\Template\TwigExtension->escapeFilter() (line 459 of core/lib/Drupal/Core/Template/TwigExtension.php)."
Reproduce: It occurs in the module tb_megamenu using module nd_visualshortcodes which at some point includes a template with the twig processing. Some code would be needed to mock this up - but the core code: (core/lib/Drupal/Core/Template/TwigExtension.php) expects there to be a toRenderable() or __toString() or toString() method on any Object that's passed - so why not just add it? (see patch file)
Comment #4
cilefen commentedCould you provide links to those modules needed to reproduce the bug so we can better understand the context of making this change to core?
Comment #5
jons commentedThe main module is https://www.drupal.org/project/tb_megamenu
nd_visualshortcodes is a custom module - part of a theme package.
The menu is in a block and when edited looks like the attached image (menu_issue.png). You click the edit icon in the + Menu strip to get the problem.
Hopefully all Entities will have a toString() method if there is any chance they will be used in Twig templates. Maybe a change the interface or the Twig filter to add a method to get the Entity metadata ('configuration entity class')?
eg in core/modules/system/src/Entity/Menu.php
Comment #6
quietone commentedChanges are made on on 11.x (our main development branch) first, and are then back ported as needed according to the Core change policies.
Comment #7
jons commented👍
Comment #8
cilefen commentedCan we get the stack trace and direct and simple steps to reproduce added to the issue summary please?
Comment #9
jons commented@cilefen the background is explained above - but if you really want a stack trace just pass a Menu entity to a twig template and you'll get one, eg In a controller:
Comment #10
cilefen commentedComment #11
jons commentedComment #12
courdo commentedI have reviewed this issues as part of the Drupal South (Melbourne) code sprint and I can not reproduce the reported issue without the steps to reproduce. Please provide step to reproduce.
Comment #13
jons commentedComment #14
jons commentedComment #15
cilefen commented@jons I am setting these issue values
Comment #16
shivam_tiwari commentedI am working here.
Comment #18
shivam_tiwari commentedThis issue found for me also after adding controller as described on #9. Created MR 11629 to resolve this issue. After adding this MR as a patch issue is resolved but pipeline is failing for PHP static analysis, so need to work on this.
Comment #19
charlliequadros commentedI'll work on it
Comment #20
charlliequadros commentedComment #21
charlliequadros commentedI fixed the PHPStan error, but now I'm encountering an issue with Nightwatch. I’m not sure how to test it locally to resolve it. If anyone can help me with the steps I need to take, I’d be happy to fix it as well.
Comment #22
charlliequadros commentedComment #23
charlliequadros commentedComment #24
smustgrave commentedSeems like something that needs test coverage.
Comment #25
dcam commentedThis is a feature request masquerading as a bug report. A problem caused by custom code trying to implement something that isn't supported by Drupal Core does not mean that there is a bug in Core. Thus, I am changing the issue category and title.
I'm tagging the issue as needing subsystem maintainer review to get buy-in from them (or not) for this new feature.