Fatal error: Call to undefined method Drupal\Core\Url::toRenderArray()

Created on 27 April 2025, 15 days ago

Problem/Motivation

After installing this module on a Drupal 11 site, as soon as I went to add a config link to a menu I received a fatal error:

Error thrown with message "Call to undefined method Drupal\Core\Url::toRenderArray()"

Stacktrace:
#26 Error in /var/www/html/web/modules/contrib/menu_link_config/src/Plugin/Menu/Form/MenuLinkConfigForm.php:193
#25 Drupal\menu_link_config\Plugin\Menu\Form\MenuLinkConfigForm:form in /var/www/html/web/core/lib/Drupal/Core/Entity/EntityForm.php:107
#24 Drupal\Core\Entity\EntityForm:buildForm in /var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php:528
#23 call_user_func_array in /var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php:528
#22 Drupal\Core\Form\FormBuilder:retrieveForm in /var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php:279
#21 Drupal\Core\Form\FormBuilder:buildForm in /var/www/html/web/core/lib/Drupal/Core/Entity/EntityFormBuilder.php:48
#20 Drupal\Core\Entity\EntityFormBuilder:getForm in /var/www/html/web/modules/contrib/menu_link_config/src/Controller/MenuController.php:29
#19 Drupal\menu_link_config\Controller\MenuController:addLink in /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php:123
#18 call_user_func_array in /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php:123
#17 Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber:Drupal\Core\EventSubscriber\{closure} in /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php:593
#16 Drupal\Core\Render\Renderer:executeInRenderContext in /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php:121
#15 Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber:wrapControllerExecutionInRenderContext in /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php:97
#14 Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber:Drupal\Core\EventSubscriber\{closure} in /var/www/html/vendor/symfony/http-kernel/HttpKernel.php:183
#13 Symfony\Component\HttpKernel\HttpKernel:handleRaw in /var/www/html/vendor/symfony/http-kernel/HttpKernel.php:76
#12 Symfony\Component\HttpKernel\HttpKernel:handle in /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/Session.php:53
#11 Drupal\Core\StackMiddleware\Session:handle in /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php:48
#10 Drupal\Core\StackMiddleware\KernelPreHandle:handle in /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/ContentLength.php:28
#9 Drupal\Core\StackMiddleware\ContentLength:handle in /var/www/html/web/core/modules/big_pipe/src/StackMiddleware/ContentLength.php:32
#8 Drupal\big_pipe\StackMiddleware\ContentLength:handle in /var/www/html/web/core/modules/page_cache/src/StackMiddleware/PageCache.php:116
#7 Drupal\page_cache\StackMiddleware\PageCache:pass in /var/www/html/web/core/modules/page_cache/src/StackMiddleware/PageCache.php:90
#6 Drupal\page_cache\StackMiddleware\PageCache:handle in /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php:48
#5 Drupal\Core\StackMiddleware\ReverseProxyMiddleware:handle in /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php:51
#4 Drupal\Core\StackMiddleware\NegotiationMiddleware:handle in /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php:36
#3 Drupal\Core\StackMiddleware\AjaxPageState:handle in /var/www/html/web/modules/contrib/whoops/src/StackMiddleware/WhoopsMiddleware.php:49
#2 Drupal\whoops\StackMiddleware\WhoopsMiddleware:handle in /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php:51
#1 Drupal\Core\StackMiddleware\StackedHttpKernel:handle in /var/www/html/web/core/lib/Drupal/Core/DrupalKernel.php:709
#0 Drupal\Core\DrupalKernel:handle in /var/www/html/web/index.php:19

Url::toRenderArray()Url::toRenderArray() was deprecated for Drupal 10.1 in this issue πŸ“Œ Deprecate Url::toRenderArray() and Url::renderAccess() Fixed .

Steps to reproduce

On a Drupal 11 site, install this module, then navigate to the main navigation menu and click to add a config link.

Proposed resolution

Replace the now missing method with code that is available in Drupal 11.

πŸ› Bug report
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡¨πŸ‡¦Canada mandclu

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

Merge Requests

Comments & Activities

  • Issue created by @mandclu
  • Merge request !12Replace Url::toRenderArray() β†’ (Open) created by mandclu
  • I have reviewed and tested the proposed patch (!12) on my local Drupal 11 environment. The fatal error caused by the deprecated Url::toRenderArray() method has been successfully resolved.

    The patch introduces a new urlToRenderArray() method within the MenuLinkConfigForm class, which prepares the renderable array manually. Instead of directly calling the deprecated method, it now checks access for the entity’s edit-form URL and builds the render array using #url and #options properties. This ensures compatibility with Drupal 11’s updated API and follows current best practices for rendering links.

    After applying the patch, I was able to add a config link to a menu without encountering any errors. Everything works as expected.

    Thank you for the fix!

    Tested with:

    Drupal 11.2-dev

    PHP 8.3.14

    Fresh install of menu_link_config module

    Marking as Reviewed and Tested.

Production build 0.71.5 2024