This document discusses routing and menus in Drupal 8. In Drupal 8, hook_menu() has been replaced by static configuration in YAML files. Routing is defined in module.routing.yml, which maps paths to controller methods. Menu items are defined separately in module.menu_links.yml. This provides a cleaner separation of routing from menu display. Dynamic routes can also be defined by returning route objects from a callback method. Route alterations can be done through event subscribers. Other menu-related items like tabs and actions also have their own configuration files.
2. About the Speaker
Joonas Iivonen
Senior developer at Exove
Architecture and development of
web and mobile applications with
Drupal or PHP framework core
Managing the applications competence
4. hook_menu()
Hook_menu() has been defining menu items
and page callbacks since for ever
But it’s not there any more in Drupal 8
How do we handle the menus and routing then?
23. Route in detail
hello.someone:
path: '/hello/{name}'
defaults:
_content:
'DrupalhelloControllerHel…
_title_callback:
'DrupalhelloControllerHel…
requirements:
_permission: 'access content’
Machine name
24. Route in detail
hello.someone:
path: '/hello/{name}'
defaults:
_content:
'DrupalhelloControllerHel…
_title_callback:
'DrupalhelloControllerHel…
requirements:
_permission: 'access content’
Path,
with named parameter for
controller or form
25. Route in detail
hello.someone:
path: '/hello/{name}'
defaults:
_content:
'DrupalhelloControllerHel…
_title_callback:
'DrupalhelloControllerHel…
requirements:
_permission: 'access content’
Where to get the content?
_content, _form, _entity_view, ...
26. Route in detail
hello.someone:
path: '/hello/{name}'
defaults:
_content:
'DrupalhelloControllerHel…
_title_callback:
'DrupalhelloControllerHel…
requirements:
_permission: 'access content’
Who has the access?
_permission, _role,
_entity_access, ...
27. Dynamic routes
Configuration in module.routing.yml
route_callbacks:
- 'DrupalexampleRoutingExlRoutes::routes’
And method returns either
an array of SymfonyComponentRoutingRoute
objects or
a SymfonyComponentRoutingRouteCollection
object
See for example Views
28. Altering routes
Event Subscriber triggered by
RoutingEvents::ALTER
Configuration in module.services.yml
services:
example.route_subscriber:
class:
DrupalexampleRoutingRouteSubscriber
tags:
- { name: event_subscriber }
Again check Views for an example
29. Menus
Menus are configured in module’s
module.menu_link.yml file
To manage menus you need to use menu_link
module (menu_link_save and so on)
30. Menu items in detail
hello.world:
title: 'Hello World'
description: 'Hello World menu item'
route_name: hello.world
hello.world_2:
title: 'Hello World child'
description: 'Hello World menu item'
route_name: hello.world
parent: hello.world
31. Other items
Like menus local tasks, actions and contextual
links are defined in configuration files:
module.local_tasks.yml
module.local_actions.yml
module.contextual_links.yml
32. Summary
Static configuration from hook_menu() has been
moved to different configuration files in YAML
format
module.routing.yml is referenced by others
Routes point to callables or forms in class format
For altering routes you need an even subscriber
There are Symfony ways to learn