SlideShare a Scribd company logo
1 of 33
GOODBYE HOOK_MENU()
ROUTING AND MENUS IN D8
About the Speaker
 Joonas Iivonen
 Senior developer at Exove
 Architecture and development of
web and mobile applications with
Drupal or PHP framework core
 Managing the applications competence
Exove in Brief
ExoveisaleadingNorthernEuropeancompanyspecialisinginopen
sourcewebservicesdesignanddevelopment.
WehelpcompaniesconductbetterbusinessontheInternet
throughbest-of-breedpersonnelandsolutions.
Quickfacts:
 Founded2006
 Over70people
 Servedmorethan170clients
 OfficesinFinland,Estonia,andtheUK
 Solidfinancialstatus
hook_menu()
 Hook_menu() has been defining menu items
and page callbacks since for ever
 But it’s not there any more in Drupal 8
 How do we handle the menus and routing then?
In Drupal 7…
To make this…
and this…
and this…
or this…
You did something like this
function hello_menu() {
$items['hello/world'] = array(
'title' => 'Hello World',
'description' => 'Hello World menu item in hook_menu()',
'type' => MENU_NORMAL_ITEM,
'page callback' => '_hello_world',
'access arguments' => array('access content'),
);
$items['hello/%'] = array(
'description' => 'Hello callback with argument in hook_menu()',
'type' => MENU_CALLBACK,
'page callback' => '_hello_someone',
'page arguments' => array(1),
'access arguments' => array('access content'),
);
return $items;
}
With something like this
function _hello_world() {
return t('Hello World!');
}
function _hello_someone($name) {
if (is_null($name)) {
$hello = t('Hello You!');
}
else {
$hello = t('Hello ') . $name . '!';
}
drupal_set_title($hello);
return $hello;
}
How about Drupal 8?
To make this…
and this…
and this…
You need: routing.yml…
hello.world:
path: '/hello/world’
defaults:
_content:
'DrupalhelloControllerHelloController::world’
_title: 'Hello World’
requirements:
_permission: 'access content'
hello.someone:
path: '/hello/{name}’
defaults:
_content:
'DrupalhelloControllerHelloController::someone’
_title_callback:
'DrupalhelloControllerHelloController::someoneTitle’
requirements:
_permission: 'access content'
And menu_links.yml…
hello.world:
title: 'Hello World'
description: 'Hello World menu item'
route_name: hello.world
And the code in controller
class HelloController {
public function world() {
return array(
'#markup' => t('Hello World!'),
);
}
public function someone($name) {
$hello = t('Hello ') . $name;
return array(
'#markup' => $hello,
);
}
public function someoneTitle($name) {
$hello = t('Hello ') . $name;
return $hello;
}
}
Routing
 Routing system matches paths to controllers
 Based on Symfony2
 Static configuration in module’s
module.routing.yml file
Drupal 8
hello.world:
path: '/hello/world’
defaults:
_content:
'DrupalhelloControllerHelloController::world’
_title: 'Hello World’
requirements:
_permission: 'access content’
Drupal 7
$items['hello/world'] = array(
'title' => 'Hello World',
'description' => 'Hello World menu item in hook_menu()',
'type' => MENU_NORMAL_ITEM,
'page callback' => '_hello_world',
'access arguments' => array('access content'),
);
Drupal 8
hello.someone:
path: '/hello/{name}'
defaults:
_content:
'DrupalhelloControllerHelloController::someone'
_title_callback:
'DrupalhelloControllerHelloController::someTitle’
requirements:
_permission: 'access content’
Drupal 7
$items['hello/%'] = array(
'description' => 'Hello callback with argument',
'type' => MENU_CALLBACK,
'page callback' => '_hello_someone',
'page arguments' => array(1),
'access arguments' => array('access content'),
);
Route in detail
hello.someone:
path: '/hello/{name}'
defaults:
_content:
'DrupalhelloControllerHel…
_title_callback:
'DrupalhelloControllerHel…
requirements:
_permission: 'access content’
Route in detail
hello.someone:
path: '/hello/{name}'
defaults:
_content:
'DrupalhelloControllerHel…
_title_callback:
'DrupalhelloControllerHel…
requirements:
_permission: 'access content’
Machine name
Route in detail
hello.someone:
path: '/hello/{name}'
defaults:
_content:
'DrupalhelloControllerHel…
_title_callback:
'DrupalhelloControllerHel…
requirements:
_permission: 'access content’
Path,
with named parameter for
controller or form
Route in detail
hello.someone:
path: '/hello/{name}'
defaults:
_content:
'DrupalhelloControllerHel…
_title_callback:
'DrupalhelloControllerHel…
requirements:
_permission: 'access content’
Where to get the content?
_content, _form, _entity_view, ...
Route in detail
hello.someone:
path: '/hello/{name}'
defaults:
_content:
'DrupalhelloControllerHel…
_title_callback:
'DrupalhelloControllerHel…
requirements:
_permission: 'access content’
Who has the access?
_permission, _role,
_entity_access, ...
Dynamic routes
 Configuration in module.routing.yml
