DrupalCon Los Angeles 2015: Routes, controllers and responses: The Basic Lifecycle of a D8 Request

guys when i’m gonna move around a little bit hey who said hi hi awesome this is why i love drupal conferences the first one i ever went to I was talking about B hat how many people have heard about be how to use B hat yeah like it’s amazing like the ridiculous number of hands and I did not make be had I was just talking about it and I showed something in the middle and the whole room broke out and applause I was like this this day like never happened before I was like this is awesome so I think some people are so grabbing last-second tacos but i’ll let them wander in I come from the symphony world this is my third drupalcon which is amazing for me and today we are we talking about the Drupal 8 requests life cycle because amazingly awesomely I’m actually qualified to talk about that because there’s so much in common between requests and responses and Symphony and requests and responses in Drupal 8 which personally I love because I can open up Drupal 8 and go oh yeah oh yeah see what they’re doing there that’s clever I like that makes sense to me so I come from the symphony world I am a writer for tutorials on a website called Camp University com how many people have seen stuff on Drupal eyes me yeah good number of hands yeah you might see a couple of are things up there too so they’re friends of ours we have a couple of our object oriented and stuff over there you’ll hear my my wife leanna’s voice where is leanna leanna she’s late we’ll just we’ll just wait then just kidding usually we I sell her out in some way well she’ll come in the middle if I see her I’ll sell her out then so let’s get into things and hold on one second well actually it’s okay I totally didn’t hook up my clicker but I’m not gonna want a very far anyways so let’s start off so I feel like making a page in Drupal a how many people have done at least a little bit with Drupal 8 at this point that’s very very good so I was like half I think maybe more than half of the hands which is actually that’s really good audience if you haven’t I’m not going to leave you behind either will cover the whole basis so we’re going to create a page so we need three steps module route and controller and we’re going to go do all three as indicated by the empty check boxes so step one making module pretty simple even if you haven’t done a second of Drupal 8 you’d be like yeah it looks familiar cool so the top is cutting off barely that’s the modules directory on top and then I’m creating a module called Dino underscore war because pretty much everything we do in my world we just somehow work a dinosaur into so don Aurora and then we enable it which you can do apparently with rush or on your web interface or probably hack into the database if you’re feeling particularly brave and then we’re done so let’s get into interesting stuff a route so page is always these two parts people that have done some stuff with Drupal 8 or like yes route and controller so we’re gonna create a route and a controller it’s always just those two ingredients to create a page so step one is going to be the route so when you have a module in Drupal 8 you can always have the module name dot routing dot yml and then this is going to be read the route is only two parts you’ll see more kind of fanciest things added to a route and Drupal 8 but you want to focus on the path which is the URL to your page so hook menu right looks familiar and then the second part is going to be the function that builds that page and probably most or all of you’ve heard the word controller before and i like to say controller is a fancy word for a function that builds your page so we have the path and then we have this underscore controller keys just the way that you indicate what functions going to build your page so that’s it for the route so step one is we created this route it’s got the path it’s got the name to that function oh by the way I should say and I realize that that greenish yellow is may be hard to read in the back the underscore controller is a class named full class name colon colon and that a method name then it can be well not exactly anything you want buts class name colon colon a method name so it’s kind of a name of a function so route check controller so we open up our controller and then we just build our page there’s two things I the highlight that are important here one is that the path is we’re inside of our module so dino underscore roar and then you create an SRC directory then a container directory then our roar controller dot PHP just because that’s I called my class roar controller in the previous thing and then two important things to highlight here are one whenever you create a class any module in Drupal 8 you will always have a namespace that starts with Drupal and then the name of your module it’s just a rule there’s a reason for related to autoloading but you always start with

