drupal 8: routing & more

19
Drupal 8: Routing & more Drupal 8: Routing & more Routing, Controllers, Menu Items, Access Control Routing, Controllers, Menu Items, Access Control Drupal Meetup Stuttgart Drupal Meetup Stuttgart 04/02/2015

Upload: drubb

Post on 17-Jul-2015

248 views

Category:

Internet


3 download

TRANSCRIPT

Page 1: Drupal 8: Routing & More

Drupal 8: Routing & moreDrupal 8: Routing & moreRouting, Controllers, Menu Items, Access ControlRouting, Controllers, Menu Items, Access Control

Drupal Meetup StuttgartDrupal Meetup Stuttgart

04/02/2015

Page 2: Drupal 8: Routing & More

1. Once upon a time,1. Once upon a time,in Drupal 7...in Drupal 7...

Page 3: Drupal 8: Routing & More

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)

Page 4: Drupal 8: Routing & More

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?

Page 5: Drupal 8: Routing & More

2. D8: Routing &2. D8: Routing &ControllersControllers

Page 6: Drupal 8: Routing & More

~ path / url ~ callback

Page 7: Drupal 8: Routing & More

...

forum.add_container: path: '/admin/structure/forum/add/container' defaults: _controller: '\Drupal\forum\Controller\ForumController::addContainer' _title: 'Add container' requirements: _permission: 'administer forums'

...

forum.routing.yml

Page 8: Drupal 8: Routing & More

namespace Drupal\forum\Controller;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

Page 9: Drupal 8: Routing & More

/admin/structure/forum/add/container

Page 10: Drupal 8: Routing & More

3. D8: Menu items3. D8: Menu items

Page 11: Drupal 8: Routing & More
Page 12: Drupal 8: Routing & More

forum.index: title: Forums route_name: forum.index menu_name: toolsforum.overview: title: Forums parent: system.admin_structure description: 'Control forum hierarchy settings.' route_name: forum.overview

Example: forum.links.menu.yml

Page 13: Drupal 8: Routing & More

forum.overview: route_name: forum.overview base_route: forum.overview title: Listforum.settings: route_name: forum.settings base_route: forum.overview title: Settings weight: 100

Example: forum.links.task.yml

Page 14: Drupal 8: Routing & More

/admin/structure/forum

Page 15: Drupal 8: Routing & More

4. D8: access control4. D8: access control

Page 16: Drupal 8: Routing & More

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

Page 17: Drupal 8: Routing & More

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

Page 18: Drupal 8: Routing & More

...

requirements: _permission: 'access content'

...

requirements: _user_is_logged_in: 'TRUE'

...

requirements: _role: 'administrator'

...

requirements: _entity_access: 'node.view'

...

There's even more...

Page 19: Drupal 8: Routing & More

Thank You!Thank You!

http://slides.com/drubbhttp://slideshare.net/drubb