Drupal 7 Module Development Basics

By shane
Wed, 2012-09-26 23:16
Daily Dose of Drupal Episode #16

Share with Others

In this episode you will learn the very basics of Drupal 7 module development. From the info file, Drupal hooks, and the module file, this is a crash course to get you started.

In this episode you will learn:

  • How to create a very simple Drupal 7 module
  • How to create a Drupal 7 module .info file
  • How to create the main .module file
  • The importance of Drupal hooks and how they can be used to interact with the Drupal API
  • How to create a custom block inside your created Drupal 7 module using hook_block_info() and hook_block_view()

This is Shane with another episode of the Daily Dose of Drupal, this is Episode 16 and this is brought to you by codekarate.com. Today we’re going to do something a little bit different and we’re going to look at some Drupal 7 module development basics.

I’m going to go through the very basics of building your own module and in this example all we’re going to do is build a simple module from scratch that outputs a Custom Block right in here somewhere and this Custom Block right now will just be Static Content but you’ll learn going through this video the basics of what is involved in creating a Drupal module and how it can be use to extend the functionality of your Drupal website and we’re doing this is Drupal 7.

I have a Drupal 7 test site up right now with just some basic content and a few Content Types and other modules installed but one of the things that you’re first going to have to do is when creating a Drupal 7 module is you’re going to have to come in to your Module’s directory which I have up here and we’re going to create a new folder; I’m just going to call this myblock because I’m just going to create a simple Block in this module.

So now I have folder called myblock and generally the folder name is the same as whatever module file name you’re going to have, it doesn’t have to be and we’re going to create an empty file called myblock.module and we’re also going to create an empty file called myblock.info. The Info file is basically … you can think of it as a configuration file. It tells Drupal about your module and it gives Drupal the information that needs to know in order to install the module onto the Drupal website.

Everything in the .module file is the code that actually runs that module and if you look at other modules and that may have download there can be ton of other files in here but this is the structure of what you’ll need to create a module and we will go ahead and open this up now; we just have this empty files here, go ahead and I’ll open this up and we’ll start with the Info file.

The first thing you’ll need is a name; we’re just going to call it My Block module, you can then add description if you want; the Core is the version of Drupal you’re using; in this case it’s a Drupal 7 site. You can specify a minimum PHP version that’s required for your module; in this case I’ll put 5.2.4, you can specify a version of your module; so for instance you may start at 7.x-1.0 and the next time you make a change you can change at to 7.x-1.1 and go up increment a leap from there, you can also add dependencies of other modules that you may need.
So let’s say we also needed the Date module; we could add that there just like that or if you needed the ctools module or the Views module you would add the Dependencies just like that. I’ll go ahead and leave that out because it’s not really required for this very simple module. So I’ll save that and now I have an Info file.
For the myblock.module file we’re just going to start out with a simple file header and this is just a comment that I like to do at the top of my files that basically can be use with certain different documentations, systems like doxygen or other things and it can actually load in some information about your module if you use a system like that but you can also add a description up here or just leave it out if you don’t need it.

Now once we got this far; the next step is learning how to interact with the Drupal API and Drupal is set up to be extremely flexible; it’s set up with something called Hooks which are basically functions that you can implement in your module to interact with the Drupal website.

So this could be interacting by adding a page on your site using a hook called hook_menu or it could be running something periodically on your site, maybe it’s a process that has to run every so often, you could use something called hook_cron for that but one of the things that’s going to be invaluable along with just the standard Drupal documentation which you can find on drupal.org is the api.drupal.org website and I pulled up two hooks here that’s going to let us create this Custom Block.

The first one is hook_block_info, you can read about it here but it allows you to define blocks within your module and that is essentially what we need to do is define a block within our new module. So you can see down here there’s always some example code; when you first start you maybe want to just grab this code, copy it and paste it in and the first thing we’re going to do with this hook_block_info function is change the word hook to myblock because that is the name of your module; it’s myblock.module so you’ll change Hook to My Block.