Drupal and then the module name and then after that it’s just whatever directories you want so I decided because it’s a standard to create a controller directory and so therefore I have drupal / Dino roar my module name / controller and if I created like three more directories deep I would just add more namespaces so your namespace has to match your directory structure that’s thing number one that’s important thing number two that’s important is that our job as developers is always just to create and return a response so like imagine roll Java developers a request comes into our server we create a response that’s just the basics of the web and it’s no different than symphony or Drupal 8 or anything else so this function here this R or function that builds our page its job is to create a symphony response object and return it so I’m going to break this rule later but your controller always creates and returns a response object and the response objects pretty simple it’s just the text you want and then you can set headers on it but basically it’s just the content that you want for your page so route check controller check and there’s our beautiful page you guys like my styling skills there’s no other HTML behind that I returned the response roar that’s literally just the words roar on the page it didn’t get wrapped in any way by a layout or anything like that there’s no theming going on or anything magic behind the scenes it’s really straightforward so then really Drupal 8 is kind of this structure we talked about Drupal 8 is a framework it’s really a routing controller framework we start with the request and goes through the routing the routing calls the controller which we write we build the phone in the controller and then we return the response from the controller and that’s it so it’s a routing controller framework I use the same slide by the way some of you guys if you’ve seen me speak before I use the same slide to describe the symphony framework and also the cylex micro framework because they all have this fundamental thing exactly in common all right so there’s actually one other small a little detail and if you’ve done Drupal 8 a little bit you’ve probably seen this I changed the path here to be / the / Dino / says / curly brace count curly brace so that’s the way you do wildcards this would match the Dino says / anything 1015 foo whatever you want so you can do that in a route okay not dissimilar from the way you could do that something like that in Drupal 7 except it has a name curly brace count the important part which is going to be even more important later is that this Maps is an argument to your controller function so the fact I have a curly brace count means that in my controller function I have a dollar sign count and that’s really really handy but for what we want to do today really important because we’re going to circle back to what’s causing that a couple times cool so we have our basic page and the whole goal of this is to be like okay but how does that work behind the scenes what’s actually going on to make that flow so we’re gonna put a couple debugging tools in and Drupal 8 has some really amazing ones already so the idea is this is another thing I really like about Drupal 8 every single page whether it’s some random admin interface has a route and a controller that fuels it that’s no different than a route and a controller that we build so if you can find the route in controller for some random page you can just open up that route and controller and see how some random core Drupal 8 page is working if you want to so could we get a list of all these routes because we know there are a lot of admin interfaces and if they all have routes can we get a big list of them well is actually a couple different ways to do that one how many people have seen Drupal console project at this point okay cool I’m there was like a quarter or third so I’m happy that a lot of you guys are going to see this this is a command line tool so you install it once it’s kinda like well it’s like brush but it has kind of a slightly different a more kind of debugging and code generation but if you know drush command line tool this gives you a command line tool called drupal and one of the things that you can do with it among many is a command called router colon debug and it just prints all of the routes in the system so you can’t see it’s caught up slightly on top but I’m actually counting all of them is 339 routes in a kind of standard Drupal 8 beta ten installation and if you run it just by itself it’s just going to print all those you can just like run down there and basically see hey when I’m at this URL here’s the route that it is and by the way the thing on the left is the machine name for the route so every route has kind of an internal machine name that you use to refer to it so if we wanted to let’s say that we kind of inherited a project and we realized we had this dino page and we didn’t know what was fueling that we’re like what code is causing this page to

happen we could go in here and just run a router debug pipe that through grep for / dino and it tells me hey there’s a route whose machine name is dino underscore says and then if i run it again with router debug in that route name that machine name it’s going to give me all the information about that route including the most important part of course that your controller is this Drupal Dino roar controller roar controller colon colon roar so like yes that’s the function that’s building my page I’m going to go open that up and do whatever work I need to do so let’s actually go up one level from this so this is awesome can we get something like this on the web how many people have seen the web at profiler for Drupal 8 even less hands okay even better that’s why it’s why I become these conferences by the way I take no credit for any of this except being the guy that happens to be in front of you today telling you about them I do none of the work on these projects other than I guess advance Eliza ng them because I think they’re amazing so the web profiler is a contributing is Luka in the room no I know he’s here because I stalked him on drupal.org last night and saw that it says he’s at this conference anyways he is the main guy behind us so you can thank him off his handle up later as well so it’s just a normal module so you download it like you normally do it has two dependencies on two random JavaScript files that you need to put inside that directory but it’s just a normal module so that’s the only reason I have this slide up it’s nothing special at all and then using your favorite method of enabling modules you enable that module the Drupal thing I was just talking about actually has a way to enable modules you know so there’s just just kind of showing that off here but nothing special at all and as soon as you do that every single page that you load on the site gets this magical little bar along the bottom well every single HTML page this will not be on your JSON pages you’re welcome and this has some incredible stuff probably most importantly if you hover over the little 200 hopefully it’s a 200 status code if you hover over the 200 it tells you the controller and the route for this page that you’re on right now so you can surf you a page you be like what’s the controller that fuels this page right there i also have i’m not going to talk about everything this has a million things also has database queries you can see how many database queries are on your page and as you’ll see in a second you’ll be able to see what those database queries actually are when you install this for a good time click all the pretty checkboxes it actually has a whole bunch of things that collects not all of these are checked by default so if you really want to like figure out how things are working you open up the settings and make sure you check all the boxes all right cool so from this screen which is the same I showed a second ago you can click any almost any of those icons on the bottom and it will take you to another screen called the profiler the profilers like the web debug tool bar like done crazily it’s like a hundred times more information along the left you have all these tabs that just show you different information this is happens to be the one for the database shows you all the queries and you can even like sort it by who was making those queries so you can figure out where they’re coming from but what I’m really interested in is a couple of things one this is another way to see your routes so it has a routing tab like here’s all the routes in your system so similar to what we saw in the command line a second ago this is one of the most important things in my opinion that you can have in symphony or Drupal because we have this in symphony as well this is the timeline this is everything that’s happening between request and response including how long they’re taking the purpose of this is for profiling so you can figure out why things are slow but it has a secondary thing which is you can see what’s actually happening like what are the magical layers that are causing things to happen between your request in your response these are nothing’s really small these are class names so you can be like Oh up in the beginning there’s something called an ajax subscribe or like I wonder what that’s doing boom open it up and find out one of the rules with sort of this modern object oriented PHP development is that a class name is always in a file of the same name so you can if you see Ajax subscriber up there and want to look up to that look that up just if you have some lot of editors have hot cuts hotkey shortcuts just to look for Ajax subscriber dot PHP that’s going to be your file that holds that class so I do that all the time so we’re going to come back to this but it’s really really I mean this is amazingly important so kind of like we’re saying let’s I want to dive in and honestly this is how i approach Drupal 8 is an outsider I was like I want to see how these things actually work behind the scenes so let’s say we go to / admin / structure you know like the main structure link on the admin bar how does that page actually work well it turns out as promised you go there web debug tool bar on the bottom and it tells you the exact controller that runs that so you can open up that controller the class is called system controller so again system controller dot PHP would be the file if you were the type that likes

