In Drupal 7, the concept of a module differs to it is in other CMS. While most solutions are evidently shown to the user, in Drupal they are used to be more focused on expanding tools for programmers or interfacing administrators.

On this CMS, and because of Panels, Views and Caos Tools modules’,  the creation of new functions for the visitors are made without the need of modifying codes.

Sometimes, we find that there is a need of calculations or operations with the stored data on Drupal that cannot be done directly with the interface.

The basics of Drupal 7

The most simple module in Drupal 7 only consists of two files; INFO and MODULE. With the first one, we have to provide information to the CMS such as the name, the description, Drupal ‘s version that’s permitted, module’s version, associated files, dependencies with other modules, etc…

However, before we start to write, we should select “machine’s name” for our module. This name must be easily distinguished from the others, without spaces and without strange characters, this is because we will use it within the code to associate our response functions with it. For this tutorial, we will  use “firstmod”.

Another important detail is…

Where will we keep our module?

The fastest answer resides in /sites/all/modules/ but if we have a multidomain Drupal installation you could also go in the folder modules/ of any of them. In this case, we will use the fastest and the most general answer.

Once these two important points are decided, we will move on to create the directory for our module: /sites/all/modules/firstmod/

Creating and firstmod.module files

Then, we will create both files: and firstmod.module.

Firstly, let’s have a look at the content of

name = firstmod

description = Mi primer modulo para Drupal

core = 7.x

package = tools

php = 5.3

version = 7.x-1.0-dev

dependencies[] = ctools

configure = admin/config/content/firstmod

  • Name: The title of the module in the list.
  • Description: The description to show.
  • Core: Drupal’s available version – not that only indicates the major version 7.
  • Package: The group that owns the module – it can be an existing one or a new one.
  • Dependencies: Indicates the modules that are required for the operation, in this case, put ctools as an example but it is not necessary from beginning.
  • Configure: The URL path of the new module configuration form. This is optional and will require certain indications in the file module to work.

Secondly, we will have a look at the content of firstmod.module:


As we can see, the firstmod.module file could be completely empty, thus creating the simplest module possible…

And the result is the following:


Before we continue, we must introduce the concept of HOOK.

They are personalisation of actions. Hooks allow us to modify behavior of a method or function without modifying the code.

For example

We have a function that provides a list with the cities of Spain, and is prepared to use Hooks for their answer. When that functionality is used, before we answer it, invoke all the hooks that “exist” for it. Any of these hooks can take the list of cities and filter them, reducing the response to cities with more than 10000 inhabitants.

However, at any time, you can disable the Hook filter and when the function is called again the full list will return. All of this is done without touching the original function.

The configuration form

On the other hand, to add a configuration form to our module we have to create a Hook with hook_menu() of Drupal 7. The way to make this is by replacing the word hook_ with the name of our module, standing firstmod_menu(). Drupal 7 will find the hook and make it work when necessary.

Then, we add the following code to firstmod.module:

** * Implements hook_menu(). */

function firstmod_menu() {

$items = array();

$items['admin/config/content/firstmod'] = array(

'title' => t('Configuration of firstmod'),

'description' => t('Configure the firstmod module'),

'page callback' => 'drupal_get_form',

'page arguments' => array('firstmod_admin'),

'access arguments' => array('administer firstmod settings'),

'type' => MENU_NORMAL_ITEM, );

return $items; }

In this hook we return a list of menu options to be added to the administration panel. The key of the array will be the route to the configuration, the same as we declared in the info file.

  • Title: The title to show in the option. It is on English because Drupal uses this idiom as base and with t() function that translates it to the final idiom.
  • Page arguments: Indicates which function will be responsible for declaring the configuration form with the FORM API and “access arguments” indicates the necessary permission to access. If we leave the module as it is now, it can be accessed with the user 1.  A special user in Drupal with all the permissions. Also, since we have not declared the permit, we will not be able to give you access either.

Therefore, we will declare the permit using another hook. On this case of hook_permission() adding the next code to firstmod.module:

/** * Implements hook_permission(). */

function firstmod_permission() {

return array(

'administer firstmod settings' => array(

'title' => t('Administer firstmod configurations'),

'restrict access' => true)



We can give access to other users. However, we don’t have the configuration form. To create it, we will have to implant the function that we declare in the hook_menu(): ‘firstmod_admin’.

We are going to add again to firstmod.module:

* Formulario de configuración
* @return array The form structure.
function firstmod_admin() {
$form = array();

// Algo que configurar
$form['firstmod_something'] = array(
'#type' => 'textfield',
'#title' => t('Something'),
'#default_value' => variable_get(‘firstmod_something’, ""),
'#size' => 60,
'#maxlength' => 250,
'#description' => t("Something"),
'#required' => TRUE,

return system_settings_form($form);

Then, in this hook we take back an array, tunned as a configuration form, with a field called firstmod_something. Almost everywhere we put forward firstmod for assure us not “crash” with other modules in their configurations and methods.

  • variable_get(): it returns a global variable in Drupal. Using this function we ensure that the value is available for all requests to the server, regardless of the server that responds (sometimes websites use a lot of servers as it is just only one. We should look PHP documentation about this objective)

Let’s see how it looks.

For this, we activate the module…

After, we move to the configuration dashboard…

To give permission to other users…

This example and guide it serves as a starting point to create our Drupal 7 module . The next step is to see the documentation from and look the modules created by others… one of the advantages of the open source es that we can learn from others.

Do you have any doubts?  leave your comments or contact with Agilia Center.