12
Routing in Drupal 8 Decoupling hook_menu() Ashish Thakur @Ashish_Thakur

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

Embed Size (px)

Citation preview

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

Routing in Drupal 8Decoupling hook_menu()

Ashish Thakur @Ashish_Thakur

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

OverviewA 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.

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

Overview

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

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.

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

Routing in Railsmatch 'products/:id', :to => 'catalog#view'

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

Routing in Symfony# app/config/routing.yml

blog_show:

path: /blog/{slug}

defaults: { _controller: AcmeBlogBundle:Blog:show }

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

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', );}

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

Routing in Drupal 8Drupal's routing system works with the Symfony HTTP Kernel.

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

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

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

Decoupling hook_menu()

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

Ashish Thakur@Ashish_Thakur

Thank You!