to open up things and see what’s going on inside you can open that guy up and actually see what’s going on behind the scenes all right so we have kind of this route controller flow and like I’m going to talk about services in a container for like 30 seconds because other people are talking about them but they’re so fundamentally important have to at least mention them so you’ll hear the word service a lot and if you haven’t already been told this by me or somebody else’s service is an fancy name for a useful object like if I you an object that makes database queries like a connection object that’s a service and that’s basically it so when you see that word just like that’s a useful object in Drupal 8 it’s just your plates basically made up of useful objects so every job that you do in Drupal there’s actually some object that does that for example on Drupal 7 when you wanted to translate something there was a function T interpolate there’s an object called string link language translator I think it’s actually just an object so if you want to translate something it’s all about getting that object and then calling a method ons on it so the same basic idea but there’s all these useful objects there’s also shortcut methods so you also still see that T method all over the place but it’s just going out to that useful object in reality so Drupal’s made of these useful objects that are floating around and in the container is just a place to put them in there’s more to it than that but you’ll see the service container dependency injection container thing it’s basically a big bucket almost like an associative array that holds all of your useful objects every object in there has a nickname you’ll see that in a second like a little machine name so if you want to get an object out of container you just ask for it by its machine name like give me the string translator and the language translator I can’t read what it’s called and it gives you that object back so in duple a like what is Drupal 8 your plates really just a bunch of these useful objects so that service container comes populated with a whole bunch of them and you can see them again on another tab inside of that profiler so this is the services or the container tab on that profiler and there’s 485 as of today services that are in the container these are basically this this is amazing because these are basically tools and if you go in here you can see what tools you have available to you and you can see the nickname on the left there like there’s one called access underscore check csrf so if you need it for some reason to actually check CSRF protection manually that’s the object that you’re going to want to get out of the container and then really important to get it for debugging while not debugging is that my goal this is to take you guys kind of a step deeper than you might normally need to go to figure out how things are going so there’s a file so all these services that are in that container are defined somewhere and for the most part like all the coarser vistas are defined in one big file which makes them really easy to look at and it’s can’t see up there but it’s the yeah just core its core / cord services dot yml this is big long file that just shows you all of those service and all the classes are behind those services so for example here there’s a service called state which is kind of like a nice little key value store thing so if i give you its nickname is literally state if i give you the service container you can ask for the service whose ID is state and it will return you this object and then you can use that object so if you wanted to set something nice little key value store from a controller it’s just a matter of asking for the state service and then calling whatever methods are on that object all right so we found like one slight level up we saw this request controller response type of thing but now I just want you to realize there’s this container floating around full of useful objects and really whenever you do anything whenever you get any work done it’s actually being done by a service inside of that container so even as we’ll see you later like the whole theming layer is just a there’s a there’s services in a container that actually taken care of that theming layer all right so events so I’m going to go up once light level above here so just like with hooks drew bleh also has something called events if you’re not familiar with them just think of them they have the exact same purpose it’s a different pattern on accomplishing the same thing where when something happens in core you want to have some way to hook into that process so it’s done a little bit different but it’s the same exact thing and for the most part I could be a little wrong about this for the most part I believe most of the hooks in Drupal 7 are still hooks they’re just now a few additional events so the hook system hasn’t been replaced by the event system we’re just putting certainly put more stuff in two events alright so a little bit unlike Drupal set on like hooks hooks least from what I’ve seen are about getting something done like hook menus about like hey we’re building the menu the kind of core events in Drupal are more about timing it’s about like hey as we’re going to this request response process if you want to hook really early into the process the event that

