Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
Lessons:13Length:1.4 hours
92006 thumbnails 062617 drupal 2
  • Overview
  • Transcript

2.1 Create a Custom Module

Let's look at the most basic building blocks of modules in Drupal 8. The first thing you need to do is to create a YAML file to describe the module. In this lesson, I'll show you how to set up this file.

2.1 Create a Custom Module

So here we are, looking at my directory structure within my applications directory here. And as you can see, I have MAMP installed, just like I did for the theming course for Drupal 8. But this time, we're talking about modules. But before we get that far, let's take a little bit of a look at the directory structure. What my local installation looks like, and then we'll go ahead and start to dive into creating modules. So as you can see here under my htdocs directory, I have a Drupal 8 directory. Now it doesn't have to be Drupal 8, I just chose to call it Drupal 8, just to kinda separate it from maybe any other versions. Lower or higher than I may want to install later on in the future, but Drupal 8 is currently the most recent version. Now within here, you can see there's quite a directory structure that's going to contain a lot of information, a lot of files that come out of the box. You really don't have to understand 90% of them to follow along with this course. All you really need to understand is a couple of the basics that we talked about in the theming course. And then we're gonna build on that and talk about some of those things and kind of go over some of the details and specifics of those things within the module course right now. So, now that I have all of this up and running, I can switch over here to my browser. And as you can see here, I am navigating into my local host port 8888 into the Drupal 8 directory. Which maps into my Drupal 8 directory right in with my htdocs directory on my machine here. And as you can see, I'm up and running and ready to go. Now a couple things that you're going to want to pay attention to is going to be the extend link here. So if I were to right-click here, and I'm gonna open this up in a new link, because I'm gonna spend a lot of time going in and out of here. So here is the extend section where I can start to install new modules, which I'm gonna show you a little bit here in just a few moments. And another thing that I'm going to need to do is I'm going to want to open up a new link for the configuration as well. Now, if you recall, during the theming course when I make a lot of changes In the customization of certain aspects of my themes, or modules in this particular course. Or if I start to change code, I typically am going to have to refresh the cache because Drupal does a lot of caching to improve its performance. But when you start to do custom coding and custom modules and custom themes and making changes, you have to clear that cache, because otherwise you won't see those changes. So if you were to go into configuration and go down to performance and click on this, you're going to see that there is a Clear all caches button here. Now I don't need to use i right now. But I'm going to show you momentarily in the upcoming lessons where that's gonna become quite important for you to know where that's at. So now we have our extension part of our site up, we have our performance part and now we are going to start to dig into the world of creating a module. So what is a module and why should I care? Well, a module is really a way to add custom functionality, custom pages, and things of that nature to our site. So, let's say I wanted to create a special page. Or a special form, or some special functionality on my site, and I want it to have a custom URL path. Maybe with some parameters that I wanna be able to pull out of that path, and do some sort of custom functionality to display something to the end user, things of that nature. If I really wanted to get into that sort of custom logic type functionality within my site, that is when I am going to need to use a module. So let's start out by simply creating a very basic module, and then we'll build on it throughout the rest of this course. Now, as you saw in the introduction to this course, what I'm looking to do is basically build just a very basic calculator module. That's going to allow me to present a form to the end user so that they can input two numbers. You can obviously extend that if you would like. But they're gonna select two numbers, or input two numbers into text boxes, and then select from a drop down or a select box what type of operation they would like to perform. Either add, subtract, multiply, or divide. And then, they are going to be sent off to a special page that's going to display the result of that. It's going to give them what type of operation it was, what the operands were, what the operator was, and also the results. Whether it's a sum, or the difference, or the quotient, or the products of that mathematical operation. So, not a very complex module, but it's gonna teach you a lot of the basic fundamental building blocks of how we're going to create modules and how you can create your own, going into the future. All right, so the first thing that we need to do is create our module, and we need to create that module so that it shows up on this extend page. So right now you see all the built in modules that come with Drupal 8, and we wanna be able to add something in here of our own. So what we're gonna do, cuz we're gonna go over to whatever text editor you want, here I have Atom.io. So now that I'm in my text editor I'm going to start to dig into the modules folder. And if you remember in the previous course, we spent a lot of time in the themes folder. Well, we're gonna stay out of that this time, and we're going to spend the majority of our time here in the modules directory. So the first thing that I wanna do, just like with themes, is I want to come up with a name for my module. And you could call it anything you want. It really doesn't make that much of a difference, something that is easy for you to remember. I'm simply going to create a new folder here, and I'm gonna call it Calculator. Something very simple, not very complex. But in this case, it's gonna make it pretty easy for me to understand what my module is, and what I'm going to call it. So now, I need to create an info.yaml file that's going to specify some information about my module so it can properly show up within the Drupal extend menu. So I can actually install it into my Drupal site. So the way that I do that is by creating a YML file. So let's create a new file. And we're gonna call this the name of my module. So in this case, it's gonna be calculator.info.yml or Y-M-L. So now that I have this, this is basically just a very simple key value pair file. If you've never dealt with YML, it's fairly simplistic. It's based on spaces and key value pairs. And that's really all you need to know, and it's pretty simplistic for creating modules. So let's talk a little bit about some of the things that you can put into your YML file to define your module. So the The first that we're gonna need to do is specify a name. So we can call this anything that we want. In this case, I'm gonna call this My Calculator. So you could call this something very cool and nifty, or whatever you want. But in this case, I'm gonna keep things fairly simplistic. Then next, we're going to specify a type. Now this type is going to define what exactly it is we're defining. So previously, in our last course we talked about creating the type of theme. Well in this case we're gonna create a module. Then we can also give it a description, so let's give this a description. Let's just call this A simple Drupal 8 Calculator. Just like that. And then we can specify a package. Now, a package is kind of another way to organize and group similar pieces of functionality, or in this case actually modules within the extend section of our Drupal site. So you'll see it fall under this category, or this package, within that part of the administrative site. So in this case we're just gonna call it custom, you could call it anything you want. I'm gonna call it custom. And we are going to specify a core version. So, what version of Drupal do you want this to work with? So you could be very specific, you could say 8.1, 8.2. But in my case, I'm just gonna say it's going to be 8.x. So it's going to be functional, and I can install it on any Drupal installation, as long as it's version 8.0 and above. Pretty simple. So let's go ahead and save that. So now that we've saved that, we have a very simple YML file that is describing the existence of our module. So let's flip back over here to our site, and let's go ahead and let's refresh our Extend section here. And if I were to search in here for calculator, you're now gonna see that I have My Calculator, which is the name of my module. And remember, I have package: Custom, and here we see it's underneath CUSTOM. So whatever version, whatever name you put in there, is what you're gonna find here. So let's go ahead and check this box, and as you can see I also have a simple Drupal 8 calculator so that's my description. And let's go ahead and click Install. Now if everything is typed in correctly and everything is working okay, you should see module whatever your name, is in my case My Calculator, has been enabled. But it doesn't actually do anything, and there isn't any functionality in it yet. So in the next lesson I'm gonna show you how to add some very basic functionality to a custom module.

Back to the top