route_callbacks:
- 'DrupalexampleRoutingExlRoutes::routes’
 And method returns either
 an array of SymfonyComponentRoutingRoute
objects or
 a SymfonyComponentRoutingRouteCollection
object
 See for example Views
Altering routes
 Event Subscriber triggered by
RoutingEvents::ALTER
 Configuration in module.services.yml
services:
example.route_subscriber:
class:
DrupalexampleRoutingRouteSubscriber
tags:
- { name: event_subscriber }
 Again check Views for an example
Menus
 Menus are configured in module’s
module.menu_link.yml file
 To manage menus you need to use menu_link
module (menu_link_save and so on)
Menu items in detail
hello.world:
title: 'Hello World'
description: 'Hello World menu item'
route_name: hello.world
hello.world_2:
title: 'Hello World child'
description: 'Hello World menu item'
route_name: hello.world
parent: hello.world
Other items
 Like menus local tasks, actions and contextual
links are defined in configuration files:
 module.local_tasks.yml
 module.local_actions.yml
 module.contextual_links.yml
Summary
 Static configuration from hook_menu() has been
moved to different configuration files in YAML
format
 module.routing.yml is referenced by others
 Routes point to callables or forms in class format
 For altering routes you need an even subscriber
 There are Symfony ways to learn
THANK YOU!
Questions? Comments?

More Related Content

What's hot

Introduction to ZendX jQuery
Introduction to ZendX jQueryIntroduction to ZendX jQuery
Introduction to ZendX jQuery
dennisdc
 
Dependency injection-zendcon-2010
Dependency injection-zendcon-2010Dependency injection-zendcon-2010
Dependency injection-zendcon-2010
Fabien Potencier
 
Introduction to Zend_Pdf
Introduction to Zend_PdfIntroduction to Zend_Pdf
Introduction to Zend_Pdf
dennisdc
 
Twitter bootstrap
Twitter bootstrapTwitter bootstrap
Twitter bootstrap
dennisdc
 
Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)
Fabien Potencier
 
Gravity Forms Hooks & Filters
Gravity Forms Hooks & FiltersGravity Forms Hooks & Filters
Gravity Forms Hooks & Filters
iamdangavin
 
Settings API - Oslo WordPress Meetup - November 22, 2011
Settings API - Oslo WordPress Meetup - November 22, 2011Settings API - Oslo WordPress Meetup - November 22, 2011
Settings API - Oslo WordPress Meetup - November 22, 2011
WPOslo
 

What's hot (20)

First Steps in Drupal Code Driven Development
First Steps in Drupal Code Driven DevelopmentFirst Steps in Drupal Code Driven Development
First Steps in Drupal Code Driven Development
 
BEAR DI
BEAR DIBEAR DI
BEAR DI
 
Introduction to ZendX jQuery
Introduction to ZendX jQueryIntroduction to ZendX jQuery
Introduction to ZendX jQuery
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For Beginners
 
Drupal 8: Forms
Drupal 8: FormsDrupal 8: Forms
Drupal 8: Forms
 
Dependency injection-zendcon-2010
Dependency injection-zendcon-2010Dependency injection-zendcon-2010
Dependency injection-zendcon-2010
 
Introduction to Zend_Pdf
Introduction to Zend_PdfIntroduction to Zend_Pdf
Introduction to Zend_Pdf
 
Twitter bootstrap
Twitter bootstrapTwitter bootstrap
Twitter bootstrap
 
