Introduction to the Module Development Series
Hello and welcome back to the Tron website developer.com. I am Peter Orski, the Tron website developer specializing in Drupal. And in this first video tutorial of our 10-part video tutorial series on module development, I want to introduce you to what project we're going to create, which is the flag application module. And I also want to get you started on the .info file and .module file, two of the key files that you're going to need when you're creating any type of module in Drupal.
But before we get started, as always, I'm over at Toronto website developer.com/store. Here you can purchase any of my video tutorial series so that you have them permanently. That purchase goes to help support the creation of additional video tutorials, so everything is greatly appreciated when you make a purchase. Additionally, if maybe you're not interested in owning these but you'd still like to help out, always appreciate a thumbs up, a comment, or even if you subscribe to my YouTube channel. That said, let's get started.
Demonstrating the Flag Application Module
I'm over at Local Host Toronto website developer TWD box7. So this is just my local server here on my desktop. And what I do is I have a nice Drupal 7 site that I've used for my development and you'll see here that I've got this, this little link here which is "remove your application / apply for this node". So what this is is I'm using the flag module and I've created my own flag.
And so if I go in here, you'll see that I've got flag application. And so this is the custom module that we're going to build. What this allows you to do is actually go ahead and say click here and apply for this node and it uses the flag module to add in these flags. I go ahead and click on a whole bunch of apply and I can actually remove my application, but the idea being here that users can come and apply for an application to do anything. So you could use this as a job site, have people click and apply, and then you automatically get their resume.
But now I can go over to structure, I can go into flags, and I can go flag applications, and I can see here that username yor has gone ahead and applied for Peter's donation event as well as test event. And you can see here that the status is pending. But we also get the actual node ID that the person's applied for, we get the user ID of the actual person applying, as well as their username. And then we get the flag ID that they've actually... this is the flag that's in the actual database. And so you can approve and deny these. So I can go ahead and I can approve this and submit. And you'll see I've got some development messages here, but the idea being that rules will then allow us to interact when a flag is approved or denied. So in the module development course that we'll be walking through the next 10 video tutorials, we'll actually look at how we actually integrate with rules so that we allow people to extend what will happen when something's approved or denied. Maybe you get an email to administrator or maybe the node is locked or maybe even that person is allowed to edit the node. All kinds of crazy stuff. That's why we use rules, because we never know what that game is going to be.
Downloading Required Modules
So that said, why don't we go ahead and let's get started. So a couple things that we're going to need for this project. Let's do that first. So I'm going to go over to drupal.org/project/flag. And so you want to grab this module first. I already have it and you'll see that you know, there's this 7.x-2.1, 7.x-3.2. Go ahead and grab the 7.x-3.whatever version happens to be out at the time when you're looking at this. But you want to make sure it's seven and three, major release is three. That's what the two and the three denote. Um so go ahead, we're going to build off of three.
With that said, you want to go over as well and grab the devel module. Devel, short for development. This is going to help us because we're going to be using this for debugging. And so go ahead and grab this module as well.
Setting Up the Module's File Structure
So now with those downloaded, what we are going to do is we're going to head back over and I'm just going to go to my modules page. And you'll see that I was actually using some code, so obviously the code exists. So I'm going to go ahead and I'm just going to delete that and we work from scratch. Actually, I can't do that. What I have to do is uninstall that module because I've gone ahead and uninstalled it. So I'm going to do that.
There we go. Now with that uninstalled, I can go back over here and I can actually delete this. So now where we are is we're brand new, refresh, we know we have a module idea, we want to go ahead and create this. So let's do that.
So first thing that you have to do is you're going to navigate to your site. So here I'm at TWD box 7, this is what my site's actually called. Here's my Drupal installation, and you'll see that I have this sites folder. So you're going to click into that. And now I've got all and I've got default. The all and default... so it's just a brief side step here. The all is referring to all sites that you might have installed off of this code base. So it's possible when you install Drupal to dig Drupal and then install multiple sites that all run off of the same code. So that when you're, you know, upgrading your sites, you're just upgrading the code once and then it applies to all the different sites. So you might have another folder here if you're using a subdomain that would be a specific subdomain name. But I don't have that, so I'm going to put everything in the sites/all folder. It's never wrong to do this. The all folder, if you did have multiple sites, the all folder would be accessible by all of those different sites.
So now I'm going to go into modules and what I'm going to do is I'm actually going to create a folder here. But typically if I were doing this and I were creating some kind of custom module that might be small or, you know, for a specific need, sometimes what I'll do is I'll go ahead and I'll create a new folder and I'll just call that custom. And then I'll put my custom module in there. So for the sake of this video tutorial, let's go ahead and actually do that. And then I'm going to create a new folder in here, and this is going to actually have my code. And so this folder that you're going to create is going to be the module name and it has to match the different files that you're going to create for that module. So if you remember when we started this, I had flag_application. And so that's what I'm going to create. You'll notice that there's no spaces because you can't have that on a server. So everything's underscored and you can go back and you can actually see that with other contributed modules that we have installed like advanced_form, advanced_help and so on. So Drupal knows it will actually look inside this folder, anything in the sites/all/modules folder. So it'll go into custom and it'll find my my code. So don't worry about the fact that we're in this custom folder. But now in here, what I need to do is create two specific files that you're going to need for any module.
Writing the .info File
So here I'm going to create flag_application.info, and it's just a warning there about changing the extension. And I'm going to go ahead and I'm going to create flag_application.module. You'll always need a .module file because that's where your code will reside. It's not the only file that could contain code, but you need a .module file.
So let's take this flag_application.info and let's edit that. Um so I want to go ahead and I want to close that previous code. So I'm going to go in here and go to my info and what I'm going to do is I'm going to paste some code in here, which I don't actually have available right now. So here, I'm going to walk through this code. You'll see the first thing that we've got here is name. This is something that you have to define. So the simple syntax word is just name equals and then whatever the name of your module is. So I could call this anything, it doesn't actually have to match the module name here, but that's the convention, so you want to do that. But just in case you know, you're wondering, this could be anything that you want, but just typically match it up. So this is Flag Application.
Description. This is the description that I'm going to provide for the module. So you'll see on our modules page there will be descriptions of all the modules. They're actually defined here. So what I've gone ahead and just, you know, flag entities and have an administrative review and approve... that should be approve. So that's what the description of this module is. That's what this module does.
The dependencies. So this is actually an array. This is not required if you were doing some other module development or using this as a jumping off point. But what dependencies does is it makes sure that you have the other module installed that you're going to be relying upon. So dependencies here, I'm relying on flag and then what you can actually do is you can define the version of that module that you're dependent upon. So remember I mentioned we're using 3.x, not 2.x. So I've gone ahead and I've defined that here. So I've got 3.x.
And then I'm mentioning core. So core is equal to Drupal 7. So this is a Drupal 7 module. And then lastly, I've gone ahead and I've defined a package and what this means is it's where the module is going to be found in the listings, and this is just under Flags.
Enabling the Module and Next Steps
So I can go ahead and I can save that and then I can go back over to my page and then I'll go ahead and I'll reload this. And I actually don't want to be on uninstall. I'm just going to go back to list the modules. And so here you'll notice that I've got module filter module enabled, so that's why mine might look a little bit different from yours if you don't have that. I'd highly recommend you go grab that. But I can go and I can click into Flags and you'll see here I've got Flag Application and my description, "Flag entities and have administrator review and approve". And it allows me to... it mentions that it's requiring flag and we're enabled so we're good there. And I can go ahead and I can actually check that off and I can go save my configuration. And then if I go back down to Flags, you'll see here that I've got Flag Application checked off. But it doesn't actually do anything because they don't have any module code.
So in the next video tutorial, we'll take a look at the .module file and get that started. Uh, just before we do that, one thing I want to show you here, if I went ahead and I went 2.x, I go ahead and save this, come back over to this page, reload, you'll see I got incompatible with version 3.x. So that's why that's pretty cool, especially if you're using different versions of different modules.
So again, hopefully this video tutorial helped you, gave you a brief introduction into the info file, also get you set up on how we're going to create this project in this video tutorial series. Again, if this helped you, please leave me a thumbs up. If you have any questions, use a comment. I always appreciate if you use those comments rather than direct messaging me. It really helps me to manage the influx of questions that I get, but also there are going to be other people that have the exact same question as you. So if you use comments, people can read through that and helps alleviate my workload. That said, hopefully we'll see you for the next video tutorial. Until then, thanks very much for watching.