OOP, Design Patterns and Drupal 8

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
and we are live okay thank you everybody I'm glad to see such a full room of people I love being able to deliver this presentation to people this is stuff that I want people to know so thank you thank you thank you for coming let us get started with our presentation this is olp design patterns and Drupal 8 so i'm marc isaacson you can find me online as vegan triathletes I'm a Drupal freelancer and you can find me on drupal.org and you can triathlete you can also find me on github.com if you can try out so you can find my company cited Isaacson web development com I've been doing this since 2010 and I'm still doing it because I love doing web development I love open source I love Drupal and I love to book even more now the Drupal 8 has come out so I'm so so excited what happened with Drupal 8 and the how I do it is it I'm a code monkey I'm a back-end guy told server-side guy okay so that's that's how I do my thing that's how I earn my money and that's the type of stuff that I love to do but enough about me what about you so who is this presentation targeted towards this is geared towards people who are beginner Drupal 8 developers and vibe beginner I mean even if you've never done Drupal at all before that's totally fine I don't make any assumptions about dirt knowledge of Drupal I'm covering the fundamentals in here and so it should be understandable for anybody in the room whatever your skill level is what you should be expecting to get out of this presentation here is that you're going to get a handle at a high level of how you be how you go about doing this is geared towards module development and Drupal so how you do development in Drupal that's what you should be expecting out of this presentation and I'm telling you when do you want to start learning this is as soon as possible and you're going to get a chance to start doing that in the presentation and why you want to be learning this as soon as possible is that with your experience with Drupal 8 with your experience with Drupal or you have no experience with it whatsoever if you're somebody who's been doing drible for a while and you maybe kind of had it up to here and you're like up like either listed Drupal 8 is a good thing for you to learn because when you're learning Drupal 8 you're learning much more than just Drupal isms at this point you're learning modern web development best practices so whether you use Drupal before you have used people before whether you done loved the development for or you haven't learning to fill eight is a really good investment because not only are you making yourself marketable as a Drupal developer at that point you're making yourself more marketable as a web developer in general because you're going to be learning about modern web development best practices at this point you're going to be able to understand how to do things where you can talk to people in a way that it's not just Drupal isms anymore like when I would talk with fellow fellow web developer friends before prior to Drupal 8 and I would mention things like when you talk to them about like hook form alter it's like what are you talking about but now I can talk to my friends about waste dependency injection like oh yeah in service containers and stuff like that so you're able to hold on a conversation with people who are outside that people community you're able to hold on conversations of prospective employers who maybe don't do Drupal because you've actually developed some skills now that are used in other web development so that's why you want to be learning this stuff and how you're going to do it is we are going to look at code here there will be code in this presentation as I mentioned I'm a code monkey I'm a back-end guy so what types of things are we going to be looking at we're going to be going over a pretty quick and high-level look brief introduction to the things that you need to know in order to be able to start doing modules development in 208 what we are not going to be doing is looking at all of the possible edge cases and what if I've got this crazy type of scenario for this enterprise client who's got to do this edge side includes and Lisak okay we're not getting to all that kind of crazy detail this is the stuff that you're pretty much going to need to do on every project that you do when you need to do some module development so having set that up this is where I now give you the opportunity to decide if that sounds good to you if that's what you came here for you're excited to listen to the rest of the presentation and you're willing to play along with my rules and if you're not this would be a fabulous time or only six minutes into this and you decide if you want to see another presentation I promise I won't be too offended if you decide to get up and walk out and go someplace else but if you remain seated you've agreed to the terms and conditions of this presentation and as we get into it you have to understand that if you want you started getting these nitty gritty type of comments and arguments about things I'm going to say ah that wasn't part of the ground rules you have you know you aren't playing by the rules so having done that nobody's gotten up in luck we're going to roll into the first real part of the presentation it's called olp design patterns in Drupal 8 so as far as Oh Opie even with having approximately an hour to talk to you this you're not going to be a master of all things object-oriented programming before zero by the time you leave here I'm going to look specifically at things like what are interfaces what our classes and abstract classes one look why would you use the trait what's dependency injection you know what are services this presentation isn't really so much Drupal as it is going to be object-oriented types of concepts what I'm not going to get into is this is the exact way you're supposed to do Opie all of the really smart people do it exactly like this and if you do it any differently then you're an absolute and you don't get to call yourself a web developer we're not getting into all those flame wars and debates about like this is absolutely the way it must be done so so again understand the expectation that I'm going to show you certain things perhaps somebody else would say well I would do that slightly different that's okay we're focusing more on these general concepts as far as design patterns the types of design patterns that I'm going to talk about have to do with the things that Drupal uses the patterns over and over again in Drupal 8 so like they use yellow files they've got services they've got plugins they use the service container the things that I'm not talking about what I'm saying the design Pantages if you're familiar with a game of four and so while Drupal is using things like battery or adapters or decorators they may be doing some type of stuff I'm not going to be using those terms here the design patterns are the Drupal specific type of design patterns like oh you're going to see this over and over again in the code what I'm going to talk about in Drupal 8 is specific code examples of how we're going to get up running what if you want to write a module here the things that you need to do but we're not going to get into the fears all about users roles and permissions all the site building stuff and here's how you use and if I multiply lingual was done in Drupal 8 this is geared at the code level and so speaking of code examples and digging into the code there is a module that's running this presentation and if I'm my github repository here it good you can triathlete slash presentation it is my intention that ultimately this will be a contributed module to Drupal I wrote a module to be able to do presentations in Drupal 8 and then we've got an actual bit of functioning code that we can look at so we are hypothetical problem cases we've come up with business stuff that I actually had to do to make this module work or this presentation that I'm giving right now and you can find it right there and that github repository as well as you can actually get this presentation in the repository as well go to my github account and look for the Oh LP - EPS e8 and you can Brett with the database there's all the code pull it down you can run this presentation locally on your own machine so then you've got you don't have to worry about taking a ton of notes while you're doing this you got places that you can go through later on to get at all of that material so please go ahead check those things out grab those resources so as far as this module that's running the presentation I needed I every module and everything needs a dot info file that's the way it is right now so naturally I've got one of those I did write services so I've got two services the mo file I've got a controller that display things and so I need a route to be able to define the path for that controller I do have a library channel file because I've got a very small library asset that I needed to maintain in here and I actually ended up meeting a dot module file as well so we're going to get a chance to see all of those things where those things reside is in the root level of that module so there you see the info the libraries the dot module the routing and the services file there's a new structure that you use when you're doing things in triplicate and it ties into the ability to be able to do this Auto loading in lazy loading of assets and so certain things go in certain places fortunately it's very consistent and this is one of those patterns that you're going to learn about it's like oh I want to do this thing this goes in that place you know where to find these various things so all of these files that I mentioned are actually at the root level of your module structure okay so let's talk about services now this is a big change the way the things are being done now and it would age you should be thinking when you're writing modules in Drupal 8 is you're going to be building a lot of services and a service is a collection of useful functionality that can be used across an application and one way you can think of a service is in terms of almost like let me pretend that I'm writing this third-party library that can get plugged into other applications and so I want to put this logic into the service so that it can be useful not just in my application and in other applications as well and so I've made heavy use of services to do this presentation and you've already seen it in action as I go through my slides you see there's a link that would take me to the previous side you see there's a link that takes you to the next slide these are blocks that I place into the regions there and they're configurable blocks to say is this supposed to be the previous block or is this supposed to be the next block but that block makes use of a service to actually get that link back that block this hope give me the next some way of the next slide is give me the link the next slide giving the link for the previous slide on the very first page when you came in that table of contents then table of contents is a controller to create that page but that controller is making use of a service that generates the table of contents for it if you look at the presentation module you look at my logic that's inside of my clock if you look at the logic that's inside of my controller it's very thin it's very teeny tiny you put your business logic inside of services so that everything else can stay really clean and thin and not all the hundreds of thousands of lines of code so that table of contents is generated through a service how do we do services well we have a class we're going to create an object it's an object that has certain methods on it those methods are what are utilized by other things in your system so I've got my presentation manager service which implements my presentation manager service interface we'll get to interfaces in a little bit let's look more at the service itself I've told you functionally that it's providing there's the create table of contents method so when other things because I've also got the ability to place a block that displays the table of contents so they use the same method I haven't placed the block in my particular presentation because I just didn't need it here but you can think in terms of if somebody had a general site and they might have wanted to have a presentation embedded someplace on the site then they might have wanted to have this block that they play someplace that shows oh here's here's what you can get in the presentation so that's one of my methods I've got create this table of contents for me I've got the method for getting the previous slide and the method for getting the next slide tell me for what the link is - - sorry some of the note is that's going to give me my previous and next slides and then finally I've got this method for getting the slide images there's the block that I placed here they've got these thumbnails it has slide image that opens up in a modal so I've got the method that tells me what slide images are associated with the node that I'm looking at right now that block needs to know what slide images are supposed to be placed inside of it so let's look at the interface then because I mentioned how I created the service it's a class that implements an interface and so you might ask well what's an interface and I know that we're four actually means by and doesn't mean what but wherefore art our interface sounds way cooler than saying what is an interface so wherefore art file interface so what is it interface an interface is essentially like a blueprint or a contract it sets up the expectations of what you have available to you when you have an object a class that implements that interface additionally interfaces are really the magic that makes dependency injection work that's not technically true dependency injection doesn't actually depend on the fact that there are interfaces but for to actually be really useful you really need to have interfaces because you tight ends with an interface instead of type ending with a specific class on the object that's being injected and if you type in tit with a class the problem you run into when you're injecting those dependencies you can only inject instantiation of that specific class while you lose a lot of functionality that way because what you don't like the way that class was written and you would rather substitute your own implementation of that logic you have to hack that class but when you type it with interfaces you can inject any object that implements that interface so it makes things much more swappable which makes dependency injection that much more useful okay so how do we define interfaces and what the interfaces look like instead of using the class keyword we use the interface keyword so I've defined my interface it's called the presentation manager service interface and then what do I put inside of that interface because you got a peek at what I put inside of the class the interface is mostly common it's mostly that block and then you get your signature of here's the method that I expected you're going to have so you can see there's not even any curly braces you put no logic at all inside an interface it's basically documentation it's a contract and it's documentation and one of the night we thought an interface speaking of documentation is that this is something that a non-technical person could pretty easily read and understand oh this is what the application is supposed to be doing this is what's available to me they don't have to go through hundreds or thousands of lines of code and interface is not something so scary to think about opening up and looking at because it's mostly comments just English language that's explaining what's happening so you can see any class is to implement this interface have to provide the create table of contents method it has to provide a get previous slide method it has to provide to get next slide method and have to provide to get slide images method when you implement an interface you must provide implementations of every one of the methods in there adapt the contract now you'll notice what I'm having to do with this presentation I have to keep escaping out of this and then going into the next slide one of the things that would be nice for me to do before I would make this a contributed module would be to have another method actually two more methods you know get next image and get previous image so this would be sort of more like when you use color blocks or those types of tools where you guys are gallery and I can actually just be in here and they go to the next image go to the previous image so whenever I would happen to have the free time to do that I would come into my interface and I would add these two methods that they get next image get previous image and that would be part of the contract and anybody who implements my interface would then have to do those methods as well maybe say somebody looked at my service and said this guy's an idiot he isn't me I'd be out of code I could write this in a much better fashion as long as they're doing my in implementing my interface they're going to also don't have to make sure that they do those two new methods that I would put in there so that's what our interface looks like let's come back to classes I'm using a lot of terms about object oriented programming I'd use the word class I've used this idea of instantiation and I'm saying these types of words here so classes is where that's where you bundle your you can bundle attributes which we call properties you can bundle functionality where you're called methods this question yes yes you so you implements the interface inside of the class the interface never has any logic in it you you put the logic inside of the class that implements that interface the interface doesn't actually call the method the interface think of it in terms of literally like it's a contract that you're signing so you can think of it is though when you are going to write this class that implements that interface you've now signed this contract and said I agreed to do this what PHP will do though is when it's going to compile your class you know turn it I'm not talking to you instantiate an object I'm talking like the PHP interpreter when it's going to compile that file so that eventually something that's executable it will check this since you've said you implement an interface it will refer to the interface and it will let you know if you haven't implemented all the methods if you've changed the type of something like it'll make sure that you are agree to that contract but basically what an interface is you just think of it as sort of like it's a text file and it's a contract that's been written by a developer for any other developers that are going to implement that interface all of the logic goes inside of a tangelo class okay all right so classes where we've got these we've got these properties we've got these functions and then we instantiate a class into an object PHP is a single inheritance language in the way that you inherit from other classes is by extending that class but you can't extend more than one class okay so let's look at what I'm talking about here an example of this you know what do we do with classes how the classes come into play with object-oriented programming I've got a controller that's responsible for creating this image that you see inside of the modal since I'm creating a controller I am extending the controller base controller base is a class that Drupal 8 provides this is including the PHP provider is just everywhere in our opie this is a class that's rupal has provided it's the controller based class when I extend that class that means I've got access to whatever methods and whatever property are inside of that class and I can use them as is they're available to my class this is the notion of inheritance with with an object-oriented programming not just within PHP the difference of PHP is that you can only extend one class which means it's single inheritance so this is how classes work and this is what you do when you do not return to programming the idea of a class is that this is a you think in terms of here's an object and here's like if you have a deck of cards you would expect certain properties about what suit is this card you know what number is this card you would expect the deck of cards to be able to shuffle itself for instance so property that you would have would be like suit and my favorite would you call it like the value like you know a three key or whatever those are the properties that you would have and then a method that you might have what the deck of cards would be shuffle so that you package objects up into classes this is an object-oriented programming is you inherit by extending another class and that's what we got here you do it with the extend statement in PHP so there's classes for you so why would we have abstract classes then so what we've seen right now is we've seen his classes I had a service that I defined as a class which and that didn't actually didn't extend any other class that didn't inherit anything I built it all on its own I've got this controller that I showed you which is a class that extends another class so it inherits things from that and I've shown you the interface that I had defined for my service that I've written so what why would we have abstract classes then why do we need yet something else why not just classes and interfaces why abstract classes well there are some limitations which we have to understand about Fon classrooms before I tell you why we're going to actually use them and I'll tell you what they don't do for you the difference between a class and an abstract class is that you can't actually instantiate an abstract class you need to extend it with a concrete class in that concrete class is what ends up getting instantiated so abstract classes are more limited than classes because abstract classes can't actually be instantiated and they also still don't get the ability to it doesn't provide you multiple inheritance you can still only extend a single abstract class so there's limitations of what you can or can't do with an abstract class we're going to look at an example we're going to stop being so abstract about it in a moment but I didn't actually have to write an abstract class for my presentation module so we don't have one that we can look at within my module what we're going to do is we're going to look at examples from Google core where they're using an abstract class and what we're going to see is this an abstract class is sort of a middle ground between an interface and a concrete class so it sort of has some things that are like an interface and it has some things that are also like a class but you don't instantiate an abstract class but you can put code into an abstract class so it's more than an interface but less than an actual full-blown class let's see examples now this is from Drupal core and I don't expect you to memorize everything that's here I'm going to tell you what to focus on in these slides we're going to look at the list items based abstract class this abstract class also happens to extend another class and implement a particular interface you can implement as many interfaces as you want by the way implementing an interface just means that you're going to guarantee that there are certain properties and they're certain methods that are available inside of your class it doesn't matter whether the class that's being expense by this is an abstract class or natural concrete class it extends this class it amongst the interface just focus on the list item face abstract class and let's look at some of the things that are inside of that abstract class these first few methods that we're looking at the false storage settings get possible values get possible options you can see these are methods that have logic defined in them and in fact we are going to inherit these methods or Drupal 8 inherit these methods directly into the classes that extend this abstract class they just take it as is and they use them so there's where an abstract class is more powerful than an interfaces that you can actually put some blocks in it that can be inherited by other classes that extend it take it use it you're good to go you don't have to do anything else so be is not going to be used straight up as is okay other types of methods that you can see inside an abstract class if you may actually see something and this is why you need an abstract class as opposed to just doing things with classes an abstract class allows you to define and abstract method and you can see this looks very much like what you get in an interface this one doesn't have the curly braces around it so what this is saying is it that when you extend this abstract class you must provide a concrete implementation of this method I have no idea what the description you want to associate with the allowed values that you have I can't even begin to put something in here so I'm going to make it an abstract method and those you are responsible for creating that type of equipment in here the first thing we looked at is where the abstract class is kind of like a regular class this is where an abstract face is kind of like an interface and then this whole abstract method is what defines the need for having an abstract class okay you don't have abstract classes in a complicated abstract methods in a concrete class so this is like an interface we're going to see that this must be implemented and then the third type of method that you're going to see in there is that this one actually has some logic in it in which you can do when you inherit from another class is you can completely override it if you want to override a method or you could use that method and then extend it a little bit in this case what we're going to see in Drupal is that this is called taps allowed values the method and you're going to notice that you get the value and it just returns the value so this is really doing much of anything right now it's not being cast as anything and you're going to see what ends up happening in the concrete implementation that extended abstract test is that they actually cast this value of something before they return so let's look at those things those first three methods I can't show you because they're just used straight up you don't even see them in your class that's extending the abstract class so what we will look at is now where we've got this list stream item that's extending our list item base and remember the list item base is our abstract class this time we're actually we're saying we're using this is actually a list stream type of item that's going to extend our generic list item base and now we're going to see those other methods not the first view that I showed you because those are inherited directly and they're used but what we will see is here's our allowed values description which we now have our concrete implementation of that is no longer an abstract method it's an actual method that we've written and there's where we've got our allowed values description and we put logic in there so that's where we've now made our implementation of the abstract method into a concrete method and then the third one that we looked at is where we had the casting catalog value and here's where we've taken it and our where this one is totally over written step function because there's only the one line in it but there are ways that we could have actually taken the stuff that the original function did and then extend to that and we will see an example of that later on in the presentation so those are the three types of things that methods that you're going to see inside of an abstract class and now we see classes interfaces and abstract classes and we are going to to the next idea traits trait is one of those things in the PHP community that has a bit of debate and contention around it this is one of those things where you could get into the flame wars but we're not getting into flame wars in this presentation really are looking at traits because I think you will find in the PHP community there's at least the consensus that they can provide some useful functionality and the useful functionality provide is a workaround to the fact that PHP is single inheritance I found a block that meets the returned a table of contents in this module and I've got a controller that needs to return a table of contents it's basically the same method it's defining a render array but I can't extend the block base and extend the controller base but if I put this logic inside of the controller basement it's not available to the block and if I trying to get the other way and it's not so what do I do about it I can't extend both things this is where traits come in they give us a way to get around the fact of PHP of single inheritance you put this code inside of a trait and then you can use that trait wherever you want to use it it provides you code reuse so you don't have to risk yourself let's have a look at a specific I have these two different places where I can create a table of contents so I created a trait that I called a table of contents traits and you use the trade keyword interface class abstract class traits these are all keywords that you use when you're defining your things in PHP that table of contents trait as a method that's called build this is what's generating a render array for us that's going to have a table of contents in it I can name the trait whatever I want to name it I can name the method whatever I want to name it it's possible when you use a trait you can alias the name of the trait you can also alias any of this message and sometimes this becomes necessary if you look in Drupal core you're going to see where there's certain traits that have been alias because they were they would have had they had another trait that was made the same way so they needed to be able to work or they needed to use to treat more than one time but you can a lease the name of the trait you can alias the name of the method I've named my method build because the block base expects that you're going to provide a build method and my block is using this trait and so now it's got the build method in my controller I can name the method whatever I want that's going to return the display for the page but Bill is as good as a other thing however it for some reason I felt like in my controller I needed to have that method name something else I could alias it might be fuse into something else this is perfectly acceptable and I end up using this as is that's the contents of my trait there's a build method in it it generates a render array that rather arrays what gets returns and that's what's displayed how do I use it very simply you put inside your class use this trait now what you're going to do is in your use statements that are outside of the class those are different from the use statements inside of the class the use statements outside of the class has to deal with namespacing in PHP and so you don't have to do the fully qualified name of the trait usually use statement outside of it then for any of the objects if you can be not just straight then you can refer to it so there is if you look at my code you'll see there's a use statement in this class that says what namespace is trade in but to use it you just inside your Christ you say use that trait and what this is it's pretty much the same thing is whether you do an include or an include once or require or requirements it's pretty much the exact same thing as you do in procedural code it's going to drop that bit of code that was inside of the trade right into that spot you can place it wherever you want to place it in your class it doesn't have to be in the top of the class you could put it at the end of the class that you wanted wherever you think it's most suitable you just use it and what ends up happening when the PHP interpreter is going to compile your file into an executable object it pretty much is copy and paste that code right into place there and now it gets interpreted in the guts compiles to an object that's all that trades are they're pretty simple to use and they're pretty handy to help you with this issue of I really want this in two different places but I can't put it only and put it into a drink so that's how did you use it okay now let's look at some of the things that some of you may have noticed you've seen a lot of dot blocks that I've had we're using a lot of code examples you might have noticed that some of my doc blocks there was these weird kind of comments at the top that has like this app sign in front of it and then there's some other stuff there's indentation there's curly braces it's like this is some of the weirdest documentation I've ever seen like what is going on there those funny comics actually are common they're called annotations and they're actually code they're more like code than they are comments they provide functionality they can make changes to configuration they can provide functionality to your class that you're writing Drupal uses annotations very heavily with this whole plugin system plugins are so tied into this notion of annotations let's look at an example of a particular type of plug-in annotation what type of annotation do you think this is right here it's not a trick question so we said the block annotation I'm going to write a block plugin you use a block annotation the block annotation you use the ID key this provides your machine name for the block the anthon lambo and the category is what you see when you go into the block layout like if you're going to go place a block and then you see that modal that comes up it gets the information about what there's two columns and so one is like what's the name of your blog the human readable name and then once the call what category is this part of that's provided using annotation and that's cool I mean that's fine and we need to have the machine name but what's super cool about this annotation is this key right here this context using that key in my block annotation makes my block a context aware plug-in I don't have to extend any particular class I don't have to implement any particular base but because I said I'm using this node context in my block I'm going to have a method available to me that enables me to get at the node that's being displayed those of you who have built use before know that when you create blocks blocks can't accept arguments and when you need to have a block display something from the node that's on the page what you do with views is you go into the contextual filters you say add an argument it's going to be a false argument we're going to get the Content ID from the URL and now I can get my note types of some when I'm in the block this context key does that same thing for us we now know that this note is being displayed on the page and I can get at that note and how I get at it is there's this method that now becomes available to us that's called get context doesn't that happen because of the annotation that's all the annotation at the top of this is at the top of my class and now I've got this new method called get context value and you'll see the context that I'm getting is note that context has a match this context that you set up above right now there are two contexts that Google provides there's a user context and there's a known contact okay I still haven't actually figured out where and how it defines those context I just know what I can do in the one is there I think theoretically you can also provide your own context but until I understand how a group of provides a user context there that those context I have no clue how to provide my own context but this whole context the word plug in is the super cool thing the triple-eight has and you tap into it by using the annotations so this provides functionality to my block class just because I put some stuff in the annotations up there it gets parsed when your file is being you know read so that it can be compiled and then the stuff happens on the backend as a result of that okay if there's one thing that you're going to take away from sitting to this entire presentation this is the thing you should take away we're coming to it right now if you're tired you've been throwing out wake up listen up you need to know this dependency injection this is a major pattern that's used throughout all of Drupal 8 it's changed the way that Drupal is done let's look at it there's three different ways that we can do dependency injection we can do it through a service we can do it through the container interface which is things like controllers or we can do it through the container factory plugin interface which is things like blocks and to be honest there aren't really three different ways to do it there's really two different ways to do it but three sounds cooler than two and if I was going to be really honest about it it's the same thing there's only one thing that you're doing but there's just a different way that you indicate what happens so we're going to look at these three different ways that you can tell symphony that you want to be able to do dependency injection so let me describe what dependency injection is before we look at how you do it it sounds like a big technical complicated word and when I first heard about it it was scary music oh my gosh how am I ever going to learn dependency injection would you write procedural code you can write functions and your functions can take whether you want to call them arguments or parameters you have something to that function that's dependency injection the only thing that's different about it is instead of passing in some screen or some you know argument you pass in an object what would you do is you pass the object into another object so when you're building your object its constructor method receives these other objects and it can make use of them you utilize dependency injection because instead of writing all of this code inside of your object if you just inject the necessary object into yours then you just use what they have available to you and you just get to assume that they work dependency injection is super helpful not just for the ability to do the swapping in and out of different functionality but also in the ability to now write actually unit tests Drupal 8 was pretty much I mean through probably pretty much impossible to unit test prior to Drupal 8 because we didn't have this ability to dependency injection so we had like all of the dependencies were written like rolled out in the code inside of your modules and so when you wanted to test stuff it became a nightmare to try to be able to break things up with dependency injection what you do if you are injecting an object but the object that you're writing is called the system under test sut you just get to assume that all that stuff that's injected into it works you will need to test your method you may need to test your specific stuff and you can mock those objects which again is why you use interfaces you could where you're going to adjust the thing in there you can have these mop interfaces that you put in this is the power dependency injection separation of concerns its testability it's just make it makes life so much better as a web developer and all it is is that instead of hard-coding things inside of your particular code that you're writing there you say I need this thing when you build my object I want you to put this other things inside of my object because I want to use what they do as well and now you've got it available to you let's look at how you get those other things those other objects inside of your object so if we're dealing with the service we define our services in a service services idml file I've got my presentation management service I defined it in my services the Apple file and you can see I've got these arguments my service needed entity type manager service that's what the X side means you can also inject like very printers into into your service if you want to do I'm injecting the entity type manager object and I needed the entity type manager object because that's what gives me the ability to get into the database and find out the content of the nodes okay but that's another service that's available to me I don't have to write all that logic just bring them in a tag manager and I've got to stuff that I'm able to use inside of my service that's how you tell symphony I want this object injected into my object in what ends up happening when you do that when you look inside of your service inside your class you got your constructor function and so when Symphony is doing make with a new statement on your service it's going to pass the entity type manager into your constructor and now that object is available to your object and then what I do is I take it you just typical you just put it into a property that you've got in in your class and then you use that property later on so the objects been injected and now you've got it available through the property that you place it into and then you just do stuff using that property that's how you get added with services if you want to get at it with something like the controller and it is my controller actually in my controller needed my presentation manager service because for instance you need to get at the table of contents my controller expense the controller base and a controller base already implement the necessary interfaces so all I need to do in a controller to be able to get objects injected into it is use this create method and in that create method that's where you tell Symphony Oh Symphony I need you to give me the presentation manager service and then with Symphony instantiates my object my class into an object into the constructor it's going to pass the presentation manager service now some of you might look at that and say wait a minute I thought you just said that dependency injection you're not hard coding things but this is your class that you're writing and in your class you're saying I want this presentation manager service how is that not hard coding anything first of all against understand you got it well Symphony what needs to be injected you can't just magically know so you need to tell it's something that you need right so you're telling us you need a particular service but notice that what I'm type dancing with is an interface so any object that implements that interface can be injected into my object and Drupal provides a way that you can override say you know what when he says he wants this presentation manager service I related light to service I've written a different service anytime you see that swap my service for his service so it's not it really isn't hard coded you have to provide enough information so Symphony has a clue what to do but you've got the ability to swap out one service for another service and since we're doing the interfaces here everything works out in the end so it really is a game it's totally swappable you just need to tie it together in some fashion and then the third way that we can do this is if we needed to do it in a block so in my block in order to be able to do dependency injection I do actually have to specifically say that I'm implementing the container factory plug-in interface that's how I can now get access because I'm implementing a certain interface I know there's a method that's going to be available to me and then when I use that method that's how I call symphony to inject the object that I need and from there it looks exactly the same is when we did it in the controller that's one say these two really are the same thing is just that with the controller based death controller base already implement the necessary interface so you've got the create method available to you when you're dealing with the Block in specifically say oh I want this block big send the block base and I wanted to implement this container factory plug-in interface and once I implement that interface that's right now I've got this create method available to me which is the same type of thing except when you're doing it with the block unless there's some other stuff that that Symphony also passes in to our create method just because we're dealing with a plug-in they need to know stuff about configuration and all this other stuff which is not really what we care about in my block what we care about my block is that I still need this presentation manager service because that's the block that generates the table of contents well there's well if there's also the blocks that generate with everything else that are using the services here is the example that I was telling you that when you you can actually our block extends the block states our plot base has a constructor method in it okay we are taking advantage of where it says parent that double colons makuta diaphragm is a Hebrew word means like double Cola and that's the scope resolution operator in this our constructor message you can see like pemain manager dimes next we've got to do yes yes this is running our parents constructor first so we have an overridden where it's sending what our parents constructed up so we're calling the parents constructor and then after that's done we do our own thing in our and our constructors while and that's what we make use of the presentation at your service center magenta so there's an example like I was saying extending a method that you've inherited your up to totally like that out you can still make reference with the scope resolution operator okay all right berets now I've talked about controllers and I said that I did need to use routing in my module this is what routing does is one piece of what used to be part of cook menu prior to Drupal 8 there were a lot of parts of the system that did a lot of stuff all in one place and one of the things that was a focus with Drupal 8 is those a separation of concerns don't try to do everything all in one spot put everything in a logical place have everything very very focused on what it does so that you don't have to you can actually understand that piece of assisted like you can focus on that say oh I want to know what this does and I can look just at this hook menu does a whole lot more stuff besides just routing in Drupal 8 they've broken that routing piece out into the routing yellow file and the only thing that the routing yeah mobile is responsible for doing is associating a path with something that gets done at that path and what it looks like is this so I needed to make a couple of paths available I've given a route aim that's the first part is a key that's the top-level key so one of my route is named presentations dot display underscore table of TOC it's associated with the path presentation slash table of contents that was that first page that you saw when we sat down in the classroom here that was my table of contents page that I put there it's handled by the controller that's located inside the namespace this controller's name is display table contents the method that it uses to get a rather array is Bill that's the thing that I put in that trick so there's the controller ties all of these things together you can have forms you've got other things that will be associated as a path the best that's the rotting file right there it's a yellow file it's pretty simple to understand when you see it and if you needed to figure out how to do things you can always look at other routing files and say all right I don't quite need to do that but let me look at other modules and their routing files and let me see how they're doing things so that I can do that same type of the thing okay now is where we get to the things that's sort of not exactly module development and is sort of blurring over into what might be considered more front-end stuff but I did need to use this in this module so this isn't just limited to front-end stuff we're to talk about these libraries because I mentioned I've got a library piano file the reason we got libraries in through both need is for asset management and for performance reasons instead of loading every single thing on every page which is what Drupal 7 and previously did jQuery is loaded on every single thing like everything gets loaded every CSS file every JavaScript like ate all of it sort of and every single page which is not great for performance what you do with libraries is you package different things up into your different libraries and then you tell Drupal when to attach that library so when I'm in this situation I need vinyasa I need this JavaScript or I need these JavaScript settings and so I want this library in this context and that's the only place that I need it that's what we do with libraries is manage our assets and say when we want to get at them we use the library piano file and what we put inside the libraries file is as many different libraries as we want I only needed one library and all I needed for my library was this little bit of JavaScript that I wrote which is what's giving the ability you notice when I had the slides and I click on it and then the next bullet point comes from the next book by Thompson next one so it's more like a PowerPoint presentation this is this tiny bit of JavaScript that's playing games that's all the things I've hidden initially not when you click it displays the next one in the next okay teeny tiny Biti's on script but it's also using the jQuery library and there's other stuff in so even though you might think on second email if your JavaScript rat loaded every page it's not that much well first of all it doesn't apply to every page in my presentation it does because every page that we look at happens to be a slide but then wouldn't necessarily be the case on every site so I make sure that I attach my slide library only when it's heated and so all I'm specifying is the JavaScript that I want but again you could say there's CSS that you want in certain situations and their JavaScript centers that you want in such situations put it into a library attached to the library when you need it and then you've got it when you need it it's not there when you don't need it we're going to look at how I use that in a moment but before we get to that we need to talk about cooks because not everything is object oriented and there still is some procedural code and the reason there's listen procedural code interval 8 is that they haven't converted all of the hooks into this system where you use like event and dispatchers and subscribers and listeners okay that's is the object-oriented way that you do things but not all of truth will be true close hooks has been converted interpolate to this whole event dispatcher type system and your hooks go in your dot module file and I did me to make use of the hook because I needed to tell Drupal a template suggestion that I wanted to use and while with themes you just put your template suggestion into your theme template and Drupal even picks it up with modules that's not the case with when you're writing a module and you want a template the deep hook up picks up you need to use hook theme and say this is the template suggestion that I want and the template suggestion that I wanted was just no slide full template and you're going to see why I needed a template in a moment and you're also going to see how I knew that I wanted to name my template no it's like folds so this is what I needed in my job my job I was this one hook this one second so we talked about template files just now the templating system that's made use interval 8 is quick we're not using PHP template anymore you can use it if you want to still but twig is very similar to our PHP template with the TPL dot PHP files although it's easier to use you still can have your template suggestions just like you could before I'm using the template suggestion there and it's evening easy to figure out what template you want to use because twins provides this debugging ability where they're going to put HTML comments in your page source so you can actually figure out what where something is coming from instead of like prior to people you looked at a page a like how is this happening like what is going on and now you've got to sit around and you try to get into the template you're like oh they've got this template that's doing that now I see why this is happening on the page but it could have taken you not joking hours till you figure that out until you dug into the right place and finally got it things are better with twit here's a template that I use this notes light bulb it's a twig template it's overriding your standard template and the only reason I needed it is that I wanted to use this attached library function when I'm looking at the bull node view of one of my slides put this library around here so that I can click my bulleted list and they'll display one by one that's why I needed a template that's how I sketch the library in the situation you can attach libraries in other ways as well as rupal but it's the exact same concept you'll attach a library in the context that it's needed okay so how did I know what template suggestion to use so I went into my this is my application services yellow file when you define your own service you've got your module name dot services at yamo this is the Drupal instance Y services yellow file this is the place where you can go to as I mentioned earlier if you didn't like my presentation manager service and you wanted to write your own service you go into the services demo file this is a way that you can let you below use this service instead of that service where if you see it I didn't need the services animal file for that so what I use the services man will file for less you was to tell Drupal turn on twig debugging and once you turn on swing buggy that's where you get these comments the HTML comments that tell you what's going on with the template and what they look like is this and there's lots of templates there are so lots of template views in triple-eight so it's very helpful to look at these comments and if you go down the page and say oh I'm in this situation and here's the various templates I could have used mode HTML which is what does the bulb is and then there are these very suggestions and what you can see right now is that X is the one that's being used right now I in this spot this template is being used and I'm going to get this wrapped up here quickly but if you want it to stick around for a minute afterwards I've actually still got that running right now it tells you further I've kind of got that grayed out so you specifically love template it tells you what directory is under like that only set the name of the template but here's where you can go if you want to find that template so that's what the twig debugging provides you that takes us all the way through the presentation I did it again I ran one minute over I am if you want to ask questions I'm happy to entertain questions all without letting it come on so we actually have some time you can catch me at lunch on the table talk to me or if you were paying attention in the beginning you know where you can reach me online go into the git repos that I provided you can file an issue against the presentation module you can file an issue against this presentation itself you can get to me through my contact form I love talking about this stuff I hope you enjoy the presentation I hope gets you excited about using Drupal 8 you want to get any questions now go ahead fire away I'll stick around I'll answer them yes oh okay so I actually can't say her so so we would have a few minutes before the other one would start but otherwise I will say thank you so much for pending hope you enjoyed it hope you love people
Info
Channel: Twin Cities Drupal
Views: 3,276
Rating: 5 out of 5
Keywords: TCDrupal, TCDrupal 2017, Drupalcamp, Drupal
Id: jozzM_6Gyi0
Channel Id: undefined
Length: 61min 30sec (3690 seconds)
Published: Sat Jun 24 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.