you want to kind of listen to that you want to attach to is called Colonel dot request it’s just early so it’s about timing the last one is called Colonel dot response so if you want to do something like wait the end of the request response flow that’s what you’re going to actually hook into I’m not going to go specifically into how you do that but just know just like hooks it’s about basically creating a function and then there’s a mechanism to tell core i want you to call this function when the colonel that response event happens so call me at the end of the request response flow cool and one of the questions I’ve gotten already this conference repeatedly is can I see what listeners there are to those events because that’s the problem with hooks and events it allows you to have this nice decoupled code in a sense but you have no idea what’s going on you’re like whoa wait but what listeners are there to this like and what are they actually doing so the web profiler has that as well there’s an events tab and this is massively important because if something magical is happening there’s about an eighty percent chance that it’s happening because of a listener so you can go here and actually see what the listeners are so almost that entire screen there our Colonel dot request that’s the first one so those are listeners that are happening at the very beginning of the request so before our controller is called so when I first went into Drupal 8 to see how it worked I basically went here and just started opening up these classes I was like well what does this do what does this do all right so we have the container floating around and we have these events happening okay so I’m gonna go back to this page here so I’m back on my page that says the / dinos / says / 10 or whatever wild card number i wanted to put their cousin or the URL and this had the little curly brace count thing at the end of it oh yeah and one problem with this page is because this is not a full HTML page I don’t have my cute little web debug tool bar anymore want want no more debugging tools and this is the same if you had a JSON response you’re not going to have a web debug tool bar and that is the end of my presentation getting know there’s a way around this if you go to the profiler for any other requests there’s a link on the top that says view last 10 which takes you to basically a search because as you requested by the way this is of course only happening like in dev mode are where I guess where you have your web profiler enabled so as you’re making requests that web profile is collecting information it’s storing in the database so it’s not just a one and done thing it actually keeps track of all those so you can go here and actually I can actually find the profiler for that request even though it didn’t have the web debug tool bar again this is really important if you’re building on an API and you’re hitting the end point you’re like oh something broke or something and I want to see more information about what happened so I click into there and there’s a tab called request and in the request it has something right at the top called request attributes which is one of the more important ideas and Drupal 8 if you are trying to understand things at a deeper level if you’re not you may not ever really realize this is happening but again we’re sitting here so I’m going to get us down one level deeper so one thing to notice here in the request attributes so I’m going to put my route back up there is that in my route path I had a curly brace count curly brace wild card that goes into my request attributes I have a count down there whose value is 10 because 10 was what was in the URL so far I want you to know that these requests attribute thingies exists but you should not be making a connection about why that’s important just know that they exist and they’re populated from the routing another key that’s in there is the underscore controller so more broadly anything that you put under the defaults key in your route ends up in your request attributes again why do you care that it’s in the request attributes we don’t have that piece yet but the routing put stuff into the request attributes there’s also a couple of other things and this is going to be important Drupal actually is going to put other things into the request attributes so we have something down there called like the route object and another thing called access result which is kind of like something that keeps track of whether or not you should be denied access to this page or not so actually all right so why is this important well it turns out that remember we had curly brace count and our route and that passed to dollar sign as an argument to our controller turns out what really happens is that the curly braces near route become request attributes as well as anything under that default key become request attributes anything in your request attributes can become an argument so it’s actually two steps behind the scenes so the fact that I have a count request attribute their means that can have a dollar sign count or I mean not even thinking about how these or caring about Holly’s are populated the fact that I have a underscore route and an underscore access underscore result request attribute means I can have those

as arguments to my controller so this the request attributes is actually the thing that feeds arguments into our controller and just to kind of like drive that point home the well there’s actually two but there’s basically one extra thing that can become an argument to to your controller so basically if you put something as an argument your controller that is not in the request attributes you’re going to get a huge exception it’s like dude I’m trying to help you out but you got to meet me halfway I don’t know where you got the idea that you could have a fubar variable to your argument your controller so normally that’s just going to blow up the one exception is the request object because sometimes in your controller it’s convenient to be able to get the request maybe because you need to read query parameters so the one exception of the rule is you can have a request arguments as long as its type hinted so the capital our request with symphonies request class that’s a way of kind of signaling to the core that like this arguments request object and once you’ve done that as one of the highlight another way to get that count curly brace thing is actually to say request attributes get count so that’s how you get those requests attributes get nothing to do this when I take us a level deeper here and be like it’s these requests attribute things that are actually important now real quick the request object is really really simple objects you guys are going to use it in reality in your code to get information off the request like headers query parameters cookies things like that the request object has a number of public properties and they’re forgetting things like that so if you want to get a header like remember now like a an accept header off of the request then you can say request arrow headers arrow get and then you pass it the argument except okay so there’s a bunch of public properties that like deal with real HTTP requests kind of stuff the one odd ball is attributes you can see it says attributes no equivalent attributes is like one random spot where we just kind of like shove things into the requests that are specific to our application and again the importance and reality and Drupal 8 of that request attributes as those become the arguments to your controller all right so the one thing out of here now is we serve know that the arguments to our controller actually populated via this request arrow attributes and kind of where I’m going with this as far as understanding how the core works is that so far the only thing that we know that really puts things into request attributes is the routing layer when routing runs any curly brace things or anything under the default ski of our route go into request attributes but there’s nothing stopping any listener so any hook to the kernel that request event or the kernel that controller event because those both happen before the controller there’s nothing stopping any of those functions from adding more stuff to the request attributes so it’s not that it’s not robbing is not the only way that we could get stuff in there and an example of that is the up casting of nodes how many people like you know what I’m talking about with up casting of nodes maybe because I’m calling it the wrong thing but I think that’s what what what the term in terminology is so what I have here is I have another page and its URL is the slash Dino / curly brace node okay and it points to another controller method called in my roar controller called node so so far this is basically it’s really simple just like the last route I made and inside my controller because I have curly brace node I’m past dollar sign node simple and that’s just the node ID so whatever is in my URL there’s no magic at all happening right here and then I’m just printing out the node is five or whatever I put in my URL cool totally straightforward if I go into my room my request after this thing I see that as a node is one and so that’s what value is being play passed as an argument all right so but we can actually make this a little bit more interesting if you take that route and you add an option ski on the bottom and by the way there’s going to be an easier way to do this in a second but this is actually what’s happening behind the scenes you had an options parameters key and then below that I have a node because of my curly braces called node so that’s why that’s called node down there and below that I say type call an entity call a node what I’m basically saying here is that I have an argument I have a curly brace in my route called node and it’s of type entity colon node you’re signaling to core that this is not just an ID this is a entity type of node so I want you to actually go like query for that and give it to me as an argument so that’s the only part that changed that in there is the kind of the options thing you’re kind of attaching more metadata to your route to signal to core what’s going on with some of your curly brace parts to view out and as soon as you do that that node is now an object it’s not the node object inside of there so I can call get title on it okay this is called called up casting

