Editing menu results in 500 error

Created on 25 February 2025, about 1 month ago

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.

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.

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

šŸ› Bug report
Status

Active

Version

10.5 āœØ

Component

menu.module

Created by

šŸ‡¬šŸ‡§United Kingdom Jons

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @Jons
  • 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.

  • šŸ‡¬šŸ‡§United Kingdom Jons

    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 nd_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)
    it 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)

  • 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?

  • šŸ‡¬šŸ‡§United Kingdom Jons

    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"),
    ...
  • šŸ‡³šŸ‡æNew Zealand quietone

    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 ā†’ .

  • šŸ‡¬šŸ‡§United Kingdom Jons

    šŸ‘

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

  • šŸ‡¬šŸ‡§United Kingdom Jons

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

    nu(['id'=>'mymenuid', 'label'=>'mymenulabel'], 'menu');
    $renderable = [
    '#theme' => 'my_template',
    '#test_var' => $m,
    ];
    return \Drupal::service('renderer')->renderPlain($renderable);

  • šŸ‡¬šŸ‡§United Kingdom Jons
  • 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.

  • šŸ‡¬šŸ‡§United Kingdom Jons
  • šŸ‡¬šŸ‡§United Kingdom Jons
  • @jons I am setting these issue values

  • šŸ‡®šŸ‡³India shivam_tiwari

    I am working here.

  • Merge request !11629Editing menu results in 500 error. ā†’ (Open) created by shivam_tiwari
  • Pipeline finished with Failed
    12 days ago
    Total: 136s
    #458009
  • šŸ‡®šŸ‡³India shivam_tiwari

    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.

  • šŸ‡§šŸ‡·Brazil charlliequadros

    I'll work on it

  • Pipeline finished with Failed
    8 days ago
    Total: 636s
    #460810
  • šŸ‡§šŸ‡·Brazil charlliequadros

    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.

Production build 0.71.5 2024