• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Goodbye hook_menu() - Routing and Menus in Drupal 8
 

Goodbye hook_menu() - Routing and Menus in Drupal 8

on

  • 258 views

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

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

Statistics

Views

Total Views
258
Views on SlideShare
205
Embed Views
53

Actions

Likes
0
Downloads
0
Comments
0

2 Embeds 53

http://www.exove.com 35
https://twitter.com 18

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Goodbye hook_menu() - Routing and Menus in Drupal 8 Goodbye hook_menu() - Routing and Menus in Drupal 8 Presentation Transcript

    • GOODBYE HOOK_MENU() ROUTING AND MENUS IN D8
    • 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
    • Exove in Brief ExoveisaleadingNorthernEuropeancompanyspecialisinginopen sourcewebservicesdesignanddevelopment. WehelpcompaniesconductbetterbusinessontheInternet throughbest-of-breedpersonnelandsolutions. Quickfacts:  Founded2006  Over70people  Servedmorethan170clients  OfficesinFinland,Estonia,andtheUK  Solidfinancialstatus
    • 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?
    • 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' => '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; }
    • 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; }
    • How about Drupal 8?
    • To make this…
    • and this…
    • and this…
    • 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'
    • 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!'), ); } public function someone($name) { $hello = t('Hello ') . $name; return array( '#markup' => $hello, ); } public function someoneTitle($name) { $hello = t('Hello ') . $name; return $hello; } }
    • Routing  Routing system matches paths to controllers  Based on Symfony2  Static configuration in module’s module.routing.yml file
    • 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'), );
    • 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'), );
    • Route in detail hello.someone: path: '/hello/{name}' defaults: _content: 'DrupalhelloControllerHel… _title_callback: 'DrupalhelloControllerHel… requirements: _permission: 'access content’
    • Route in detail hello.someone: path: '/hello/{name}' defaults: _content: 'DrupalhelloControllerHel… _title_callback: 'DrupalhelloControllerHel… requirements: _permission: 'access content’ Machine name
    • 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
    • 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, ...
    • 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, ...
    • 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
    • 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
    • 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)
    • 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
    • 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
    • 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
    • THANK YOU! Questions? Comments?