you’ll see why in a second there’s a slayer inside of Drupal score called pram converters and this is a layer where you can basically take any of your curly brace things in your route and really anything that’s any request attributes and change them before your controller is called and so there’s an existing one in there that does exactly what I just said we’re going to say hey this argument here is actually an entity of this type so go query for that for me and pass that object in and what’s cool about this is you can see this inside of cord out services dot yml that’s where all the services and you can go in there and search for purim converter and i’m not going to talk about what the tags thing is there it’s a little bit more advanced but if you search for the tags of pram converter these are all of the classes that are responsible for doing that pram conversion these are all the things that may be changing your arguments to something else like taking the note ID and turn to a node arguments also in the web profiler if you go to the services tab they actually have one of their search boxes as you can search by tag so you can actually see all of the things in the system including maybe contribu installed that might have a pram converted like that and this is going to be a theme that you’ll hear over and over again with Drupal 8 when you want to get something done like I want to have a custom block plug-in or I want to have a custom pram converter the way you’re going to do that is you’re going to create a class and it’s going to implement an interface so in the case of a pram converter if you want to create your own you just create a class in your module and you have implement parameter interface and the interface there is pretty simple like the important method there is called convert and it basically passes you like the argument node it says you can change this if you want to and that would be your opportunity for example to maybe query for that I mean that’s already done in core but you can do whatever you want in there and it change those arguments and and up cast them or whatever you want and then you’d register this as a service and give it that same parameter tag oh yeah and if you do this that the first argument to convert is the value like 10 because you’re maybe that’s like the value of the argument the second thing there is definition so it’s going to get past anything that’s you’ve kind of put in your route there so you could randomly this is a little bit more advanced I realizing you’re not necessary going to do this it’s more of like I want you guys to understand like what’s going on in core you guys could instead of type of colon entity colon node you can put whatever other random information you want there in a route and have your custom pram converter read that information off the route and do something different to change one of your arguments alright cool so same things we have before but now with this idea of / am converting sweet like going back to that simple request controller thing we had this you know our routing ran it called our controller and it passed in any curly braces as arguments and it’s still true we now have the opportunity to maybe change what those arguments are like change one to the node object that represents node number one alright so I promise there was an easier way to do this and there is so this is that same route before but it took off that ugly options thing and this is the way you’re actually going to see it done most of the time inside of Drupal 8 this is why it’s called up casting because you can take an argument like that like node and type int it so with well entity interface or you can type in with node whatever your actual classes for your entity type and that’s going to give that same signal as we had in our route before so the same parameter system instead of reading those options from the route and saying you want a node is going to actually look at the type int there and say oh looks like you want a node or whatever other type of thing it’s going to query for that for you so this is the same end result alright the render array which i think the render array has kind of a bad connotation in Drupal I said render right before like yeah like it’s kind of fun can return an array with a bunch of stuff I mean that the hashtags are a little weird but I think it’s kind of a cool thing anyways how the heck does that work so we’re going to take that same controller because now we’re being passed the note object next you can see I changed my type int from entity interface to node because I know it’s going to be a node you can use either now and return my nice little render ray with hashtag title and here’s me symphony guide doing some Drupal stuff I don’t totally understand everything but I know I can do this which is cool and when I go instead of getting just like the words on the top of the screen I’m all of a sudden decorated inside of my theme and my content shows up in the middle of the page yes so we you guys all understand this in fact way better than I do but how does that work because it shouldn’t work because your controller should always return a response so who the heck created the response here we have to create a response returning an array is not sufficient enough inside of Drupal or Symphony framework to actually send

