So you want to build your own Drupal 7 Module?

If you're like me, then there are times when you just want to change something small as far as functionality goes, but you don't want to download and install a bulky contrib module to do the simple job. Instead you'll decide to write your own small module specifically for the change you are wanting to make. There are plenty of tutorials on the subject, but I feel like most of them aren't very "newbie" friendly. Hopefully I can help you understand exactly what is happening each step of the way.

Step One: Create your module folder

Navigate to your sites/all/modules folder. If you have followed standard practices, all your contributed modules should be in a folder called "contrib". If you haven't done this, it is no big deal, but for future reference when you install a fresh version of Drupal you should always make a contrib folder. If you haven't yet make a directory called "custom". This is where you will store all custom modules. Enter into the custom folder and now make another directory. The name for this directory should be the name of your module. It should be all lower case and use underscores rather than spaces or dashes. In this case I'm going to name mine blog_form_changes. Now enter into this directory.

Step Two: Create your module's .info file

Now what you want to do is create a new file with the same name as your folder with .info at the end. This will be blog_form_changes.info for me. This file essentially communicates with Drupal and gives it details about your module. Once you are editing the file there are two items you have to set, the name and the core version. You can also add a description and a package identifier. Below is an example of my .info file.

name = Blog Form Changes // This is the Human readable name for your module
description = Altering the Blog form to more fit my needs  // This is a description of your module that appears on the module admin page
core = 7.x  // This tells Drupal that this module is for Drupal 7
package = Custom  // This is the category for which the module shows up in the module admin page.  I'm using custom, but you can put anything you like.  Maybe the name of your website.

After you have all of that entered, save the file and move on to the next step.

Step Three: Create your module's .module file

Just as with the .info file, the .module file should be the exact same name as your folder with .module at the end. So mine is blog_form_changes.module. You want to start the code with ''. This is just a Drupal standard practice to leave the tag open. After that you want to add a comment about the hook you're going to use. You should do this comment before every hook you use. For my module I'm just going to change the text on the submit button for the blog post to "Publish", just to give my users that feeling like they are publishing their words to the world. We're going to use the hook "hook_form_FORM_ID_alter()" for this module. Head on over to drupal.org and view their API for all hooks and look at their examples. https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_form_FORM_ID_alter/7

Below is how you should begin your hook.

/**
  * Implements hook_form_FORM_ID_alter().
  *
  * Changes 'submit' button label from 'Save' to 'Publish' for form ID 'blog_node_form'
  */

The top line just references the hook we are using, and the second line is a description of what we plan for this hook to do. Next we create a function. The name of the function is based on the name of your module. So in my case I'll replace 'hook' with 'blog_form_changes' then add '_form_' then add the id of your form (which can be found by inspecting the element) 'blog_node_form' and then 'alter(&$form, $form_state, $form_id)'. Inside our function we'll call $form['actions']['submit']['#value'] = t('Publish'); So your total function will look like this.

function blog_form_changes_form_blog_node_form_alter(&$form, $form_state, $form_id) {
    $form['actions']['submit']['#value'] = t('Publish');
 }

Save this file and you're good to go. Drupal will recognize this module and display it in the modules list. Just enable it from there and attempt to create a blog post and you'll see that the submit button now displays "Publish".

This was a very simple module just to guide you on how to get started. The drupal.org API site has tons of hooks and tons of examples. Get started messing around with different things. Trial by error is one of my favorite ways to learn something and Drupal provides a great framework for this. Spin up some dummy sites and just get to enabling and disabling modules to your heart's desire.

Tags: 
Drupal 7
Custom
Modules