How to write your own module in Drupal 7

So this is my first post I figured a good place to start would be how to create a simple Drupal 7 module. I know there are many posts out there already about this but as many of my post will probably reference creating a simple module I figured it would be a good place to start that I can reference later.

So to create your first Drupal 7 module you will need to create two files. You’ll want to name your folder and files with your module name.

mymodule.info 
mymodule.module

Your .info file tells drupal everything it needs to know about your module. You’ll want to include your modules name, a description, and the version of drupal it is compatible with. It should look something like this.

name = My Module
description = My Module does awesome stuff!
core = 7.x

This is the minimum you will need to include in your .info file. There are other things like dependences or the package to group it with. You can find a full list here.

Next is your .module file which is just a php file that contains all of your code. In our module we are going to do two things. We are going to create a page with a form that saves two variables to the database and a block that will display the data on our site.

Drupal's module system is based on what it calls "hooks". A hook is simply a PHP function. Take for example hook_permission, if we wanted to use hook_permission in our module we would replace hook with the name of our module like so mymodule_permission. We'll use hook_permission() in our module now to create a new permission so we can control who has access to our page.

function mymodule_permission()
{
 return array(
   'edit my settings' => array(
     'title' => t('Edit My Settings'), 
     'description' => t('Edit My Custom Settings Page'),
  ),
 );
}

Next we will use hook_menu() to add our page to the menu system.

function mymodule_menu() 
{
 $result = array();
 $result['manage/mysettings'] = array(
  'title' => 'My Settings', // Title of our page
  'description'=> 'My Custom Settings', // Description of our page
  'page callback' => 'drupal_get_form',
  'page arguments' => array('mymodule_form'), // function that will returns the results to our page
  'access arguments' => array('edit my settings'), // permission to access this page
  'type' => MENU_NORMAL_ITEM, // type of menu item
 );
 
 return $result;
}

In our menu item we set our page callback to call drupal_get_form and pass it the name of our function creating the form, which in our case is mymodule_form. Now we create our form using Drupal's Form API.

function mymodule_form() {
 
$form = array();
 $form['mymodule_my_name'] = array(
  '#type' => 'textfield',
  '#title' => t('My Name'),
  '#default_value' => variable_get('mymodule_my_name', ''),
  '#required' => TRUE,
 );
 $form['mymodule_about_me'] = array(
  '#type' => 'textarea',
  '#title' => t('About Me'),
  '#default_value' => variable_get('mymodule_about_me', ''),
  '#required' => TRUE,
 );
 
 $form['submit'] = array(
  '#type' => 'submit',
  '#value' => 'Submit',
 );
 
 $form['#submit'][] = 'system_settings_form_submit';
 return $form;
}

Now we want to create our block and retrieve our variables we saved in the database with our form. We’ll use hook_block_info() to tell Drupal about our new block.

function mymodule_block_info() {
 $blocks['mymodule_about_me'] = array(
  'info' => t('About Me'),
 );
 return $blocks;
}

Now we’ll use hook_block_view() to define the content for our block.

function mymodule_block_view($delta='') {
 $block = array();
 switch ($delta) {
  case 'mymodule_about_me':
   $block['subject'] = variable_get('mymodule_my_title', ''); // set the subject of the block to our mymodule_my_title variable
   $block['content'] = variable_get('mymodule_about_me', ''); // set the content of the block to our mymodule_about_me variable
   break;
 }
 return $block;
}

Now we can go and enable our module then browse to http://www.example.com/manage/mysettings to access our form.

Justin Phelan

Hi, I'm Justin Phelan. I'm a web developer at Blackwood Media Group and I've been working with Drupal since 2006. My goal here is simply to share some of the tips and tricks I've learned along the way. I'm by no means an expert but I hope I can help a few folks out. Feel free to contact me with questions or suggestions.

Drupal Association Member