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.

Drupal 8: Routing & More

913 views

Published on

Introducing Drupal 8 Routing, Controllers, Menu Items & Access Control, a presentation at Drupal Meetup Stuttgart, 04/02/2015

Published in: Internet
  • Be the first to comment

  • Be the first to like this

Drupal 8: Routing & More

  1. 1. Drupal 8: Routing & moreDrupal 8: Routing & more Routing, Controllers, Menu Items, Access ControlRouting, Controllers, Menu Items, Access Control Drupal Meetup StuttgartDrupal Meetup Stuttgart 04/02/2015
  2. 2. 1. Once upon a time,1. Once upon a time, in Drupal 7...in Drupal 7...
  3. 3. function forum_menu() { ... $items['admin/structure/forum/add/container'] = array( 'title' => 'Add container', 'page callback' => 'forum_form_main', 'page arguments' => array('container'), 'access arguments' => array('administer forums'), 'type' => MENU_LOCAL_ACTION, 'parent' => 'admin/structure/forum', 'file' => 'forum.admin.inc', ); ... } Example from D7 (forum.module)
  4. 4. function forum_menu() { ... $items['admin/structure/forum/add/container'] = array( 'title' => 'Add container', 'page callback' => 'forum_form_main', 'page arguments' => array('container'), 'access arguments' => array('administer forums'), 'type' => MENU_LOCAL_ACTION, 'parent' => 'admin/structure/forum', 'file' => 'forum.admin.inc', ); ... } Nice, but... Routing + access control + menu item + ... Separation of concerns?
  5. 5. 2. D8: Routing &2. D8: Routing & ControllersControllers
  6. 6. ~ path / url ~ callback
  7. 7. ... forum.add_container: path: '/admin/structure/forum/add/container' defaults: _controller: 'DrupalforumControllerForumController::addContainer' _title: 'Add container' requirements: _permission: 'administer forums' ... forum.routing.yml
  8. 8. namespace DrupalforumController; use ... class ForumController extends ControllerBase { ... /** * Returns add container entity form. * * @return array * Render array for the add form. */ public function addContainer() { $vid = $this->config('forum.settings')->get('vocabulary'); $taxonomy_term = $this->termStorage->create(array( 'vid' => $vid, 'forum_container' => 1, )); return $this->entityFormBuilder()->getForm($taxonomy_term, 'container'); } ... } ForumController.php
  9. 9. /admin/structure/forum/add/container
  10. 10. 3. D8: Menu items3. D8: Menu items
  11. 11. forum.index: title: Forums route_name: forum.index menu_name: tools forum.overview: title: Forums parent: system.admin_structure description: 'Control forum hierarchy settings.' route_name: forum.overview Example: forum.links.menu.yml
  12. 12. forum.overview: route_name: forum.overview base_route: forum.overview title: List forum.settings: route_name: forum.settings base_route: forum.overview title: Settings weight: 100 Example: forum.links.task.yml
  13. 13. /admin/structure/forum
  14. 14. 4. D8: access control4. D8: access control
  15. 15. function forum_permission() { $perms = array( 'administer forums' => array( 'title' => t('Administer forums'), ), ); return $perms; } administer forums: title: 'Administer forums' Drupal 7: hook_permission() Drupal 8: forum.permission.yml
  16. 16. function forum_menu() { ... 'access callback' => 'user_access', 'access arguments' => array('administer forums'), ... } ... requirements: _permission: 'administer forums' ... Drupal 7: hook_menu() Drupal 8: forum.routing.yml
  17. 17. ... requirements: _permission: 'access content' ... requirements: _user_is_logged_in: 'TRUE' ... requirements: _role: 'administrator' ... requirements: _entity_access: 'node.view' ... There's even more...
  18. 18. Thank You!Thank You! http://slides.com/drubb http://slideshare.net/drubb

×