Codigo taller-plugins
Codigo taller-pluginsCodigo taller-plugins
Codigo taller-plugins
 
Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)
 
Gravity Forms Hooks & Filters
Gravity Forms Hooks & FiltersGravity Forms Hooks & Filters
Gravity Forms Hooks & Filters
 
Bag Of Tricks From Iusethis
Bag Of Tricks From IusethisBag Of Tricks From Iusethis
Bag Of Tricks From Iusethis
 
Settings API - Oslo WordPress Meetup - November 22, 2011
Settings API - Oslo WordPress Meetup - November 22, 2011Settings API - Oslo WordPress Meetup - November 22, 2011
Settings API - Oslo WordPress Meetup - November 22, 2011
 
Facebook
FacebookFacebook
Facebook
 
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
jQuery UI Widgets, Drag and Drop, Drupal 7 JavascriptjQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
 
WordPress overloading Gravityforms using hooks, filters and extending classes
WordPress overloading Gravityforms using hooks, filters and extending classes WordPress overloading Gravityforms using hooks, filters and extending classes
WordPress overloading Gravityforms using hooks, filters and extending classes
 
Writing Drupal 5 Module
Writing Drupal 5 ModuleWriting Drupal 5 Module
Writing Drupal 5 Module
 
Building scalable products with WordPress - WordCamp London 2018
Building scalable products with WordPress - WordCamp London 2018Building scalable products with WordPress - WordCamp London 2018
Building scalable products with WordPress - WordCamp London 2018
 
Drupal 8 Sample Module
Drupal 8 Sample ModuleDrupal 8 Sample Module
Drupal 8 Sample Module
 
Drupal 8: Routing & More
Drupal 8: Routing & MoreDrupal 8: Routing & More
Drupal 8: Routing & More
 

Similar to Goodbye hook_menu() - Routing and Menus in Drupal 8

Do you want a SDK with that API? (Nordic APIS April 2014)
Do you want a SDK with that API? (Nordic APIS April 2014)Do you want a SDK with that API? (Nordic APIS April 2014)
Do you want a SDK with that API? (Nordic APIS April 2014)
Nordic APIs
 

Similar to Goodbye hook_menu() - Routing and Menus in Drupal 8 (20)

The Flexibility of Drupal
The Flexibility of DrupalThe Flexibility of Drupal
The Flexibility of Drupal
 
What's new in the Drupal 7 API?
What's new in the Drupal 7 API?What's new in the Drupal 7 API?
What's new in the Drupal 7 API?
 
The Flexibility of Drupal
The Flexibility of DrupalThe Flexibility of Drupal
The Flexibility of Drupal
 
Drupal 7 Release Party - Yogyakarta
Drupal 7 Release Party - YogyakartaDrupal 7 Release Party - Yogyakarta
Drupal 7 Release Party - Yogyakarta
 
Feeds drupal cafe
Feeds drupal cafeFeeds drupal cafe
Feeds drupal cafe
 
CodeIgniter PHP MVC Framework
CodeIgniter PHP MVC FrameworkCodeIgniter PHP MVC Framework
CodeIgniter PHP MVC Framework
 
Mojolicious
MojoliciousMojolicious
Mojolicious
 
Grok Drupal (7) Theming
Grok Drupal (7) ThemingGrok Drupal (7) Theming
Grok Drupal (7) Theming
 
PHP pod mikroskopom
PHP pod mikroskopomPHP pod mikroskopom
PHP pod mikroskopom
 
Reactive Type safe Webcomponents with skateJS
Reactive Type safe Webcomponents with skateJSReactive Type safe Webcomponents with skateJS
Reactive Type safe Webcomponents with skateJS
 
Do you want a SDK with that API? (Nordic APIS April 2014)
Do you want a SDK with that API? (Nordic APIS April 2014)Do you want a SDK with that API? (Nordic APIS April 2014)
Do you want a SDK with that API? (Nordic APIS April 2014)
 
PSR-7, middlewares e o futuro dos frameworks
PSR-7, middlewares e o futuro dos frameworksPSR-7, middlewares e o futuro dos frameworks
PSR-7, middlewares e o futuro dos frameworks
 
Introducation to php for beginners
Introducation to php for beginners Introducation to php for beginners
Introducation to php for beginners
 
Rails 2010 Workshop
Rails 2010 WorkshopRails 2010 Workshop
Rails 2010 Workshop
 
