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: mymenulabel

Remaining tasks

Apply patch to current and future branches

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

CommentFileSizeAuthor
#5 menu_issue.png79.39 KBjons
#3 menu.patch378 bytesjons

Issue fork drupal-3508884

Command icon 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

jons created an issue. See original summary.

cilefen’s picture

Can 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.

jons’s picture

Issue tags: -Needs error information, -Needs steps to reproduce
StatusFileSize
new378 bytes

The 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)

cilefen’s picture

Could you provide links to those modules needed to reproduce the bug so we can better understand the context of making this change to core?

jons’s picture

StatusFileSize
new79.39 KB

The 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

* @ConfigEntityType(
 *   id = "menu",
 *   label = @Translation("Menu"),
...
quietone’s picture

Version: 10.5.x-dev » 11.x-dev

Changes are made on on 11.x (our main development branch) first, and are then back ported as needed according to the Core change policies.

jons’s picture

👍

cilefen’s picture

Can we get the stack trace and direct and simple steps to reproduce added to the issue summary please?

jons’s picture

@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:

use Drupal\system\Entity\Menu;
....
$m = new Menu(['id'=>'mymenuid', 'label'=>'mymenulabel'], 'menu');
$renderable = [
   '#theme' => 'my_template',
   '#test_var' => $m,
];
return  \Drupal::service('renderer')->renderPlain($renderable);
cilefen’s picture

jons’s picture

courdo’s picture

Status: Active » Postponed (maintainer needs more info)

I 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.

jons’s picture

Issue summary: View changes
jons’s picture

Status: Postponed (maintainer needs more info) » Needs review
cilefen’s picture

Status: Needs review » Needs work
Issue tags: +Needs merge request

@jons I am setting these issue values

shivam_tiwari’s picture

Assigned: Unassigned » shivam_tiwari

I am working here.

shivam_tiwari’s picture

Assigned: shivam_tiwari » Unassigned
Issue tags: -Needs merge request

This 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.

charlliequadros’s picture

I'll work on it

charlliequadros’s picture

Assigned: Unassigned » charlliequadros
charlliequadros’s picture

I 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.

charlliequadros’s picture

Assigned: charlliequadros » Unassigned
charlliequadros’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Seems like something that needs test coverage.

dcam’s picture

Title: Editing menu results in 500 error » Add \Drupal\system\Entity\Menu::toString()
Category: Bug report » Feature request
Issue tags: +Needs subsystem maintainer review

This 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.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.