DrupalCon Prague 2013

El nuevo sistema de enrutamiento de Drupal 8

Tim Plunkett  · 

Transcripción

Extracto de la transcripción automática del vídeo realizada por YouTube.

all right it's one o'clock people will probably file in from lunch but I might as well get started there's a lot to talk about and everyone hear me okay yeah and everyone can see so this is Drupal 8 routing my name is Tim Plunkett I am a developer

at Stanford University Graduate School of Business and a core subsystem maintainer mostly with the views in core and Drupal 8 but wanted to talk today about the new routing system for Drupal 8 so first show of hands who has never written hook menu in a module

before anyone okay I'm skipping all of that so I'm assuming you all know that so that was only like two or three people raise their hands hopefully you'll catch up as we go who here has yet to use I really started already using Drupal 8 and looking

at it so about half the room okay first things first hook menu did too much I don't think anyone will argue with that it was responsible for routing menu links local actions local tasks breadcrumbs contextual links and even the overall menu hierarchy which

is not necessarily a function it was just a kind of hidden feature that you got but that's you know six different things one hook it's it's a lot hook menu is dead in Drupal 8 or will be shortly there are a couple things in this presentation that

have not yet happened but they will this week and I will point them out which ones haven't happened yet but already just like an hour ago the breadcrumbs are now completely path based we'll get into that later on so routing a RAL is just a map from

a URL to a controller here's an example of a d7 hook menu this is borrowed from the pants module which is a nice example module so you have the path admin config people pants then you have a title pants the description is to administer pants page callback

you will get forum page arguments is a string pants setting that will be the function name that is called the access arguments are just administer pants and this callback happens to be located in a separate file so this is all familiar to everyone yes great

ok this is the Drupal 8 version of hook menu I said hook my news about to die it's about to die right now if you're working on porting your modules you'll need this for any visible menu links but but the only thing that really remains of the title

and description you'll notice we have a route name here and it's just pants dot settings it's a machine name and you can choose it's not magically named at all so this is the pants routing yamel this is the part that takes over the routing

aspect of hook menu from Drupal 7 so there's the machine name again you have pants settings and the path so the path is the same thing that you used to put in the items is a key in the array of the menu items and then there's two other sections defaults

and requirements you'll see that this is exactly the same as they are here just the instead of access arguments administer pants we have permission administer pants as a requirement and the title is also here the new thing is this form Drupal pants form

pen settings form so that's all p sr 0 and names PHP 5.3 namespaces not covering that but the point is that this is the form that we want to be used as this instead of the old page call back so here's the d7 version of the page call back it was just

a settings form it determined what type of pants and in this case it has no no pants option so you can see we have a just a regular form API and then this magical system settings form so here's the Drupal 8 version this is the what we call route controller

and I'll cover a bunch of some of this stuff more in depth later but we have this it's a form and it has a build form method and we're doing the same thing we have form API all the way down and then we have build parent build form that's just

you know a proper object or in where the parent class config form base contribs most of the magic that system settings form used to do there's a couple other parts of the form base that I've cut out from this because I'll talk about later so don't

just copy and paste this and hope it works but that's the general idea is instead of a single function in a file you have a method on a class so menu links we talked writing was the first part the second part of hook menu we're the menu links this

is the part that's not done yet there's a patch to do it and it's in the process but these are the ones that are physically visible on your site so the new way to do them will be called hook default menu links and it will just be what route name

it's for the title of the description and you specifically have to tell it which its parent is so before if you were defining many links it just if you had you know admin config the development and you know deadening config development testing those that

would be the child of it just magically based on the path structure now you can have links be the children of anything just based on its parent and it's not restricted to the path this will let you kind of build better menus where you can just pull something

in you can give it the URL you want not the URL that you needed to be with an extra / at the end so if you want more information on it that's the issue and it should probably go in maybe this weekend local actions local actions are the buttons at the top

of the page they look like that that's stupid 7 and that's triple eight so a local action it used to be in hook menu and now it's in a yamo file like a lot of our code is in the ammo files now including the info files and the routing files in the

local actions files in love with task files and contextual links files and the config files and the config schema files so there's a lot of young long thankfully it's really easy to write its whitespace sensitive it takes keys and values and that's

it so you'll see just as simple the this is actually the exact one I was just showing you the add content type and it takes it appears on the node overview types route and it has a title of ad content type and route name where it links to is the node type

add to add a new content type so you'll have just a file full of these just by themselves and thank then you can keep all of that those little buttons separate from the actual routes themselves and you just reference them the other nice thing is in Drupal

7 and before this you could only kind of put that on one place you had this this button can only appear on this page now this you'll see appears on is an array you can just list as many rounds as you want here and that button will show up on all those

pages local tasks they're kind of very very similar in code to local actions but they're completely different in implementation and use case these are the ones they're sort of related tasks not necessarily a new thing so those are the local tasks

on the views page for view settings so you have like list your settings basic advanced these this is the definition of them it gets a little small at the bottom of the slide but you just have a machine named completely arbitrary the route name that it links

to and the title and then each of these has a route ID you'll see that these all have these two have the same route ID that's the list in settings and these two have the same story parent ID which is the the other one so they're all four of those

are grouped together but using the different the parents and the roots this code might change a little bit in terms of the naming we just it's this is matches exactly what was in menu Inc in Drupal 7 but as we all know menu Inc is pretty confusing so there's

work to rename those keys but it the idea is that it will keep it separate from all your actions and all your other things and you can just have a file of all your local tasks contextual links those are the little you know you can see them they pop up generally

on a an entity we don't really use them anywhere else but you can put them on anything and their little helpers for that little piece of of content or that that block or menu what not and those are also going to be defined enamel this is also not yet in

core but it will be very very soon and it's exactly the same you have a route name which is just the machine name and then you have the title and these are grouped so that you can have them you know in a specific order and all of these support weights

actually I think I had yeah way down at the bottom of this it says wait 10 they all have weights so you can you can the order of the yamo file is not necessarily important if you want you can list them in any order or any grouping and just change the weights

okay breadcrumbs this is the thing that just went in today so breadcrumbs are a huge problem for most people I don't know of anyone that actually really uses the breadcrumbs that are provided by default in core there's about dozen plus different decent

[ ... ]

Nota: se han omitido las otras 4.413 palabras de la transcripción completa para cumplir con las normas de «uso razonable» de YouTube.