Drupal Module Builder Module

By shane
Wed, 2012-11-14 23:35
Daily Dose of Drupal Episode #50

Share with Others

The Drupal Module Builder module makes it easy to build out a basic Drupal module with the boilerplate code already added. Using this module makes it easy to avoid typing all the hook implementations that normally are needed when building a Drupal module.

In this episode you will learn:

  • How to use the Drupal Module Builder Module as a Drush plugin
  • How to run Module Builder drush commands to display .module file code
  • How to run Module Builder drush commands to display .info file code
  • How to run Module Builder drush commands to create an actual Drupal module

Hello there everyone and welcome to another episode of the Daily Dose of Drupal. We have a big one today; we’re on Episode Number 50. Today we’re going to be talking about the Module Builder module but before we get started, if you haven’t signed up for the Code Karate newsletter you should do that now and you should also follow me on Twitter if you haven’t already, I’m Shane, you can follow me @smthomas3 but let’s go ahead and get started; the Module Builder module; this is a tool you can use when you’re trying to either maybe you’re just getting started in building modules or maybe you just want to not have to type up the same comments and the same Hook definitions in every module that you create.

What this does is it allows you build out the Boiler Plate Hook Definitions in your module file and Hook Codes so you don’t have to type all that stuff out every time. So as you can see it says that it builds out that skeleton or scaffolding for the module.

There are two different ways you can use this module; you can download it just as you would in a traditional module and use the Drupal UI; in this case that’s a point and click interface where it will allow you to create that basic module code that will get you started and of course the module is not going to do anything on its own but it’s going to give you examples of that code so you just have to fill in the pieces, it saves you time and just saves you a lot of typing.

The other way and this is the one I’m going to go over today quickly is the Drush Plug-in and this is a little more confusing because you have to know a little bit more about the command line a little bit more about what the module can do. So we’ll go ahead and we’ll go over some of these options; the first thing you’re going to do is you’ll need to install it as a Drush Plug-in which in this case we just need to go to our command lie, go to our home directory and look for the Drush … the .drush folder, I’m just going to download this file here and unpack it and now you can look if I go in there it has those files for the Module Builder module, you can of course download this as you normally would into a Drupal website but in this case we want it to be available through Drush.

I’m going to go ahead and go to a working Drupal website just so I can use the Drush commands and I’m going to start going through this list of commands on the documentation page, you can get to that by going here and clicking on Read Documentation on the Module Builder project page and this is going to go through some of the features on how to install and configure the UI and just how to use some of the Drush commands.

So we’re going to go ahead and get started; you first have to start by downloading the Hook database, this is going to make all those Hooks available for you, it says Hook files have been downloaded.

The next is to run mb-list and one thing to keep in mind is that this module is in the development stage so I’ve notice there’s a few things that don’t seem to work quite right all the time but you got to keep in mind what this is meant for and this is meant for use when you’re developing, it’s just a handy tool to help you build modules more quickly, it’s not something you’re going to use on a live Drupal website that’s in a production.

So if you run that command it gives you a list of all of these Hooks that it has definitions for. This allows you to get that Boiler Plate or structure of that code into a module really easily.

So you can also see that there’s some presents here, I don’t generally use those but you can try those out and see if you have any luck with getting those to work. Next thing we’re going to do is go ahead and run this simple command here; and you’re going to notice that it’s going to ask you some questions.

Some of these aren’t necessarily going to be irrelevant all the time but it goes through all the steps anyway so we don’t want to use any presets, we can give it readable name, a description if we want, give it a help text if we want some dependencies, the package you’ll notice that a lot of those are information that would go into the Info file.

Since we didn’t tell it to build an Info file it just spits out the code for any of the Hooks that we told it to. In this case we did Drush Module Builder which is the MB and we said we want to create My Module … my_module and we want to use Hook Menu and Hook Cron.

So you can see it also of course takes in the help text that we created so it implements Hook Help, implements Hook Menu and implements Hook Cron. It doesn’t actually create the Info File in this case. If you want to just create the Info File we’ll go ahead and we’ll run this command which goes into a little bit more detail, this one is only going to output the Info File.

Keep in mind that this is not actually creating the module, its’ just giving you the code at this point that you can drop into a module. So the first thing on this is going to do a Drush MB for that Module Builder, my_module, we can give it a name so we don’t have to answer the question for entering the name, so - - name and then we’re going to call it My Module.

If you want to enter the dependencies this way you can do - - DEP, in this case we’ll use Views and we only want to build the Info File. So it’s going to ask us presets, it’s going to ask us for Hooks, again; since we’re only building the Info File that’s not going to do anything and you can add a description package for the module and you’ll see it gives you the proposed to My Module Info, it gives you the name, the description, dependencies, package and you can just drop that in and create that .info file for your module.

The next step is we’re actually going to create the entire module because it’s nice getting the code but what about if we actually want to have this Drush command create the module for us, no reason to have to do that manually so I’ll run through this here.

Drush MB for Module Builder, my_module, we want Hook Cron … I’m going to go ahead and leave this out, I’m going to define a few here; I’m going to name it My Module, we’ll say we’ll use views as a dependency in this - - write, that’s actually going to force the module to be created.

We’re not going to use any presets, we’re going to use Cron Menu, permission, mail and any other Hooks you might want to add in there, add a quick description, help text, we’ll give it a package and you can see it does. In this case I’ve already created this so I’m just going to overwrite this, I was testing it earlier but you can see it actually does still spit out the code for you so you can copy and create it yourself but now if we go into the modules and you look and you can see that My Module has been created.

We’ll go ahead and we’ll open these files up and you can see we have our Info file that was created and we have Help, Hook Menu, Hook Permission, Hook Cron, Hook Form Alter and Hook Mail along with sample code that you can then strip out and use. You can see it use my_module to create it using for the Hooks.

So really simple and it just makes things extremely easy, you can also … let’s say you forgot … you went ahead and say you forgot a Hook that you want it to add in, after we create it we can run this command and let’s say we want to add to My Module, we want to add Hook in it, no presets, let’s see if this actually works; and so we’ll take a look; I’m not sure if this is actually going to work or not but we’ll go ahead reopen it and just see … okay in that case it does not look like that one was working.

As I said; I don’t use it too often, I use it just to get the basic starting point of the module. In some cases I just started using it a little more frequently. It’s a nice little Drush Plug-in to have especially if you are familiar which is the basic commands in just running your Module Builder with your default Hooks that you generally use when creating a module, it just saves you a lot of time, it makes things easy for you to get up and running quickly when building modules.

So go ahead and give it a try and see what you think and let me know. Thanks for watching the Daily Dose of Drupal and we’ll be back again next time.