 All right good afternoon everyone Thanks for coming to Cthulhu Drupal. We're going to code with some Lovecraft today Appreciate everybody coming out. I know it's lunchtime. So Good decision. So I'd like to start off just real quick introducing ourselves So my name is Ryan Loos. I'm a web developer at Phase 2 I'm relatively new to Drupal 8 and if any of you were at DrupalCon last year Phase 2 did a presentation about their Internship program and I'm one of those former interns And I'm Toby Hagler. I'm director of engineering of our product delivery group at Phase 2 I've been with the company for almost nine years. I've been doing Drupal for maybe more than a dozen And so yeah, so I'm here to kind of impart some some knowledge All right So how many of you have read some of Lovecraft's short stories? All right, how many of you are at least familiar with his mythology? Okay, it's pretty good So in short the Lovecraft mythos is one where the universe is infinite and full of unknowable horrors that find the whole of mankind to be Less than insignificant some of these cosmic entities like Cthulhu have found their way to earth and their eldritch truths have Inspired cults works of art horrible crimes and sanity and even the rise and fall of entire civilizations Now you're probably asking yourself how does this strange mythology from the 20s and 30s have anything to do with Drupal? So a common theme in cosmic horror, which is the genre of horror that Lovecraft inspired Is this trope called blue and orange morality? And so humans tend to see things in black and white or maybe shades of gray Or if you're a little bit more complex There's a sort of like x and y axis of good versus evil on one axis Law versus chaos on another but if you think of like a cat, right? They're cute. They're cuddly. They love you because you feed them And they kind of sometimes like to murder things on Sunday days So their value system can't really be contained and in our sort of like two-dimensional values grid, right? So strange and alien things like elder gods or cats they see things with a completely new axis Right, it's the sort of like z-axis And and as sometimes referred to as blue and orange morality So this this blue and orange spectrum, right? So symphony and Drupal 8 they kind of exist like that You know people that are new to Drupal 8 You know that have done Drupal in the past. It's hard. This is hard for them people that they come to Drupal from the symphony world or from other object-oriented environments. It's easy. So we're not talking about easier heart It's just it's just different right and so we accept this and so in this session We're going to strive to kind of understand some of Drupal 8's other worldly ways So show of hands just real quick. How many of you would consider yourself relatively new to Drupal? Maybe you know less than two years Yeah, okay And how many of you are experienced with previous versions of Drupal, but maybe new to Drupal 8? Good. Yeah, this is right crowd So for those of you that are new hopefully this is going to be you know, hope you feel a little bit better adopting the insanity For for the veterans out there, you know, maybe this is going to help you teach Drupal 8 to people who are new to it So another reason we're drawing the comparison to Lovecraft is that if you work on Drupal's back end Then you might find the cycles of his stories oddly familiar a lot of Lovecraft's Protagonists start their stories by finding something unusual that is on the surface Understandable by humans like a statue or a diary or something or in this case a very straightforward looking UI They then attempt to go a little bit deeper learning that there's this entire complex world under the surface But just looking at this world drives people to insanity as they struggle to comprehend its awesome power a lot of Lovecraft protagonists Spoiler alert end up either insane or dead But some will push through the madness to ward off the forces of darkness or even harness these powers for themselves and As Drupal developers, we've all been through something like this, right? I mean a big reason we are at Drupal con is to improve our skills and understanding so that we can push through exactly this kind Of thing a lot faster So what we want to do here is teach you some of the back-end concepts of Drupal 8 and the relationship between them while couching It all in a metaphor that ties things together Express a sympathy for how insane all this stuff can make you feel and hopefully shows you the real power You have at your fingertips once you've mastered this system Now Toby and I have made a madness module that you can find right here That forms the backbone of the presentation and is the source of a lot of these examples Feel free to download it for some more clarity on what's going on now. There's one more thing We have to establish before we get started so You are the protagonist of one of HP Lovecraft stories a recent graduate of the Miskatonic University You've returned to your hometown of Innsmouth to find that some things is not right Now in Smith has always been a sleepy kind of place where not a lot of good or bad really happens But in the years that but in the years that you've been gone things have changed Folk have been disappearing fighting each other jabbering weird arcane things and you an Upstanding citizen have decided that you're going to investigate and chart this madness that you've observed taking over In your studies at the University you heard rumors about this new tool called Drupal 8 You suspect that you could make use of a module to accomplish your task First you need to get a better understanding of what's going on of the insane utterings of your fellow townsfolk services plugins routes controllers as any sane scientific mind would do you need to begin cataloging their behavior and Make observations as to when their minds started to unravel But our exp but your experience up until now has been with Drupal 7 or 6 Drupal 8 is so alien so otherworldly. Where do we begin? So just to make sure we've got the the level set here just a quick show of hands However, many are you familiar familiar with? Classes in PHP an object-oriented programming Okay, cool. So I won't spend a whole lot of time on these next couple of slides I just kind of wanted to make sure we cover some bases for those that aren't so just for clarification classes Are a custom user-defined data type. So you've got strings integers, you know enums floats Arrays hashes and that sort of thing classes are just another data type However classes are also made up of properties and methods So if you think about how arrays have you know different elements these elements can have different data types including more arrays Classes have properties. These are just variables within the class They can also have methods, which is a special type of function That that is contained solely within the scope of that one class So you can't call a method directly like you would a function without first either calling the the class statically or Instantiating an object and then calling the method from that The the interesting thing though is the classes stack. So this is this is a really key thing that you're going to see in a lot of The design architecture behind Drupal 8 and in symphony classes stack so classes can extend other classes They take on traits from one class make those traits its own and then add more bits of functionality as they go along So what is scarier than a spider a? giant spider Or now what if it has a hundred legs? What if it becomes a shambling mound of hundred legged giant spiders, right? They're all coming at you in the dark. So each one of these extends the previous class So from a small eight-legged spider to a giant one to a hundred legs They just you know you can keep extending this thing out until the fear and insanity kind of overwhelms you And this is what's called class inheritance So in Drupal 8 Yeah, so so objects are essentially just going to be the Variable version of a class so a class is a data type an object is the variable you can you can call things like Node equals node create you're calling a static class in this case Well that node variable then becomes an object and you can access its methods through that arrow operator So you can do things like node save Is that kind of clarify for those that weren't familiar with classes and objects? Okay, so One of the things that's changed in Drupal 8 is the introduction of symphony Symphony is a is a very mature PHP framework has been around for a number of years It's been around probably as long as Drupal if we get the year so forgive me But it's it was it was added to Drupal and they have worked very simpatico But if you want to gain the benefits and powers granted by the Elder God symphony you first must submit to its design patterns So much like the Elder Gods are want to do the very physics of Drupal have been rewritten with a new underlying framework So their their acolytes have hailed this as Getting off the island or proudly found elsewhere, right? You've heard these over the years and yet these non-euclidean forces at work that quickly drive our citizens of vinsmith Mad without deeper understanding of symphony object-oriented programming or service-based architecture Plug-in-based design principles. So so what about these changes? Do we really need to know so symphony aims to sort of wrestle the chaos and forge it into something a little bit more Universal across platforms and frameworks But in doing so it introduces that that blue and orange morality that we talked about a minute ago to a world It's used to black and white or shades of gray So in Drupal 8 there's there's now plugins and there are service classes and other terrors That frighten even the most stalwart developers or towns folks So what what's worse that it's you know new type of code that doesn't even live in a dot module file So, you know how in the name of city are you even going to be able to find this code and execute it? So here's the lesson so Drupal 7 and earlier every single page load would load up all of the active dot module files It stores it in active memory, right? You could have a Huge memory heap that gets called every single time you hit a page Whether or not that code needs to get you know gets executed. There's there's no way for Drupal to know so it just loads everything So if your site has 300 modules think about how much wasted memory was getting allocated just to perform one simple task symphony grants special access to What's called an auto loader to Drupal so that means it can build up a code cache of where everything is now and only load what's relevant For the particular page call whether it's a page or a service endpoint You know it loads up, you know just the module in ease it loads up just to plug in just the services that are required as this can result in an order of magnitude fewer resources being allocated to Drupal however Everything comes with a catch as we've learned in any Lovecraft story to be granted these special powers It requires that we enter into a pact with the Elder God symphony So henceforth we now must follow their design patterns with our code and our modules so Let's begin with namespaces Familiar with namespaces already or new new concept. Yeah, okay So a namespace is basically it's just an abstract container to hold the logical grouping of you need you. Yeah, I can't even say it unique identifiers That we call names. It's very simple and I did an identifier defined in a namespace is associated only with that namespace So just like the methods found in your your class are limited to scope in your to your class namespaces in PHP essentially limit the the scope of your class to your module a Lot of times you you'll see things if you're if you're requiring things with composer everything is prefixed with a namespace It's always composer require Drupal slash panels right Drupal slash features Drupal slash. It's just a namespace Other projects could conceivably have a module with the same name So we just namespace it with with Drupal makes it unique Drup in Drupal namespaces also tell Drupal where to find code So this namespace declares that a Drupal module called madness has at least one plug-in And one of these plug-ins has a plug-in type called block and The class that defines our block is going to be named top madness which lives in a PHP file of the same name So you kind of see the file structure here. You see how namespaces are playing out Even things like you statements, which we'll talk about here shortly So So when you add a new module Drupal scans that source directory that you see here Finds and categorizes all of your class files, and then it parses each one the namespaces within each class today It tells Drupal the Drupal autoloader where it is and how to find it. So it's a way to call back to itself later It's also a way to make everything unique So in your class as you need to reference other classes or objects, you'll use this use statement Which is it's using the namespace. So I want to use the user Class that is defined in Drupal user entity user Following the namespace the cool thing is if you did have another class in your module or in another module called user You could use that one by specifying the namespace So that's that's one design pattern that you'll see you pretty frequently Coming from object-oriented programming general but in symphony and now in Drupal 8 You also notice by the way that the directories and file names in under the source directory Use camel case. That's that's not me being sloppy That's that's actually just a new sort of coding style that you'll see in Drupal However, you should still use lowercase if you're working in a dot module if you're working with hooks So yes, we do slightly mix styles in Drupal 8 Maddening so Another design pattern that you'll see are things like auto discovery of code In annotations and so in this code sample You know only the important things are very easy to read in your code editor, right? Everything's all highlighted the comments are kind of grayed out. However, the syntax highlighter is full of lies It's lying to you So this this completely and utterly looks like a comment block in the middle of a regular code So you see the at signs for IDE self documentation, but otherwise it just seems pretty innocuous, right? But just like the thing on the doorstep. There's something that lies asleep within us all right so When Drupal's auto loader scans your your module directory it sees this and it actually parses that particular block of comments out So You know when Drupal's auto loader scans modules It can parse YAML files all day long and it does this for a lot of things like services Which we'll talk about later. However, because plug-in classes are swappable They're self contained in a single file. You're not going to have a plug-in Spread out over multiple files like that. So it needs to have a sort of a way to be discovered by Drupal And so that's what annotations are there for It's it's kind of like the final entry in a researcher's log That you find in a cave just before he was swept off to some other place. It's a way for Drupal to kind of Follow along with them So each plug-in type whether it's a block form Migration sources data mappers or whatever these things all have different annotation blocks So they have to have an annotation block, but they actually have different fields that are important So you can actually go to api.drupal.org to look up how to instantiate a plug-in It's going to have a list of everything that is required or optional in an annotation Another cool thing real quick. I want to point out is is the use of the app translation Directive That's very cool because if you have stuff that's going to be user facing or even admin facing A lot of your strings can be translatable directly from the annotation so Symphony plug-ins services these words all feel inhuman and strange as they pass through your lips And now you're beginning under now that you're beginning to understand these alien terms It's time to talk to the villagers whose rantings and ravings suddenly sound a little more coherent It occurs to you that speaking to mad entities in a mad tongue might drive you mad yourself You decide to put that out of your mind So speaking of entities nodes users media items these are all entities in Drupal 8 so in in Drupal You know in terms of inheritance for instance a Drupal user is an object of the user class Which extends the entity base class? Drupal also adds the concept of fieldable entities Which grants other entity types the privilege of having fields? So you know in in previous versions of Drupal only nodes had fields You know Drupal 6 and 7 you know only nodes headfields You know so now You know you're able to add a new field to any entity that is a content entity in Drupal It doesn't matter. I will say that if you you can share fields between different entity types of the same entity so Content types within nodes can share fields However those fields can't be used on user objects on taxonomy objects and so on so there's a sort of limit there So in the example module that we use in the madness module We wanted to add a madness level field to the user object to keep track of everyone's level of insanity There's a lot of ways to provide that sort of functionality in a module. We're gonna do it the easy way We're gonna do it the moral way So Very simply a lot of this can be done through the UI initially so you can create a new field you go to Admin configuration account settings manage fields Then you can click on the manage fields tab you add the field to create You know you can click add field to create a new field obviously and and so for the for the madness module we actually created a Numeric list just a scale of one to ten to figure out how mad a particular user is And then the next step of course is you want to be able to export that field as Configuration that you want to add to your module and so you you can start by going to the admin section again configuration config sync This is all just part of court And then click the export tab Then you choose the configuration type that you want in in the first case We choose field in this in this screenshot. We choose field And then you choose the configuration type madness level And you copy and paste that text somewhere in your in your editor But you also want to pay attention to the dependencies because you want to be able to capture all of those others as well So in the case of a field you also want to capture the field storage You may also need a you know if you're using this method that because there are other methods They're easier as you become more familiar with it But in this particular case you do want to maybe edit the configuration slightly and remove you by the and Underscore core items from this from this yaml. However, the configuration that it gets exported is just yaml like everything else It's important to put your configurations in the right place if you do then your Configuration is going to get automatically added to Drupal the moment you Enable the module and then it's going to get ignored from here on out It's kind of nice that means you can then edit the fields that this module enables through the you I like more But you put your config files in the right director structure and then when the the modules enabled You know that configs automatically imported one time and then that's it then you can edit the yaml config by the way So for instance, we decided to remove that pesky field underscore That appears before every user defined field So the our field name is actually just madness underscore level now That's not a requirement. It was just a way to sort of demonstrate that you can manipulate some of the inner workings of the configuration Also, you may want to if you do use this method and you enable that module You want to be able to remove the field manually from your from your config Before you enable the modules and then Drupal might complain that it's a duplicate field this is just one of several ways to Create config in your new module. It's not the only way. It's just a quick and dirty way to do it if you're doing something simple So here we have it We've got a newly added field on the user profile page that is available as soon as the modules enabled So now we have a field on the user object. That's cool, right? We can start we can start cataloging the insanity of users in in Smith We can start comparing users to others It would be nice though if we had a block that could just kind of list the mad user So if we want to do a block We need to learn about plugins Now that you have a better understanding of the dark powers at work You decide that you need some way of tracking the madness in town You try to find some way to represent the madness, but there's just too much data and it's ever shifting The hooks that you came to rely on in the past won't provide the power you need And you're about to give up on this seemingly a possible task But it occurs to you that maybe you could harness the dark ones power for yourself You recall the plug-in functionality from your earlier research and realize it'd be the perfect way to chart this data You're only going to use a small fraction of the power before you. What could possibly go wrong? All right So another important part of the madness module is the madness levels block which we have right up here It uses Drupal's plug-in system Which is a really important back-end concept to show how mad the various people in town is gone And this is just one example of what can be done with plugins So in a broad sense plug-ins are a specialized kind of class that provides a single unit of functionality that solves one problem And is swappable some parts of Drupal core like blocks fields forms Etc have been designed to be pluggable so that you can reach in and change them if necessary Plug-ins always have a plug-in type Which is the central controlling class that extends and and defines how the plug-in is discovered isn't and instantiated These are the most common plug-in types, but you can of course create your own a Really helpful way to think about plug-ins is that they're very similar in functionality to hooks But if the right hook isn't the right hook isn't always available And they there aren't always hooks that will do exactly what you need it to do So in that case write up a plug-in to get that thing done So for instance a while ago I ran into a situation where I needed to filter the results of a view based on data that I was getting from a contrib module and There was no default Drupal filter that would do that for me So I had to write up my own filter plug-in to get the job done So earlier we talked about the various requirements for plug-ins to work and here are some examples of them in action First we've got the namespace Which just shows both you and Drupal the exact path for this plug-in Which is just Drupal slash madness slash plug-in slash block And we've got the use statement which tells Drupal which class you want to use by calling on that class's namespace And this class by the way has a bunch of other use statements. We just have this one as the example And then there's the annotation which again is is code. It's not a comment. I have trust me I have made that mistake myself This annotation just registers it with symphony as the top madness block and that it can that it can be found in the madness category Finally we have the declaration at the bottom Which declares that it's going to be called the top madness plug-in and it's going to extend the block base class And it's going to implement the block plug-in interface As for the content of the class itself It starts by setting two protected variables one for the number of users to show and one to decide whether or not to link Those users or to link that that to the page of the individual users This is basically just setting defaults that we're going to use later on in the logic of the actual code It then implements three methods and you can look at the module yourself to see the actual logic that's going on between them But a rough summary is we've got the build method which is required for any use of the block plug-in Then you've got the block form method which alters the default form and Then you have the block submit method which allows you to run block specific submit handlers Also, if you were to go into the block plug-in interface You would see all three of these methods and a bunch of other ones that you could potentially pull in if you needed them You're starting to get it now You see the old ones influence all over town you can feel the power coursing through the plug-in you just created It occurs to you that this power must be emanating from somewhere You find certain routes in town have become unusually well traveled of late and you decide to follow them for yourself So we've just seen how to create Configuration for a specific block, but what if we want to have configuration for our entire module? So let's take a look real quick at how to create a configuration admin page This is also the same method you would use to create almost any other page So long gone are the days of hook menu and callback functions Pads and URLs to content they've been all replaced with symphonies introduction You know the symphony framework of routes and controllers So routes can be thought of as the path to a page, but it's a little bit more abstract than that It's not necessarily just a path to a page. It's not necessarily even One particular piece of content. It's it's it's just sort of an abstract thing of You're going to request something and that's that's the the route is the the sort of Thing it's the switchbox that routes that request and so that that route Oftentimes will give the request to a controller and a controller is just another type of class And so they're just special classes that handle the response to the request They control the content they control the message So looking at how to define a route so you in your module have the module name dot route routing dot yaml So it's important again to reiterate that routes are not paths Now routes do provide ways for users to get to your content and sometimes that's a page sometimes that's the API endpoint The important thing is that you just define a route to content or data and then a controller handles the response It's just a traffic cop basically How that's that's kind of how routes can can link to anything so the first key Of this yaml file of course is the route name itself. So madness dot settings dot form again We're using a very common design pattern of name spacing everything with our module So the route can be any string. It doesn't have to have that dot However, it's a design pattern to have the module name dot and then some internal route thing so It's and it's important to know that route name later Because Drupal no longer uses paths directly, you know used to you know, if you made a link It was always like L the L function and then the the actual string in this case. You're actually always going to use Use the route name Then the path you do define the path as part of the route the path is just the thing that it's just one way For the for the request to come in So it can take tokens just like hookmen you did in previous versions of Drupal And the token, you know, it passes that value From the path on to the controller if necessary defaults is just another Bit of configuration that you can have Just you know, it's where you define the basic information about the response, you know things like title is pretty self-explanatory And anything here is going to get automatically translated which is pretty handy In this particular example, we use underscore form so that we can use a form plug-in because forms in Drupal now are plug-ins We're going to use that as the controller however, if you were not to use a form plug-in you could specify a controller just like in the the second code sample here But when you do that when you actually specify a Controller you actually have to give it the full namespace to the class as well as which method you want to be there to handle the response One controller could have, you know dozens or however many Methods that you want There are some other things that are handy to note Like requirements of the route. This is where you would define permissions things like options You can you can tell it. Hey, this is an admin page Do some of your special Drupal magic to maybe not let people just willy-nilly find the route And it kind of a quick aside since we're dealing with routes and controllers and paths and menu things You can also define menu links directly in your module in this case. It's module dot links dot menu dot yaml And what this does is it establishes a link item To appear somewhere in Drupal so to make things easier we created this for the module as well This automatically just adds a menu link somewhere. So we start with adding the route name which I said, you know You're going to use the route name over and over again now And so this is just one case of where you use the route name instead of a path So the title again is self-explanatory then the parent is the route name of the parent menu link So adding adding this particular optional Element here means that our menu link is going to be a sub menu of the people group of the admin page which the admin page It's just a really big fancy menu So we talked about routes, but routes can hand things off to controllers. So here's a sample of a controller class It does use the namespace of Drupal your module name and controller You'll remember in some of the namespaces. They get a little bit more defined like Drupal module name plug-in plug-in type and then the class name But since controllers don't really extend anything They're not there they don't go that deep So it does use the namespace. It doesn't have any default methods So that's why you have to specify the method you want to use in the route But this is all just kind of default behavior that comes from Symphony that's you know established in the symphony framework Routes and controllers are a very common design pattern and a lot of frameworks After all this investigating and the near madness of its complexity you start to realize what you've discovered This is no mortal power. You're dealing with entities plug-ins routes You cackle with glee as you channel the old ones power into your own creations and yet you sense There's still something missing you keep seeing this word service appear and you don't know what it does You only catch it in passing and each time you do it leaves you shaken and doubtful Maybe man was not meant to reach this far. Maybe you've reached the limits of your comprehension of the old ones power No, you're in far too deep to turn back now So one of the several design patterns the symphony introduces of course is service-oriented architecture So if you can imagine for a moment a warren a tiny little creatures running around doing something for a master entity H1 has their own tasks that they perform and that's all they do and sometimes They actually have to work together to accomplish tasks. And so this is essentially how you can visualize service-oriented architecture Just a very common design pattern that is now a part of Drupal 8 So as we said before a service, it's just a class It's just a fancy name for for a fancy class Specifically, it's a class that's intended to do something So services are often you can think of as verbs in your in your module Unlike a plug-in there may be multiple levels to the service in order to performance task Now another scary concept of services and you can almost go to an entire session about this is the concept of dependency injection Is anybody Familiar with dependency injection and you're feeling okay. Yeah, yeah, yeah hands only go up about this high when we say yes The the dependency injection at school So, you know like the name suggests you're injecting whatever dependencies you need for that service directly into it So in this particular design pattern, it's meant it's meant that your service class doesn't you know It can operate independently without having to go and fetch anything. It doesn't have to know anything You just it's doing a thing. That's it and it has everything it needs in its little bundle of injected dependencies So, you know, so you so this pattern sometimes is referred to as separation of concerns It's not concerned with How something gets logged it just needs to log something so it says hey You've already injected the logger dependency The logger service as a dependency Into my service. I'm just gonna log something with whatever you pass I don't care if it needs to make a database connection a database connection is a service that can be injected into your service And it just you know uses that it doesn't have to go and fetch config settings or anything special So that's how you're able to kind of separate the concerns from one service from another So When when Drupal's auto loader then of course comes along and discovers all of these service classes It puts it in what's called the service container, which Kind of kind of spooking itself so the service container actually is just another Drupal service But it's basically just a fancy index of all of the services that Drupal knows about it It's almost like a just a big array with some metadata You know so it knows about all of the services that Drupal knows about plus whatever dependencies they need so There are also other types of services just like blocks have Plugins have plug-in types, which are things like blocks and forms and field widgets Services also sometimes have types Again, this is not a code thing. It's just a design pattern But events are one type of service that you'll run into so events are You know, they're just a regular service. They get called automatically by Drupal at particular points of code execution They're not something that you call Out into the dark like you would a regular service They can get triggered at certain times So one event might be an incoming HTTP request is received or a response has been generated Or that response has just been delivered so you can have events that do some garbage collection or cool things like that Also when you describe and when you when you when you're working with events you can actually create an event subscriber Which is a thing that just says hey, I'm listening to whatever event is going to get called out I'm waiting for my orders from the my symphony overlords And then and then that gets executed in just a priority order So one of the things about services, of course just like plugins that have annotations services have services YAML file which you know is it's what it's what Drupal uses for the discovery method So when your module you have my module dot services dot YAML That defines all the services that your module needs Each service of course is going to have a service ID So in this case we have madness dot event subscribers one and then madness dot levels Is another service So once you tell Drupal that a service exists You know you give it the namespace of your class So if Drupal knows about that namespace it'll know where that class is and so on You can also tell Drupal a little bit more information about the service Tags tags is something that's that's essentially how you tell it what type of service that is So in this case, we've created an event subscriber so Later on if you go download the module and play with it There's an event so that any time a page is loaded. There's a sanity check that's executed And then if if random users Fail their sanity check their their madness levels and incremented by one automatically So that's that's that's all that all that does is tell Drupal that that particular service classes is an event subscriber And then you also notice That that particular event subscriber takes arguments event dispatcher and logger factory because we want to you know We want to say hey, I'm listening to event also kind of want to record the fact that Somehow somebody might have been driven a little bit more mad So those are examples of dependency injection. We're injecting two services directly into our event service Now once Drupal sees the service YAML file all the services found therein are added to the service container Which you know like like we said is just a special data structure within Drupal that houses all the information for every service So that is sort of like a tome of ancient lore So services may be forgotten over time because they're not used frequently But they are recorded in the annals of the service container They're just kind of waiting for the day. They should be seven fourth and awakened to the daylight So in this particular code example We see the start of the madness level service class. So as you can see, it's just like any other class It doesn't actually extend anything. It's a very simple service And so it's very simple in a lot of ways so it sets a namespace for itself And it also has some protected properties for internal use only Because services can kind of keep secrets to themselves They don't they don't want other services to know what they're up to You'll also notice that this this get users method Is using the entity query method from the static Drupal class? So that's actually one way that you can you can call Another static class the Drupal flash Drupal is just an object. It's floating around in Drupal That's that's you know, it's I'm sorry. It's not actually an object. That's actually just a static class And it's slash Drupal So we don't even have to define a use statement because you don't have to define a use statement to list every class It's it's a good good practice. So don't listen to me But you can say slash Drupal colon colon entity query and you can call that that method directly and statically Also a quick side note I did say that services like to keep secrets sometimes as all good Creatures of the night do So you you actually have the concept of privacy with object-oriented programming So so if it's something that's declared as public it could be called anywhere outside of the class So for instance that entity query is a public method Private means that it can it can be called by the containing class Private properties or methods. They can't be accessed outside of the class And then of course protected means it can be called only from within the current class or any children of that class So if you if you want things that extend your class to not be able to use those properties or other methods You can actually market as private So Let's talk about invoking a service. I showed you how to invoke a static method. So here we can invoke a service So now in the in the madness level block This is this is part of that build method that Ryan showed us a minute ago We can simply request users from the madness level service. We invoke it using the Drupal classes service method This looks up the madness levels In the service container and executes to get users methods for us So, you know, then we can pass things like the user count variable Which was the configuration that we said in our block because we had the we had those other two methods that actually handled form Submission for configuration options for the block And so then we can pass that as part of the creation of the service or the instantiation of this service So that that users variable is just getting the return from from that particular service So, yes, the user count variable is in fact a block level configuration Which you can see being set in the screenshot here So you can choose how many how many users you want to be able to see at one time in that block And then since we're using a service to fetch the users We can easily change this at the block level and the service doesn't care It just knows hey you instantiate me with however many users you want that's how many you get At last not only have you traced the source of the madness in your town You've harnessed its power for your very own and now you wield it with impunity But you're never completely safe You never know when a new module or new functionality might distort your understanding and send you stumbling to the madhouse You need tools spells with which to shape and tame your unholy creations. You need an economic on So here are some of the things that we've found particularly helpful when developing Drupal's back end now Obviously your necronomic on should include basic things like stack overflow and Google and mentors But here are some of the things that we found or that we wanted to focus on and by the way each of these could Probably occupy an entire talk on their own So at the end of this slide or at the end of this presentation we have links to a bunch of tutorials for these So Imagine if you had the power to manipulate time itself pause it move forward at your own pace look backwards through it Surely such a power would drive near mortals insane. Well X debug is that power? Like all debuggers it lets you stop a function at any point so you can look at what the variables are doing at exactly that point Which is especially useful in Drupal when you're dealing with enormous objects like the form object on screen there lets you sift through it and Run methods on properties for those objects that you do so that you don't have to test it by writing it out and Refreshing the page over and over again. You can jump forward in time by skipping over or stepping through various functions and you can even move backwards in time by going to the through the stack trace that's also up on the screen and You've also got your CLIs They allow you to communicate directly with and invoke the great old ones powered Without using the slow visual interface that most mortals rely on Drush is something that is Absolutely essential it saves you a bunch of different a bunch of clicks and allows you to do simple repetitive tasks really easily like clearing the cash pulling up your reports and installing modules and then there's also the Drupal console Which can do a lot of the same things that Drush does but it also lets you generate boilerplate code for modules plugins Entities field widgets any of that stuff and it can import and export config files There's there's a lot of things that Drush or the Drupal console can do So you know that the world of the UI is largely an illusion It takes the data that's too complex for human Comprehension and presents it in the way that's pleasing to the eye But sometimes you need to see that raw data and that's where sequel pro comes in sequel pro is great because it gives you a nice visual interface for your database and if you're doing back-end development You're probably going to have to do this at least once It allows you to double check if your information is actually there check where it's stored and you can even run queries directly in sequel pro sequel pro to See if the queries that you're creating are actually pulling out what you need So you're now fully versed in the ancient powers lying just beyond human perception But to get the most out of those powers sometimes you have to pull them up to the surface and Develle is your spell to merge the worlds of the UI and the back end together The information you gain through this can be kind of overwhelming, but it's super helpful Essentially what Develle does is it takes a series of modules that are for developers and makes the UI a lot clearer And it gives you a bunch of extra information For instance, it has kint in it which takes your basic var dump or print our function and Spits it out in a much more readable user-friendly way You've also got the web profiler which adds a toolbar to the bottom of your screen It gives you helpful information like the route you're currently on or whatever queries are being run or the blocks that are on the page It also allows you to generate and fill in dummy content like nodes and users in case you want to have actual data to work with The final spell will recommend for your Necronomicon is what I like to call the the magic was inside you all along spell In other words Drupal itself is really good for learning about Understanding and implementing Drupal It's got a lot of built-in utility functions to help you do things like parse HTML convert unicode manipulate arrays The core dot services dot yaml file will list all of the all of the services in core that we were talking about earlier the api dot Drupal dot org is a great reference for how all the apis work together all The core classes hooks methods and properties are very well documented And my personal favorite is just using Drupal itself to figure out what you want to do So if you say have a method that you want to invoke and you're having trouble figuring out how to make it work Just control f find it in Drupal core throw your debugger on it and just run through and see what what specifically it's doing And with that you come to the end of your journey Despite the odds you've not only survived your Lovecraftian adventure. You've come out more powerful than before This path you've chosen will push you to madness many more times as you wrestle with Drupal eights immense complexities However, you now know how to face it head-on and that a little madness ain't actually that bad So we've actually got a few minutes left if anybody has any questions If there's anything that we covered that we went through fat too fast for or just Thoughts in general sounds like maybe we covered everything. Well, everyone fully understands it now, right 100% All right Well, if you do if you do wind up with questions later Ryan and I are gonna be in the phase two booth at 205. So yeah, I feel free to come by and check it out We can talk more Also, like Ryan mentioned earlier, we're gonna put these slides up here this afternoon on the Drupal con session page So you'll be able to flip through the PDF of the slides There are some pretty good resources at the end that you'll be able to Link back to and also download the madness module and play with it yourself All right, thanks everyone Yeah, I appreciate it. Thanks for coming out Oh Or That'd be fun Yeah Yeah, yeah I Well So so that's always kind of a grab bag, right? One of the first things that I recommend doing is there's a module called Module upgrade their own and I you know, you know, I don't know Angie Byron What chick she she worked at It is a module that has a bunch of Yeah, we figured it would be a module and do a lot of But things like a lot of your books you can never So It could potentially break Those make your life so much easier Through it then Yeah You can there's a I think it's like command B or something that will just say hey import the class that I need That Yeah So it's just like a regular So say say you're gonna print out a form element, right? And you just want to get a part of it You don't have to say like print just form variable because that gives you everything you can narrow it down to like form and then I don't know title or whichever, you know Further down in the array So I would you honestly I tend to use extra bug more than Because you can just you throw the break point in and then it just I just open on there and it runs super fast and it's it's great and you can narrow it down Yeah, it's super helpful No, no, no, they're still there not all of them, but Like hooks And like I said in the presentation if you find like if you're going through the list of hooks on d.do and they don't Rewrite a plug-in in a way that'll accomplish what you need Yeah, we were just talking about that a bit it's It's it's it's basically it varies from project to project because there's so many different things that it uses that seven doesn't Um, Toby might be better Well, are you looking to go from seven to it at a later version of seven yes, okay, so yeah So with the most recent security updates Um, they were released as patches so you can actually apply just the security update without necessarily having to upgrade the entire CMS So you can you can go apply the patch and there's there's you know if you search for applying patches on Drupal other work It'll actually show you the commands to apply it and so But you can you can apply just the patch now Yeah, you definitely should because While all this sort of like you call it like Drupal getting to You know while that patch is related is able to be patched. You may actually be Vulnerable with other things, right?