that back to the user so somebody did that the answer is because when it’s magic it’s probably an event listener and what profile or even tells us what event listeners we have which is awesome it’s an event listener and it happens to be an event listener on Colonel dot view which is the fur event that happens after your controller is executed so if you go into your timeline this is just a little bit of the timeline so let’s say I’m debugging this i refresh I click into my timeline there’s part of the timeline and see the the first big dark bar there is if you look to the left of small text there is Colonel that view that’s actually telling you how long all of the listeners for the colonel that view event are taking and below that you can see all of the listeners for the colonel that view event in particular the one giant green bar right below it is for a class called main content view subscriber so there’s the colonel that view event there and then main content view subscriber so already you’d be like mmm okay what’s this main content view subscriber thing doing there so the main content view subscriber is actually the thing that takes care of the render array and it does that by turning your array into a response which I’ll show in a second based on the format because of course we have HTML but we also have JSON and there’s some Ajax and modal e type of things to inside of there’s a couple different formats and it does this with something called so before I talked about the parameter system this is called the render dot main content renderer system it’s not quite as short as the other one it’s kind of the same thing so that listener I just said you could actually open up that class and what you would see it doing is actually looking at what format the client wants is the client want an HTML format or they want a JSON format and then offloading the work to another class and this is again this is back in court out services that yml you can see this class here has a tag for rendered main content render with a format of HTML again if you’re not familiar with tags don’t worry about the details here but I want to highlight is you can actually go into this court at services dot yml file search for render dot main content render and you’ll see a class that takes care of the render array for all of your formats so there’s one here for HTML there’s another one for Ajax and there’s like two or three other ones for other formats so now I know that this class here is the HTML render this is actually the guy when we’re rendering the whole page just takes care of the render ray and if you open it up this is just obviously a massive summary it’s exactly what we expect it basically somehow generates some HTML and ultimately it creates that response so the point of this is like our job is always to create a response we can do it in the controller I can basically use Drupal 8 likes without using any of the CMS features I would just always return a response from my controller you like done no thank you theming layer I just don’t need you in this case but if you do return an array then it offloads to this layer but ultimately it always returns that same like it always returns a symphony response object back to you well by the end of it oh and I want to highlight again to go into the interface thing I think you guys are going to dig interfaces the way they’re used if you wanted to have your own main content render for some other formats I think with the serializer and rest modules there are other ways to do this but you see this implement main content render interface so you can create your own class implement this interface and then just give it that same tag in the service container and then maybe you make your formats I don’t even know like well the simple example you make your format JSON and then basically if the client is requesting JSON your class is going to be called when out when you return to render array instead of this guy being called so you can plug into the system and that’s a big theme of Drupal 8 it’s almost not even on purpose just the way that Drupal 8 is built like everything can be plugged into you can even unplug stuff so that’s you’re probably doing stuff you shouldn’t but technically you can even replace this class entirely with your own like No thank you I’m going to do it myself all right so the one thing about here now our content render so if you don’t return a response from your controller which you are totally welcome to always return response / controller there’s one last thing here called the content render system which is actually going to turn your render array into the response so this well let me say it this way drupal 8 like drupal 7 there are a lot of layers and even though okay I realize I’m talking beyond what i can say here but me going into drupal 8 there are a massive amount of tools that i can use to actually dive in yes but how does that work probably more than drupal 7 i’m guessing just because the tools that I’ve shown you are like are so powerful

so let me give you like one last quick example here of ryan being like yeah but how does that work because i don’t that doesn’t make sense to me so our route every page is a route in a controller this route does not have a controller dun dun dun so it shouldn’t work but it does work instead it has underscore form and you should kind of recognize this even if you haven’t done your play stuff yet this is going to run to that form instead of actually you know it’s going to run our form so but how does that work is you need to have an underscore controller always because it needs to be some function that builds your page well like all magic inside of symphony or Drupal turns out it’s a listener and you can actually see this if you go into your timeline or your events tab of your profiler you can actually see there’s a listener called content controller subscriber which is one of these simplest listeners you’ll see it’s called early on inside of the request response flow and says hey if request attributes because remember everything in the defaults array of a route becomes a request attribute request attributes has underscore form then just set underscore controller to be this controller right there and that’s it so it’s like there’s a little bit of magic they’re not really in fact there’s a core issue on this they were discussing it’s a little weird to have underscore controller most places but then underscore form without an underscore controller other places you know they’re like maybe we should just make it consistent and always have underscore controller well it turns out if you do that in this case you just have to have more on your route because then you have to have this underscore controller key and you have to have an underscore form key so this is kind of almost like a little usability developer experience thing we’re like we’ll just make them have underscore form but in reality we all know that there’s still an underscore controller going on behind the scenes and if you follow this through it’s not quite this simple but if you follow this through and actually opened up that controller function you would see it using the underscore form argument because that it really is the purpose the underscore form argument still is important it’s going to use that inside of this controller to figure out what form you want to render so man you guys like I that for me Drupal 8 is fun because I have all these massively amazing tools in fact there are a lot of these tools are based off of things are in the symphony world but in some ways they’re actually already better than the things that we have in the symphony world so thing number one try out this Drupal console so this is Drupal console or go have a link on the end of the second second thing profiler please try out the profiler I cannot believe how much amazing work has been put into this thing on there like the things I want you guys to look at are the events tab because that’s going to show you all of the magic things that go on behind the scenes and there are more than the events that i showed you so down in this list is that going to show you all of the events even if an event wasn’t called because some events don’t always happen the ones I showed you happen on every request response but sometimes events only happens sometimes at the bottom of this list it tells you which events weren’t called in which listeners are attached to those and one really important family of events are the router events so Drupal loads your routes only every time it needs to actually rebuild the cash so on a request response cycle all of the routing stuff the routing loading stuff doesn’t happen so you’ll see those down at the bottom is that not called listeners but if you are kind of hooking into figuring out how the routing process or how routes are loaded those are massively important and in fact there are hooks I think it’s called like something like route alter kind of like probably hook menu alter that you can hook into it actually modified the routes when they’re coming into the system but again that only happens when you actually need to do it once when you’re building the cash so the events tab the timeline tab massively massively important to understand things the services tab this is actually the one I’m jealous about because you can search it and there’s the tag search box over there if you want to search by tags and then this is probably the one that go mostly unnoticed by most Drupal developers so you guys can kind of pass it along the request tab and the request attributes because even if you don’t fully understand the parameters and up casting and that kind of stuff if you make a page and then go here these are the things that you can have as arguments to your controller even if you’re not sure how your node ID became a node object you can go here and be like yes I can use these things and the last thing is the core dot services that yml as you get more especially when you get more to the service dependency injection container kind of stuff I didn’t talk about it too much here this is massively massively important and then to go beyond this every time you open a module you’ll see a module name that services dot yml and you can see what extra stuff it’s actually adding inside of there so this is what we ended up with here but I’m gonna scoot a few things out of there I want us to remember that ultimately this is really what’s important we have a routing

