Routing in Drupal 8 
Decoupling hook_menu() 
Ashish Thakur 
@Ashish_Thakur
Overview 
A route is a path which is defined for Drupal to return 
some sort of content on. For example, the default front 
page, '/node' is a route. When Drupal receives a request, 
it tries to match the requested path to a route it knows 
about. If the route is found, then the route's definition is 
used to return content. Otherwise, Drupal returns a 404.
Overview
Routing in Drupal 7 
● In Drupal 7 routing is handled by hook_menu(). 
● Apart from just routing it has many other responsibilities to 
do 
➢ Routing 
➢ Access Control 
➢ Visible navigation 
➢ File uploading 
● It is too much for a single function to handle and apart from 
routing many other tasks are tightly coupled in a single 
function. 
● And this looks a bit alien concept to developers from other 
MVC frameworks.
Routing in Rails 
match 'products/:id', :to => 'catalog#view'
Routing in Symfony 
# app/config/routing.yml 
blog_show: 
path: /blog/{slug} 
defaults: { _controller: AcmeBlogBundle:Blog:show }
Routing in Drupal 7 
/** 
* Implements hook_menu(). 
*/ 
function node_menu() { 
$items['node/%node/edit'] = array( 
'title' => 'Edit', 
'page callback' => 'node_page_edit', 
'page arguments' => array(1), 
'access callback' => 'node_access', 
'access arguments' => array('update', 1), 
'weight' => 0, 
'type' => MENU_LOCAL_TASK, 
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE, 
'file' => 'node.pages.inc', 
); 
}
Routing in Drupal 8 
Drupal's routing system works with the Symfony HTTP Kernel.
Routing in Drupal 8 
● Drupal's routing system works with the Symfony HTTP 
Kernel. 
● Rather than defining routes in hook_menu(), they are now 
defined in module.routing.yml files, in YAML format 
● An example from node.routing.yml file 
entity.node.edit_form: 
path: '/node/{node}/edit' 
defaults: 
_entity_form: 'node.edit' 
requirements: 
_entity_access: 'node.update' 
options: 
_node_operation_route: TRUE
Decoupling hook_menu()
Useful Links 
● https://www.drupal.org/developing/api/8/routing 
● https://www.drupal.org/developing/api/8/menu 
● https://www.drupal.org/node/1800686 
● https://www.drupal.org/list-changes
Thank You! 
Ashish Thakur 
@Ashish_Thakur

[Srijan Wednesday Webinars] Routing in Drupal 8: Decoupling hook_menu

  • 1.
    Routing in Drupal8 Decoupling hook_menu() Ashish Thakur @Ashish_Thakur
  • 2.
    Overview A routeis a path which is defined for Drupal to return some sort of content on. For example, the default front page, '/node' is a route. When Drupal receives a request, it tries to match the requested path to a route it knows about. If the route is found, then the route's definition is used to return content. Otherwise, Drupal returns a 404.
  • 3.
  • 4.
    Routing in Drupal7 ● In Drupal 7 routing is handled by hook_menu(). ● Apart from just routing it has many other responsibilities to do ➢ Routing ➢ Access Control ➢ Visible navigation ➢ File uploading ● It is too much for a single function to handle and apart from routing many other tasks are tightly coupled in a single function. ● And this looks a bit alien concept to developers from other MVC frameworks.
  • 5.
    Routing in Rails match 'products/:id', :to => 'catalog#view'
  • 6.
    Routing in Symfony # app/config/routing.yml blog_show: path: /blog/{slug} defaults: { _controller: AcmeBlogBundle:Blog:show }
  • 7.
    Routing in Drupal7 /** * Implements hook_menu(). */ function node_menu() { $items['node/%node/edit'] = array( 'title' => 'Edit', 'page callback' => 'node_page_edit', 'page arguments' => array(1), 'access callback' => 'node_access', 'access arguments' => array('update', 1), 'weight' => 0, 'type' => MENU_LOCAL_TASK, 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE, 'file' => 'node.pages.inc', ); }
  • 8.
    Routing in Drupal8 Drupal's routing system works with the Symfony HTTP Kernel.
  • 9.
    Routing in Drupal8 ● Drupal's routing system works with the Symfony HTTP Kernel. ● Rather than defining routes in hook_menu(), they are now defined in module.routing.yml files, in YAML format ● An example from node.routing.yml file entity.node.edit_form: path: '/node/{node}/edit' defaults: _entity_form: 'node.edit' requirements: _entity_access: 'node.update' options: _node_operation_route: TRUE
  • 10.
  • 11.
    Useful Links ●https://www.drupal.org/developing/api/8/routing ● https://www.drupal.org/developing/api/8/menu ● https://www.drupal.org/node/1800686 ● https://www.drupal.org/list-changes
  • 12.
    Thank You! AshishThakur @Ashish_Thakur