GOODBYE HOOK_MENU()
ROUTING AND MENUS IN D8
About the Speaker
 Joonas Iivonen
 Senior developer at Exove
 Architecture and development of
web and mobile applicatio...
Exove in Brief
ExoveisaleadingNorthernEuropeancompanyspecialisinginopen
sourcewebservicesdesignanddevelopment.
Wehelpcompa...
hook_menu()
 Hook_menu() has been defining menu items
and page callbacks since for ever
 But it’s not there any more in ...
In Drupal 7…
To make this…
and this…
and this…
or this…
You did something like this
function hello_menu() {
$items['hello/world'] = array(
'title' => 'Hello World',
'description'...
With something like this
function _hello_world() {
return t('Hello World!');
}
function _hello_someone($name) {
if (is_nul...
How about Drupal 8?
To make this…
and this…
and this…
You need: routing.yml…
hello.world:
path: '/hello/world’
defaults:
_content:
'DrupalhelloControllerHelloController::world’...
And menu_links.yml…
hello.world:
title: 'Hello World'
description: 'Hello World menu item'
route_name: hello.world
And the code in controller
class HelloController {
public function world() {
return array(
'#markup' => t('Hello World!'),...
Routing
 Routing system matches paths to controllers
 Based on Symfony2
 Static configuration in module’s
module.routin...
Drupal 8
hello.world:
path: '/hello/world’
defaults:
_content:
'DrupalhelloControllerHelloController::world’
_title: 'Hell...
Drupal 8
hello.someone:
path: '/hello/{name}'
defaults:
_content:
'DrupalhelloControllerHelloController::someone'
_title_c...
Route in detail
hello.someone:
path: '/hello/{name}'
defaults:
_content:
'DrupalhelloControllerHel…
_title_callback:
'Drup...
Route in detail
hello.someone:
path: '/hello/{name}'
defaults:
_content:
'DrupalhelloControllerHel…
_title_callback:
'Drup...
Route in detail
hello.someone:
path: '/hello/{name}'
defaults:
_content:
'DrupalhelloControllerHel…
_title_callback:
'Drup...
Route in detail
hello.someone:
path: '/hello/{name}'
defaults:
_content:
'DrupalhelloControllerHel…
_title_callback:
'Drup...
Route in detail
hello.someone:
path: '/hello/{name}'
defaults:
_content:
'DrupalhelloControllerHel…
_title_callback:
'Drup...
Dynamic routes
 Configuration in module.routing.yml
route_callbacks:
- 'DrupalexampleRoutingExlRoutes::routes’
 And meth...
Altering routes
 Event Subscriber triggered by
RoutingEvents::ALTER
 Configuration in module.services.yml
services:
exam...
Menus
 Menus are configured in module’s
module.menu_link.yml file
 To manage menus you need to use menu_link
module (men...
Menu items in detail
hello.world:
title: 'Hello World'
description: 'Hello World menu item'
route_name: hello.world
hello....
Other items
 Like menus local tasks, actions and contextual
links are defined in configuration files:
 module.local_task...
Summary
 Static configuration from hook_menu() has been
moved to different configuration files in YAML
format
 module.ro...
THANK YOU!
Questions? Comments?
Upcoming SlideShare
Loading in …5
×

Goodbye hook_menu() - Routing and Menus in Drupal 8

4,245 views

Published on

Drupal 8 uses Symfony2 routing. This presentation shows how to configure the new routing and menus in Drupal 8.

Published in: Technology, Business
  • Be the first to comment

Goodbye hook_menu() - Routing and Menus in Drupal 8

  1. 1. GOODBYE HOOK_MENU() ROUTING AND MENUS IN D8
  2. 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
  3. 3. Exove in Brief ExoveisaleadingNorthernEuropeancompanyspecialisinginopen sourcewebservicesdesignanddevelopment. WehelpcompaniesconductbetterbusinessontheInternet throughbest-of-breedpersonnelandsolutions. Quickfacts:  Founded2006  Over70people  Servedmorethan170clients  OfficesinFinland,Estonia,andtheUK  Solidfinancialstatus
  4. 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?
  5. 5. In Drupal 7…
  6. 6. To make this…
  7. 7. and this…
  8. 8. and this…
  9. 9. or this…
  10. 10. You did something like this function hello_menu() { $items['hello/world'] = array( 'title' => 'Hello World', 'description' => 'Hello World menu item in hook_menu()', 'type' => MENU_NORMAL_ITEM, 'page callback' => '_hello_world', 'access arguments' => array('access content'), ); $items['hello/%'] = array( 'description' => 'Hello callback with argument in hook_menu()', 'type' => MENU_CALLBACK, 'page callback' => '_hello_someone', 'page arguments' => array(1), 'access arguments' => array('access content'), ); return $items; }
  11. 11. With something like this function _hello_world() { return t('Hello World!'); } function _hello_someone($name) { if (is_null($name)) { $hello = t('Hello You!'); } else { $hello = t('Hello ') . $name . '!'; } drupal_set_title($hello); return $hello; }
  12. 12. How about Drupal 8?
  13. 13. To make this…
  14. 14. and this…
  15. 15. and this…
  16. 16. You need: routing.yml… hello.world: path: '/hello/world’ defaults: _content: 'DrupalhelloControllerHelloController::world’ _title: 'Hello World’ requirements: _permission: 'access content' hello.someone: path: '/hello/{name}’ defaults: _content: 'DrupalhelloControllerHelloController::someone’ _title_callback: 'DrupalhelloControllerHelloController::someoneTitle’ requirements: _permission: 'access content'
  17. 17. And menu_links.yml… hello.world: title: 'Hello World' description: 'Hello World menu item' route_name: hello.world
  18. 18. And the code in controller class HelloController { public function world() { return array( '#markup' => t('Hello World!'), ); } public function someone($name) { $hello = t('Hello ') . $name; return array( '#markup' => $hello, ); } public function someoneTitle($name) { $hello = t('Hello ') . $name; return $hello; } }
  19. 19. Routing  Routing system matches paths to controllers  Based on Symfony2  Static configuration in module’s module.routing.yml file
  20. 20. Drupal 8 hello.world: path: '/hello/world’ defaults: _content: 'DrupalhelloControllerHelloController::world’ _title: 'Hello World’ requirements: _permission: 'access content’ Drupal 7 $items['hello/world'] = array( 'title' => 'Hello World', 'description' => 'Hello World menu item in hook_menu()', 'type' => MENU_NORMAL_ITEM, 'page callback' => '_hello_world', 'access arguments' => array('access content'), );
  21. 21. Drupal 8 hello.someone: path: '/hello/{name}' defaults: _content: 'DrupalhelloControllerHelloController::someone' _title_callback: 'DrupalhelloControllerHelloController::someTitle’ requirements: _permission: 'access content’ Drupal 7 $items['hello/%'] = array( 'description' => 'Hello callback with argument', 'type' => MENU_CALLBACK, 'page callback' => '_hello_someone', 'page arguments' => array(1), 'access arguments' => array('access content'), );
  22. 22. Route in detail hello.someone: path: '/hello/{name}' defaults: _content: 'DrupalhelloControllerHel… _title_callback: 'DrupalhelloControllerHel… requirements: _permission: 'access content’
  23. 23. Route in detail hello.someone: path: '/hello/{name}' defaults: _content: 'DrupalhelloControllerHel… _title_callback: 'DrupalhelloControllerHel… requirements: _permission: 'access content’ Machine name
  24. 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. 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. 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. 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. 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. 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. 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. 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. 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
  33. 33. THANK YOU! Questions? Comments?

×