How to build a menu render array programmatically
Issue:
Sometimes you might need to load and render a custom menu inside a controller – for example on a user dashboard – with access checking, default sorting, and a custom theme hook applied.
Solution:
This is a small but useful snippet for generating menu tree render arrays for any menu from a custom controller.
Just change $menu_name to the machine name of your menu.
Extra: For similar use cases, you might want to use the Drupal Twig Tweak module. You can also use the {{ drupal_menu('my_menu') }} function directly in your template.