Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Routing in Drupal 8
July 31, 2014
Nice to Meet You!
SECTION TITLE
Kalpana Goel
Developer
William Hurley
Manager, Technical
Development
Routes basically are the mappings between URL
paths and their corresponding page and access
callbacks.
What is a route?
What’s Changed
hook_menu defines the
routing in Drupal 7
SECTION TITLE
1:1 mapping of path to route
hook_menu is dead in 8.0.x
SECTION TITLE
There is no hook_menu in Drupal 8!
MODULENAME.routing.yml
One path may map to mul...
Why the Change?
D7 hook_menu
SECTION TITLE
● Routing (page and access callbacks)
● Menu links
● Local actions
● Local tasks
● Breadcrumbs
...
*.links.actin.ml *.links.contexual.yml
Basic Example
D7: hook_menu()
function user_menu() {
$items['user/logout'] = array(
'title' => 'Log out',
'access callback' => 'user_is_...
D8: Routing
user.routing.yml
user.logout:
path: '/user/logout'
defaults:
_controller: 'DrupaluserControllerUserController:...
D7: page callback
/**
* Menu callback; logs the current user out, and redirects to the home page.
*/
function user_logout(...
D8: Controller
namespace DrupaluserController;
class UserController extends ControllerBase {
public function logout() {
us...
Path Variables
D8: Path (required)
For dynamic properties, you can include them in curly braces.
For example -
‘/admin/structure/views/{j...
D8: dynamic path example
views_ui.form_display:
path: '/admin/structure/views/{js}/display/{view}/{display_id}/{type}'
def...
D8: Optional Attributes
user.cancel_confirm:
path: '/user/{user}/cancel/confirm/{timestamp}/{hashed_pass}'
defaults:
_titl...
D8: Page Title
user.view:
path: '/user/{user}'
defaults:
_entity_view: 'user.full'
_title_callback: 'DrupaluserControllerU...
D8: Page Types
_content : -display content on a page
_form : - display form on a page.
_controller : - use to generate raw...
Access Restrictions
D8: Available Checks
_permission - A permission string (e.g. - _permission: ‘access
content’)
_role : A specific user role...
D8: Access check
user.role_add:
path: '/admin/people/roles/add'
defaults:
_entity_form: user_role.default
_title: 'Add rol...
D8: Access check
Multiple access check -
node.add_page:
path: '/node/add'
defaults:
_title: 'Add content'
_content:
'Drupa...
Forms
D7: Form Router
$items['user/password'] = array(
'title' => 'Request new password',
'page callback' => 'drupal_get_form',
...
D8: Form Router
Forms are classes
There is no method in forms as forms are presented as one class
Use _form instead of _co...
D7: User Password Form
function user_pass() {
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Username or e-...
D8: Form Interface
namespace DrupalCoreForm;
* Provides an interface for a Form.
interface FormInterface {
* Returns a uni...
D8: User Password Form
class UserPasswordForm extends FormBase {
public function getFormId() { return ‘user_pass’; }
publi...
D7: Form Validation
function user_pass_validate($form, &$form_state) {
[...]
form_set_error('name', t('Sorry, %name is not...
D8: Form Validation
public function validateForm(array &$form, array &$form_state) {
[...]
$this->setFormError('name', $fo...
D8: Form Base
class* * Base class for implementing system configuration forms.
DrupalcoreformConfigFormBase
for example -
...
Other functionality
from hook_menu
local task
local task
local task
D7: menu local tasks
$items['user/password'] = array(
'title' => 'Request new password',
'page callback' => 'drupal_get_fo...
D8: menu local tasks
user.links.task.yml
user.page:
route_name: user.page
base_route: user.page
title: 'Log in'
weight: -1...
local action
D7: Local action
$items['admin/structure/types/add'] = array(
'title' => 'Add content type',
'page callback' => 'drupal_ge...
D8: Local action
node.links.action.yml
node.add_page:
route_name: node.add_page
title: 'Add content'
appears_on:
- system....
D8: Local action on multiple pages
block_content.links.action.yml
block_content_add_action:
route_name: block_content.add_...
D8: Contextual
links
D7: Contextual
links$items['admin/structure/block/manage/%/%/configure'] = array(
'title' => 'Configure block',
'type' => ...
D8: Contextual
links
block.links.contextual.yml
block_configure:
title: 'Configure block'
route_name: 'block.admin_edit'
g...
D8: breadcrumb
breadcrumb is path based in Drupal 8
https://www.drupal.org/node/2098323
Useful Tips
Options
Useful Tips
_admin_route -- whether to use the admin theme for this route
_maintenance_access -- whether route is ...
Useful links
SECTION TITLE
https://www.drupal.org/node/1800686 - change record
https://www.drupal.org/node/2118147 - D7 to...
THANK YOU!
Kalpana Goel
William Hurley
Routing in Drupal 8
Upcoming SlideShare
Loading in …5
×

Routing in Drupal 8

3,165 views

Published on

Drupal 8, page callbacks are completely rewritten by utilizing Symfony Routing component. This session will explain many parts of routing, how to convert from Drupal 7 menu system to Drupal 8 routing system and it will cover how to define local tasks, local actions, and contextual links in Drupal 8.

Published in: Technology
  • Dating direct: ❤❤❤ http://bit.ly/2Qu6Caa ❤❤❤
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Dating for everyone is here: ❶❶❶ http://bit.ly/2Qu6Caa ❶❶❶
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Routing in Drupal 8

  1. 1. Routing in Drupal 8 July 31, 2014
  2. 2. Nice to Meet You! SECTION TITLE Kalpana Goel Developer William Hurley Manager, Technical Development
  3. 3. Routes basically are the mappings between URL paths and their corresponding page and access callbacks. What is a route?
  4. 4. What’s Changed
  5. 5. hook_menu defines the routing in Drupal 7 SECTION TITLE 1:1 mapping of path to route
  6. 6. hook_menu is dead in 8.0.x SECTION TITLE There is no hook_menu in Drupal 8! MODULENAME.routing.yml One path may map to multiple routes
  7. 7. Why the Change?
  8. 8. D7 hook_menu SECTION TITLE ● Routing (page and access callbacks) ● Menu links ● Local actions ● Local tasks ● Breadcrumbs ● Contextual links ● Title ● Weight
  9. 9. *.links.actin.ml *.links.contexual.yml
  10. 10. Basic Example
  11. 11. D7: hook_menu() function user_menu() { $items['user/logout'] = array( 'title' => 'Log out', 'access callback' => 'user_is_logged_in', 'page callback' => 'user_logout', 'weight' => 10, 'menu_name' => 'user-menu', 'file' => 'user.pages.inc', ); return $items; }
  12. 12. D8: Routing user.routing.yml user.logout: path: '/user/logout' defaults: _controller: 'DrupaluserControllerUserController::logout' requirements: _user_is_logged_in: 'TRUE'
  13. 13. D7: page callback /** * Menu callback; logs the current user out, and redirects to the home page. */ function user_logout() { global $user; watchdog('user', 'Session closed for %name.', array('%name' => $user->name)); module_invoke_all('user_logout', $user); // Destroy the current session, and reset $user to the anonymous user. session_destroy(); drupal_goto(); }
  14. 14. D8: Controller namespace DrupaluserController; class UserController extends ControllerBase { public function logout() { user_logout(); return $this->redirect('<front>'); }
  15. 15. Path Variables
  16. 16. D8: Path (required) For dynamic properties, you can include them in curly braces. For example - ‘/admin/structure/views/{js}/display/{view}/{display_id}/{type}' The {display_id} element in the URL is called a slug and is available as $display_id in the controller method.
  17. 17. D8: dynamic path example views_ui.form_display: path: '/admin/structure/views/{js}/display/{view}/{display_id}/{type}' defaults: _content: 'Drupalviews_uiFormAjaxDisplay::getForm' class Display extends ViewsFormBase { public function getForm(ViewStorageInterface $view, $display_id, $js, $type = NULL) { $this->setType($type); return parent::getForm($view, $display_id, $js); }
  18. 18. D8: Optional Attributes user.cancel_confirm: path: '/user/{user}/cancel/confirm/{timestamp}/{hashed_pass}' defaults: _title: 'Confirm account cancellation' _content: 'DrupaluserControllerUserController::confirmCancel' timestamp: 0 hashed_pass: ''
  19. 19. D8: Page Title user.view: path: '/user/{user}' defaults: _entity_view: 'user.full' _title_callback: 'DrupaluserControllerUserController::userTitle' requirements: _entity_access: 'user.view'
  20. 20. D8: Page Types _content : -display content on a page _form : - display form on a page. _controller : - use to generate raw data like json output _entity_view : - for example - node.teaser _entity_form : - display a form for a entity _entity_list : - display list of entity like node
  21. 21. Access Restrictions
  22. 22. D8: Available Checks _permission - A permission string (e.g. - _permission: ‘access content’) _role : A specific user role (e.g.- administrator) _entity_access: In case where an entity is part of route, can check a certain access level before granting access (e.g. node.view) _custom_access: You can also do custom access checking on route. Same as title callback (define as method on class)Read more - https://www.drupal.org/node/2122195
  23. 23. D8: Access check user.role_add: path: '/admin/people/roles/add' defaults: _entity_form: user_role.default _title: 'Add role' requirements: _permission: 'administer permissions' Some permissions based on roles , permissions _permission: ‘administer nodes’ _role: ‘administrator’ Based upon access to Entities _entity_access: $entity_type.$operation check to see if everyone has access _access: TRUE
  24. 24. D8: Access check Multiple access check - node.add_page: path: '/node/add' defaults: _title: 'Add content' _content: 'DrupalnodeControllerNodeController::addPage' options: _access_mode: 'ANY' _node_operation_route: TRUE requirements: _permission: 'administer content types' _node_add_access: 'node'
  25. 25. Forms
  26. 26. D7: Form Router $items['user/password'] = array( 'title' => 'Request new password', 'page callback' => 'drupal_get_form', 'page arguments' => array('user_pass'), 'access callback' => TRUE, 'type' => MENU_LOCAL_TASK, 'file' => 'user.pages.inc', );
  27. 27. D8: Form Router Forms are classes There is no method in forms as forms are presented as one class Use _form instead of _content or _controller user.pass: path: '/user/password' defaults: _form: 'DrupaluserFormUserPasswordForm' _title: 'Request new password' requirements: _access: 'TRUE' options: _maintenance_access: TRUE
  28. 28. D7: User Password Form function user_pass() { $form['name'] = array( '#type' => 'textfield', '#title' => t('Username or e-mail address'), '#size' => 60, '#maxlength' => max(USERNAME_MAX_LENGTH, EMAIL_MAX_LENGTH), '#required' => TRUE, '#default_value' => isset($_GET['name']) ? $_GET['name'] : '', ); [...] } function user_pass_validate($form, &$form_state) function user_pass_submit($form, &$form_state)
  29. 29. D8: Form Interface namespace DrupalCoreForm; * Provides an interface for a Form. interface FormInterface { * Returns a unique string identifying the form public function getFormId(); *Form constructor. public function buildForm(array $form, array &$form_state); * Form validation handler. public function validateForm(array &$form, array &$form_state); * Form submission handler. public function submitForm(array &$form, array &$form_state); }
  30. 30. D8: User Password Form class UserPasswordForm extends FormBase { public function getFormId() { return ‘user_pass’; } public function buildForm(array $form, array &$form_state) { $form['name'] = array( '#type' => 'textfield', '#title' => $this->t('Username or email address'), '#size' => 60, '#maxlength' => max(USERNAME_MAX_LENGTH, EMAIL_MAX_LENGTH), '#required' => TRUE, ); public function submitForm(array &$form, array &$form_state) {....} public function validateForm(array &$form, array &$form_state) {...}
  31. 31. D7: Form Validation function user_pass_validate($form, &$form_state) { [...] form_set_error('name', t('Sorry, %name is not recognized as a user name or an e-mail address.', array('%name' => $name))); }
  32. 32. D8: Form Validation public function validateForm(array &$form, array &$form_state) { [...] $this->setFormError('name', $form_state, $this->t('Sorry, %name is not recognized as a username or an email address.', array('%name' => $name))); }
  33. 33. D8: Form Base class* * Base class for implementing system configuration forms. DrupalcoreformConfigFormBase for example - class MenuSettingsForm extends ConfigFormBase ** generic base class - thisincludes string translation, link generator DrupalCoreFormFormBase for example - class UserLoginForm extends FormBase ** base class for a confirmation form. DrupalCoreFormConfirmFormBase for example - class LoggingForm extends ConfigFormBase
  34. 34. Other functionality from hook_menu
  35. 35. local task
  36. 36. local task
  37. 37. local task
  38. 38. D7: menu local tasks $items['user/password'] = array( 'title' => 'Request new password', 'page callback' => 'drupal_get_form', 'page arguments' => array('user_pass'), 'access callback' => TRUE, 'type' => MENU_LOCAL_TASK, 'file' => 'user.pages.inc', );
  39. 39. D8: menu local tasks user.links.task.yml user.page: route_name: user.page base_route: user.page title: 'Log in' weight: -10 user.pass: route_name: user.pass base_route: user.page title: 'Request new password'
  40. 40. local action
  41. 41. D7: Local action $items['admin/structure/types/add'] = array( 'title' => 'Add content type', 'page callback' => 'drupal_get_form', 'page arguments' => array('node_type_form'), 'access arguments' => array('administer content types'), 'type' => MENU_LOCAL_ACTION, 'file' => 'content_types.inc', );
  42. 42. D8: Local action node.links.action.yml node.add_page: route_name: node.add_page title: 'Add content' appears_on: - system.admin_content
  43. 43. D8: Local action on multiple pages block_content.links.action.yml block_content_add_action: route_name: block_content.add_page title: 'Add custom block' appears_on: - block.admin_display - block.admin_display_theme - block_content.list
  44. 44. D8: Contextual links
  45. 45. D7: Contextual links$items['admin/structure/block/manage/%/%/configure'] = array( 'title' => 'Configure block', 'type' => MENU_DEFAULT_LOCAL_TASK, 'context' => MENU_CONTEXT_INLINE, );
  46. 46. D8: Contextual links block.links.contextual.yml block_configure: title: 'Configure block' route_name: 'block.admin_edit' group: 'block'
  47. 47. D8: breadcrumb breadcrumb is path based in Drupal 8 https://www.drupal.org/node/2098323
  48. 48. Useful Tips
  49. 49. Options Useful Tips _admin_route -- whether to use the admin theme for this route _maintenance_access -- whether route is publicly available when the site is in maintenance mode _access_mode -- whether requirements are ANY or ALL
  50. 50. Useful links SECTION TITLE https://www.drupal.org/node/1800686 - change record https://www.drupal.org/node/2118147 - D7 to D8 upgrade tutorial https://www.drupal.org/developing/api/8/routing - Routing system in D8
  51. 51. THANK YOU! Kalpana Goel William Hurley

×