VoIP Service and Marketing using Odoo and Asterisk PBX
Drupal 8 in action, the route to the method
1. Drupal 8 in action,
the route to the method
@juanolalla #DrupalCampEs
2. about.me/juanolalla
Juan Olalla
web developer focused on drupal
at @ideup (@gowex)
Motivation
I’ve recently been contributing to Drupal 8 core
and I want to share with you what I've learned
5. Goodbye to hook_menu() for routing
and for everything else...
Routing
Routes defined in routing.yml and controllers
Menu links
Defined in hook_default_menu_links()
Local actions
Plugins defined in local_actions.yml
Local tasks
Plugins defined in local_tasks.yml
Contextual links
Plugins defined in contextual_links.yml
Breadcrumbs
Implementing BreadcrumbBuilder plugins
Links can be related regardless of the path, managed by machine names.
6. Drupal 8 uses Symfony2 Routing
component
And also uses Symfony CMF
RoutingBundle for dynamic routing
16. Drupal 8 will use PSR-4 for module classes instead
of PSR-0
/modules/foo/lib/Controller/FooController.php
instead of
/modules/foo/lib/Drupal/foo/Controller/FooController.php
drupal.org issue: https://drupal.org/node/1971198
17. Returning a JSON response
nmsaeDuaootolr
aepc rplfoCnrle;
ueSmoyCmoetHtFudtosnepne
s yfnopnntponainJoRsos
casFootolr{
ls oCnrle
pbi fnto hlool( {
ulc ucin elWrd)
rtr nwJoRsos(
eun e snepne
ary'reig = 'el Wrd'
ra(getn' > Hlo ol!)
)
;
}
}
21. Returning a render array
nmsaeDuaootolr
aepc rplfoCnrle;
ueDuaoeCnrleotolrae
s rplCrotolrCnrleBs;
casFootolretnsCnrleBs {
ls oCnrle xed otolrae
pbi fnto hlool( {
ulc ucin elWrd)
$ul =ary)
bid
ra(;
$ul[#akp]=$hs>(HloWrd';
bid'mru'
ti-t'el ol!)
rtr $ul;
eun bid
}
}
23. What about dynamic page titles?
drupal_set_title() is being removed
Use _title_callback in the route defaults
dfut:
eals
_il_alak
tteclbc:
'DuaootolrFootolr:eTte
rplfoCnrleoCnrle:gtil'
Or return it as part of the main render array
$ul[#il' =..
bid'tte]
.
24. Returning a "hello world"
greeting configuration form
at /admin/config/people/greeting
30. dfut:
eals
#Cnrltefl rsos aatfo Dua etacnet
oto h ul epne pr rm rpl xr otn
_otolr 'DuaokCnrleokotolr:okxot
cnrle: rplbootolrBoCnrle:boEpr'
#Ptcneti temi rgo adltDua d ters
u otn n h an ein n e rpl o h et
_otn:'DuaokCnrleokotolr:okedr
cnet rplbootolrBoCnrle:boRne'
#Gttebidommto o acasetnigaDua Fr
e h ulFr ehd f
ls xedn
rpl om
_om 'DuaokFroketnsom
fr: rplboomBoStigFr'
31. Entity defaults
dfut:
eals
#Cl bidomfrteato ett adoeainFr
al ulFr o h cin niy d prto om
_niyfr:'cinad
ett_om ato.d'
#Dslyals o txnm_oauayett ojcs
ipa
it f aooyvcblr niy bet
_niyls:'aooyvcblr'
ett_it txnm_oauay
#So tefl ve md o aue ett ojc
hw h ul iw oe f
sr niy bet
_niyve:'srfl'
ett_iw ue.ul
32. Passing parameters to the controller method
#Prmtrdfndi tept
aaee eie n h ah
pt:'cmet{omn}apoe
ah /omn/cmet/prv'
#Dfutprmtr
eal aaee
pt:'amnsrcuetps
ah /di/tutr/ye'
dfut:
eals
ett_ye 'oetp'
niytp: nd_ye
#Otoa (ohi tept adwt adfutvle
pinl bt n h ah n ih
eal au)
pt:'amncni/erhpt/ky}
ah /di/ofgsac/ah{es'
dfut:
eals
ky:NL
es UL
34. Parameters are upcasted to entities by entity type id
cnatproa_ae
otc.esnlpg
pt:'ue/ue}cnat
ah /sr{sr/otc'
cnatst_aectgr:
otc.iepg_aeoy
pt:'cnat{otc_aeoy'
ah /otc/cnatctgr}
casCnatotolr{
ls otcCnrle
pbi fnto cnatesnlaeUeItrae$sr {.}
ulc ucin otcProaPg(srnefc ue) ..
pbi fnto cnatieae
ulc ucin otcStPg(
CtgrItrae$otc_aeoy=NL){.}
aeoynefc cnatctgr
UL ..
}
35. Explicitly converting parameters into entities
fohloue:
o.el_sr
pt:'{o_sr/el/ue}
ah /foue}hlo{sr'
#eg //el/
.. 1hlo2
dfut:
eals
_otn:'..Footolr:elUe'
cnet .oCnrle:hlosr
otos
pin:
prmtr:
aaees
foue:
o_sr
tp:'niyue'
ye ett:sr
#{o_sr wl as b pse a aue ett
foue} il lo e asd s
sr niy
36. Regular expressions requirements for parameters
#{p prmtrcnol b 'nbe o 'ial'
o} aaee a ny e eal' r dsbe
vesu.prto:
iw_ioeain
pt:'amnsrcuevesve/ve}{p'
ah /di/tutr/iw/iw{iw/o}
rqieet:
eurmns
o:'nbedsbe
p eal|ial'
#{sr prmtrcnol b anme
ue} aaee a ny e
ubr
ue.iw
srve:
pt:'ue/ue}
ah /sr{sr'
rqieet:
eurmns
ue:+
sr d
38. Drupal 8 doesn't implement Symfony2 Security
component, and manages access from routing
Symfony uses a separate Security component for controlling route access
that is too complex to integrate in Drupal 8.
Drupal 8 implements access checkers services, which will ignore, allow or
deny the access based on a requirement on the route.
40. Still setting permissions in hook_permission()
fnto nd_emsin){
ucin oepriso(
rtr ary
eun ra(
'diitrcnettps = ary
amnse otn ye' > ra(
'il'= t'diitrcnettps)
tte > (Amnse otn ye',
'etitacs'= TU,
rsrc ces > RE
)
,
)
;
}
I asked for hook_permission() in IRC #drupal-contribute
<timplunkett> juanolalla: no plans to replace that AFAIK
41. Common access checkers provided by Drupal core
#Awy ps,o 'AS't awy fi adbokacs
las as r FLE o las al n lc ces
rqieet:
eurmns
_ces 'RE
acs: TU'
#Rl Issprtdb ""(n rl)o ""(l rls
oe D eaae y , ay oe r + al oe)
rqieet:
eurmns
_oe 'uhniae,di'
rl: atetctdamn
#Pse i teue i lge i
ass f h sr s ogd n
rqieet:
eurmns
_sri_ogdi:'RE
ue_slge_n TU'
42. Other useful access checkers provided
#Cek i ayal(/)teseiidmdlsaeeald
hcs f n/l ,+ h pcfe oue r nbe
rqieet:
eurmns
_ouedpnece:'oe+sac'
mdl_eednis nd
erh
#Cek acs t teseiidett_yeoeain
hcs ces o h pcfe niytp.prto
rqieet:
eurmns
_niyacs:'oeei'
ett_ces nd.dt
#Cek acs t teseiidett_yeett_ude
hcs ces o h pcfe niytp:niybnl
rqieet:
eurmns
_niycet_ces 'aooytr:txnm_oauay'
ett_raeacs: txnm_em{aooyvcblr}
43. Declaring your own custom access checker
Declare your requirement checker in .routing.yml
rqieet:
eurmns
_o_hl_o_as 'RE
yusalntps: TU'
Register an access checker class as a service (in .services.yml)
srie:
evcs
acs_hc.o.adl:
cescekfognaf
cas DuaocesGnafceshc
ls: rplfoAcsadlAcsCek
tg:
as
-{nm:acs_hc }
ae cescek
44. Writting your own custom access checker class
casGnafceshc ipeet SaiAcsCeknefc {
ls adlAcsCek mlmns ttcceshcItrae
pbi fnto apiso){
ulc ucin pleT(
rtr ary'yusalntps';
eun ra(_o_hl_o_as)
}
pbi fnto acs(ot $ot,Rqet$eus){
ulc ucin cesRue rue eus rqet
$eurmn =$ot-gteurmn(_o_hl_o_as)
rqieet
rue>eRqieet'yusalntps';
i (i_ra(hts,$ot-gtcee()
f !nary'tp' rue>eShms)
& $eurmn = 'RE){
& rqieet = TU'
rtr sai:DN;/ o rtr sai:ALW
eun ttc:EY / r eun ttc:LO
}/ N rtr masohrcekr wl dcd
/ o eun en te hces il eie
}
}
45. Access mode options, checking any or all requirements
nd.d_ae
oeadpg:
pt:'nd/d'
ah /oead
dfut:
eals
_il:'d pg'
tte Ad ae
_otn:'DuaoeCnrleoeotolr:dPg'
cnet rplndotolrNdCnrle:adae
otos
pin:
_cesmd:'N'
acs_oe AY
rqieet:
eurmns
_emsin 'diitrcnettps
priso: amnse otn ye'
_oeadacs:'oe
nd_d_ces nd'