In this example; in this myblock.info function there’s two block examples that are defined; the first on is called Syndicate; tells you the information about it and then it tells you that it’s going to use the cache called Drupal_No_Cache which in this case isn’t going to cache the block and this one there is no cache so Drupal cache per role is assumed which means that the caching is based on the user’s role on the site. We’ll go ahead and not worry about caching right now; this is just a really simple example so let’s get rid of this and we’re going to call this My Block and for the Info we will do My Custom Block, we’ll save this and now we should have everything we need to at least install the Drupal module and get started.

So we’ll come over to Modules page, scroll down into the Other section, you’ll see the Other fieldset and we’ll click on My Block Module which is what we created, here’s the description, here’s the version that we said that we wanted to use and you’ll see that there’s a whole bunch of different field sets for different types of modules and you can define where your module falls using package, defining the package in the Info file.

In this case we didn’t define one so it defaults to falling into the Other Fieldset on the Modules page, we’ll go ahead and save this, and now that it is saved we’ll come over to Structure, click on Blocks. If we scroll all the way down to bottom we’ll be able to see My Custom Block somewhere in there, here it is; we’re going to go ahead and in my case I’m going to add it to my sidebar first and I’ll go ahead and I’ll drop it all the way at the top and I will click Save.

Now if I go to Homepage I still don’t see anything. That’s because we just defined the Block, we haven’t actually told the block what to display yet. So in order to display what should be viewed for that Block you need to define hook_block_view and you can of course read the information here about hook_block_view but what I’m going to do is I’m going to copy this in; so I’m going to take the example code and I will droop it right into our .module file, of course change Hook to the name of our Module so I’ll call it myblock and you’ll see there’s one parameter here and the parameter is Delta and what that’s going to do is this function is going to be called and the Delta value is going to be passed in; so if I have 3 or 4 different custom Drupal blocks one called My Block1, My Block2, My Block3, however I wanted to call that then when My Block 1 needed to be rendered it would pass in with a Delta value of My Block 1.

So in this case you’ll see we do a switch statement on that Delta value. All we care about is My Block, in this case we don’t really care about anything else because our module doesn’t define the others but if you have other blocks you would of course add additional case statements to this Switch Statement.

So if it’s My Block we have a subject of … we’ll call it this is my custom block for this subject and what you’ll see here for the content; this is an array and what this is called is this is a renderable array. Basically; Drupal knows how to render this. What Drupal is going to do is it’s going to call a specific theme function, you can give it a specific title but in this example we don’t really want to go into all of the details of the renderable array so we’ll leave that for another episode.

You can also go ahead just define a straight … string of HTML here. The preferred way is of course to use that renderable array but for this example we’re just going to go through it with just HTML. So if we save that now and we come over to our page, refresh, you’ll see our custom block now shows up.

One thing to keep in mind with this; this is example is just creating basic content within the block. So if that’s all you needed to do you can easily come into structure in the blocks and add a custom block this way but the reason I’m going through it building a custom module is because there maybe times when you need to be able to deploy the block in multiple sites and having in the module that makes it easier or being able to have the block in code or there maybe more things like a form or a view or like something else inside of the block that you need to actually customized.

In this case of course we’re just going through this simple example. One thing I’m going to do is I’m going to add some simple comments here; generally when you’re building a module file you want to tell anyone who’d be reading it what Hook this implements. So if you’re reading it later or someone else is reading it or you need to quickly search through your module you would be able to determine which hook functions you’re implementing.

So that’s generally the standard; it just implements whatever the function name for the hook you’re implementing and it ended with a period. So we’ll save that and I’ll show you now that you can add some HTML in here, now if we save that and refresh you can see it’s now changed, you can see that the HTML is taking effect and we have a custom block that we can now turn on on any site and to drop it into any different section inside of our blocks that we want and everything will work out great. You’ve created your first Drupal module.

So that’s all there is this time, now next time we’ll be going over different topic, hopefully you learn a little bit about building and developing a custom module in Drupal and how you can create a custom block within that module. Until next time, this is Shane with the Daily Dose of Drupal; you can follow me on Twitter @smthomas3, thanks for watching.