Intro to php
Intro to phpIntro to php
Intro to php
 
Convert modules from 6.x to 7.x
Convert modules from 6.x to 7.xConvert modules from 6.x to 7.x
Convert modules from 6.x to 7.x
 
Php with my sql
Php with my sqlPhp with my sql
Php with my sql
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
The State of Drupal 8
The State of Drupal 8The State of Drupal 8
The State of Drupal 8
 
Debugging in drupal 8
Debugging in drupal 8Debugging in drupal 8
Debugging in drupal 8
 

More from Exove

More from Exove (20)

Data security in the age of GDPR – most common data security problems
Data security in the age of GDPR – most common data security problemsData security in the age of GDPR – most common data security problems
Data security in the age of GDPR – most common data security problems
 
Provisioning infrastructure to AWS using Terraform – Exove
Provisioning infrastructure to AWS using Terraform – ExoveProvisioning infrastructure to AWS using Terraform – Exove
Provisioning infrastructure to AWS using Terraform – Exove
 
Advanced custom fields in Wordpress
Advanced custom fields in WordpressAdvanced custom fields in Wordpress
Advanced custom fields in Wordpress
 
Introduction to Robot Framework – Exove
Introduction to Robot Framework – ExoveIntroduction to Robot Framework – Exove
Introduction to Robot Framework – Exove
 
Jenkins and visual regression – Exove
Jenkins and visual regression – ExoveJenkins and visual regression – Exove
Jenkins and visual regression – Exove
 
Server-side React with Headless CMS – Exove
Server-side React with Headless CMS – ExoveServer-side React with Headless CMS – Exove
Server-side React with Headless CMS – Exove
 
WebSockets in Bravo Dashboard – Exove
WebSockets in Bravo Dashboard – ExoveWebSockets in Bravo Dashboard – Exove
WebSockets in Bravo Dashboard – Exove
 
Diversity in recruitment
Diversity in recruitmentDiversity in recruitment
Diversity in recruitment
 
Saavutettavuus liiketoimintana
Saavutettavuus liiketoimintanaSaavutettavuus liiketoimintana
Saavutettavuus liiketoimintana
 
Saavutettavuus osana Eläkeliiton verkkosivu-uudistusta
Saavutettavuus osana Eläkeliiton verkkosivu-uudistustaSaavutettavuus osana Eläkeliiton verkkosivu-uudistusta
Saavutettavuus osana Eläkeliiton verkkosivu-uudistusta
 
Mitä saavutettavuusdirektiivi pitää sisällään
Mitä saavutettavuusdirektiivi pitää sisälläänMitä saavutettavuusdirektiivi pitää sisällään
Mitä saavutettavuusdirektiivi pitää sisällään
 
Creating Landing Pages for Drupal 8
Creating Landing Pages for Drupal 8Creating Landing Pages for Drupal 8
Creating Landing Pages for Drupal 8
 
GDPR for developers
GDPR for developersGDPR for developers
GDPR for developers
 
Managing Complexity and Privacy Debt with Drupal
Managing Complexity and Privacy Debt with DrupalManaging Complexity and Privacy Debt with Drupal
Managing Complexity and Privacy Debt with Drupal
 
Life with digital services after GDPR
Life with digital services after GDPRLife with digital services after GDPR
Life with digital services after GDPR
 
GDPR - no beginning no end
GDPR - no beginning no endGDPR - no beginning no end
GDPR - no beginning no end
 
Developing truly personalised experiences
Developing truly personalised experiencesDeveloping truly personalised experiences
Developing truly personalised experiences
 
Customer Experience and Personalisation
Customer Experience and PersonalisationCustomer Experience and Personalisation
Customer Experience and Personalisation
 
Adventures In Programmatic Branding – How To Design With Algorithms And How T...
Adventures In Programmatic Branding – How To Design With Algorithms And How T...Adventures In Programmatic Branding – How To Design With Algorithms And How T...
Adventures In Programmatic Branding – How To Design With Algorithms And How T...
 
Dataohjattu asiakaskokemus
Dataohjattu asiakaskokemusDataohjattu asiakaskokemus
Dataohjattu asiakaskokemus
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Recently uploaded (20)

Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 

Goodbye hook_menu() - Routing and Menus in Drupal 8