controller framework and there are hook points in between there but that’s all it is we’re gonna have a route it’s going to call controller that controller is going to return a response or in the case of the view the render array we return an array and there’s one little extra layer that returns at response but it’s just this we just have containers kind of a container of services kind of floating around so when you guys think about Drupal 8 and how things are working on keep this in mind look at the timeline with this in mind because you’ll see those events happening there you can kind of start opening up the listeners and figuring out what the heck is going on behind the scenes all right so thank you guys very much and also if you get a chance tweet it those guys thank those guys and there are those links down on the bottom for that so thank you guys very much all right who has questions right here yeah good question yeah he said the count curly brace thing can you kind of force that to be an integer yeah it’s a there’s a key and most righteous II called requirements and Drupal uses the requirements key usually for permissions so you see like an underscore permissions key under there but the real purpose of the requirements in the routes is actually for that so under requirements you’d have maybe you still have permissions and under that you have ID and then what you pass it is a the inside of a regular expression so in that case / d plus a digit of any length very good question and kind of corollary to that because this is when it becomes important route routes match from top to bottom so if you have two routes that have the same pattern first one will win so usually you don’t necessarily care about forcing something to be a digit for example if you didn’t force something to be a digit and somebody passes a string kind of worst-case scenario that means you just going to be query in the database for an ID that matches a string it’s not going to be found and they’ll 404 so might not really matter that much to do the ID you know making sure that node is an idea that counts an ID or sorry it is an integer but if you have two routes that would match otherwise like for example we have like slash slash blog / curly brace ID and then you have / blog / and are actually / blog slashes slug which would be like a string like slash blog blog / hello world but then you also have / blog / curly brace page so it’s like hey if you go to slash blog / hello world that’s your viewing one if you go to slash blog / to that’s page 2 of the blog list in that case you’d actually need to do well the studio ways to do it but doing a requirement so would make sure that they won only matches IDs no matches everything else so after our long answer to your question yep question yeah in Drupal 7 and before routes were sort of uncomfortably mashed in with menus so now that it seems to be decoupled where where are the menus to find I should do not know the answer to that so the question was the routes in Drupal 7 word uncomfortably tight to the menus you know hook menu that kind of and they’re not now so like where is the menu kind of stuff yep cool separate yamo file called menu link cialis I’m guessing like module name menu links yml awesome and then I’m guessing cuz I’ve actually seen that now that you mentioned it you’re I believe you’re referencing the route names yeah so when you make a menu you’re like it’s that route name that machine name that i gave the route that’s how you reference it yep not question yeah so using the the routing to define where the controller is to build something new that that kind of makes sense but if I want to jack into an existing like a form alter or a node pre saber or something is that is that still using the old hook system like you know in a dot module file or do we still use a controller and tie into the new event system yeah most and people can Craig from abrama most my precious most hooks are still hooks and so that hasn’t changed much like the maybe the file name that you put that hook into like change slightly but basically like those old hooks are still new hooks they’re not at least mostly aren’t converted over the new system do you have a tentative that work you’re talking about the new events yeah yeah so the one difference because the question is kind of like you have to the new events do you have to kind of like hook them into the correct listener so that big difference between hooks and events is that hooks were done by the function name so the core the core when drupal boots doesn’t know what hooks are listening to anything it’s just once a hook actually happens it just iterates over all the modules calling function exists function exists function exists so the difference between that and events events you make the function and

the function can be called anything but there’s an extra step to tell the core of Drupal this function listens to this event so call my function foo bar when Colonel dot request event that’s the name of the event happens so you plug everything into the center need into the core the advantage of that I mean one advantage of that is that it means you’re your core like the core of Drupal it knows at all times basically when it starts to boot it knows all listeners to all events which gives you the ability to have the nice like events of web profile or tab because it’s just like hey everyone had to pre-register all their listeners with me so I can very easily just tell you exactly who’s listening to what event glad events have like a global scope or is there some kind of bubbling like you haven’t JavaScript and a way to prevent them from going up or catching them like that um ask that again I talked about I tell my something in the theming layer or talking about just like you said first is that kind of global scope well so I guess there’s like deeming events and then there’s controller events so I saw yesterday in the talk that yeah the theming ones do have some kind of bubbling going on like the ones from the field will go up to the note level oh yeah I believe that’s unique to the theming layer I’ve seen some of the bubbling stuff actually in the when I was looking at that HTML render that classic showed at the end that does the HTML page for it you can see the bubbling kind of stuff happening in there on the controller level they’re kind of like just free-for-all like you have Oaks yeah yeah yeah it’s on the controller level events there’s no like bubbling or this events related that event or has a parent event it’s just some probably this might be wrong but probably more like traditional non theme hooks for Drupal 7 just like this module hooks into it and that module hooks into it they don’t really know or care that they’re either of them are doing it’s just kind of a flat field of events there is a priority for events usually you don’t care about that usually you’re just like hey I want to listen to Colonel out requests because I need to do something really early on occasionally you’re like I need to happen before this other event or beef after this other event because maybe that event does something so that happens sometimes you’ll see that in Drupal score if I remember correctly the one of the first listeners that’s called on Colonel bat request is the authentication listener and they set that in core with a really high priority in case you register something with Colonel that request you’ll have access to the authenticated user because they made sure that they had a really high priority if you want to I think you can even make an even higher priority but you’d have to go out of your way to do that so that you know by default you’ll kind of end up where you want to yep oh boy yes asking a really good question asked about yellow files if you know coming from Drupal 7 a Drupal 8 the yamo file stuff is just like you said just like lots of colons and spaces so I may not be the best person asks i’m using the amal I feel like since before I was potty trained at this point just been using it for so long but yeah Mel is come from if you don’t know from an official yeah milspec just like a JSON spec so there’s official y Amal spec you do get really nice errors with the Amal parser so we will tell you when things go wrong so I don’t know if like a specific resource to go to the the thing I will tell you though is obviously so like I’ll give you three tips to remember with it one is it is space oriented which you probably saw and with that keep in mind that when you parse the yamo file it always turns into a big array so just keep them back your mind like this is an array structure in reality so when I go out to spaces that’s going to be inside of the array that you know I’m indented myself from so it is space oriented and you can actually use two or four spaces as long as you’re consistent basically so it’s these spaces and it’s like oh you’re now inside of that array the second thing is don’t use tabs most editors will correct that for you because they’ll say see that you have a gamma file and you’ll hit tab and that will put spaces for you if you do use tabs you’ll get a very clear exception though because symphonies llamo parts are freaks out and tells you have tabs at a certain thing let’s thing or do they number three is at the end of every line you guys have a colon it’s a key value pair would like you know key colon and then the value so that’s basically how those things work the gotcha on there which is kind of an odd gotcha is it always has to be key colon and then at least one space and then the value it sort of like the number one gotcha that would give you a weird error you can even out yet excited you have key colon space space space space space and place value but it has to have key call on a space and then a value oh the like why would you have one key versus another key very good question so where do the keys come from so as far as the Yama format yeah Mille doesn’t care

what keys you have you know you could just put anything you want so it depends on who’s parsing those keys so in this case of the routing file the thing that loads the routes is defining like what keys that you have so whoever wrote that is saying mmm we should have a path key now the good news is most of the time and if this isn’t the case somewhere this is always a good thing to kind of like make an issue to core that we could do a better job most of the time those are validated most of time the class that’s loading that yamo file will see that you have a path key a default key requirements key and then it will see you have something else that it doesn’t recognize and it should throw an exception to be like you said you know OOP shins expected these keys in some cases in symfony core because those classes are oftentimes share between drupal and symphony so it means we’ve had like years of getting them con tuned for those good except some messages sometimes I’ll even go as far as saying you said OOP shins did you mean options because how many people have used the levenshtein functions I’m saying that correctly Levenstein functions there’s a crazy yeah there’s a hand in the back yeah there’s a crazy function in PHP it’s from some algorithm where you can actually give a function to strings and it calculates the distance between those and so you can actually inside of those classes if you detect that you have something that’s invalid like if you’re a core maintainer you can actually then iterate over all the valid keys and find out if any of them are very close to the one that the user typed and then say did you mean options because you typed options with 2 o’s and levinstein says those are awfully close yep yeah so is there a limit to the number of kind of arguments or curly brace wild cards you can put in the route and therefore arguments inside of your controller there’s no limit on those and put as many as you want a couple important pieces of information and let me know if this satisfies kind of how this works with Drupal 7 is that order doesn’t matter so if you have like / curly brace count / curly brace ID you can have dollar sign count dollar sign ID or dollar sign ID dollar sign count so it’s all matching by name so you can have whatever or do you want the curly brace things are required by default so if you have curly brace count like / hello / curly brace count as soon as you put that curly risk count down there you can’t go to / hello anymore you just have one that’s that / hello if that’s the path you can go to / shallow as soon as you have that wild card it’s required and you have to have a value for it unless you add something under your default ski so remember in default so use they have underscore controller you can also then say count colon 5 as soon as you give that curly brace default value then it’s no longer required you can go to / hello or you can go to / hello / curly brace count now if you have multiple ones like / curly brace count / curly brace ID if you make count optional it still has to be there because see you in because it’s kind of in the middle there so you’d still have that case have to say /a low / 5 / something so you can kind of play with those with the what’s that oh yeah very good unless you made also the ID optional then you could have neither of them and if you even need more flexibility there’s nothing stopping you from creating a second controller with a totally different pattern that passes to the same exact controller you know because like I need to move arguments around in some kind of weird way that I could never quite get the exact optional non-optional thing so second route different you know manipulation of those orders same controller yeah and then I got to stop the last question here yeah yeah if then over your head yeah you could create two routes one which is hello which is you know / hello and another route which is / hello / curly brace ID and never worry about wild cards and you have two routes ones / hello the other one / hello with a required wild card yep very good all right I’m running late on my time now so if you guys have more questions like by all means come up and we’ll nerd out I’m gonna get out of the way thank you oh oh very good see you get you got my plugged in her group Eliza no my pleasure oh really okay good