How to Create a Custom Module in Drupal 7

How to Create a Custom Module in Drupal 7

Hey guys,

In this post I’m going to show you how to create your own custom module in Drupal 7. Before you get started though, you should have basic knowledge of PHP and MySQL as this tutorial involves some coding.

Anyway, after you’ve installed Drupal 7, go to the directory named Sites/All/Modules and follow the steps below to create your custom module.

Step 1: Create Custom Folders

Create a new folder named ‘Custom’ under the root directory Sites/All/Modules. All Drupal 7 custom modules are created inside this folder.

As you know, a Drupal module contains many files, but at the basic level, you only need two files in order for the Drupal 7 module to functions. These files are:

  • custom_module_name.module

Things to Keep In Mind before Creating the Drupal 7 module

When you create the custom drupal module folder (above), make sure to give it a name that is similar to your custom module’s name. For example, if your custom module is called ‘books .module’ then your custom module’s folder should also be called as ‘books’. This will help in making your drupal development module much more convenient to develop.

Also, make sure that the name you pick for your custom Drupal module development is not similar to any existing Drupal 7 module names (both custom and contributed modules). Another thing to keep in mind is that you should use only alpha-number, lowercase letters and underscores in your custom module’s name.
Now that we’ve clarified those points, let’s get back to our tutorial.

Step 2: Create Empty Files

Inside the ‘books’ folder, create the following two empty files.

  • (this is the description of your module)
  • books.module (this contains the actual code including hooks).

Now, open the file and add the below code to it and hit Save.
name = books
description = “Manage Books”
core = 7.x

The ‘name’ above contains the name of the module or any custom text that you want to display on the module’s configuration page. The ‘description’ describes what your module does and you can add any custom text for it that will also be displayed on the module’s configuration page. The ‘core’ part refers to the version of Drupal that this particular module was written for.
There are more options that you can put in the .info file, but the fields above should be added.


Step 3: Insert PHP Open Tag

Open the books.module file in the text editor and insert a php open tag at the beginning of the file and save it.
Note : It is recommended that you don’t include a php end tag in the above file.
Once you’ve done that, you can place your code into the books.module file, but first, you’ll need to enable this module via the admin area.
To enable the module, just login to the Drupal admin area then click on the modules menu. Find your module by looking up its name in the grid displayed. Select your module and then click the save configuration button that you see at the bottom of the grid. Your module is now active and you can use it for further development.


Step 4: Define Module Setting

The next step is to define some setting in the books.module file so you can access it via a URL, to view or access its content. To do this, you need to perform the following steps.

  • Define a path where you want to access the content from.
  • Create the content

To make a path, you need to create a Drupal hook_menu function. ‘Hook’ indicates the name of the module, so in our case the function’s name will be books_menu()

Now, open the books.module file and add the below code to it and hit Save.

* implement hook_menu()
* create menu item
books_menu function will create a menu item which can be access with localhost/path/to/books

 function books_menu() {
    $items = array();
     $items['books'] = array(
        'title'             =>  'Books View',  //page title
        'description'       =>  'Books Information',  //description show when mouse hover on link
        'page callback'     =>  'books_list',  //callback function which is invoked when menu item is called.
        'access callback'   =>  true,  //any user can access this page
     return $items;



Make sure to clear the cache when you make any changes to a menu item or create a new menu item, as these might not be reflected unless you clear the cache. To clear the cache go to localhost/your/path/admin/config/development/performance


Step 5: Manage Themes

To manage the code better, you should create a separate file to manage themes. You can also do that by adding the above function into the books.module file. We’ll use the former approach in this tutorial.

Create a new file named a‘’ under the custom module folder i.e. sites\all\modules\custom\books\

The above file basically allows you to display data through the theme template.

Next, you need to create a theme menu which can be used to load template files (tpl files) from the theme folder i.e. sites/all/themes/custom/{current_theme_name}/templates/books

This tpl file will house the HTML and styles of your content in whatever manner you wish to display it. Theme/Template is basically called when all your content has been fetched from the database and is ready to be displayed.

Next, open the ‘’ in the text editor and add the below code to it and hit Save.



* hook_menu to load tpl files from theme folder

 * i.e sites/all/themes/custom/{current_theme_name}/templates/books
 * current_theme_name = fiction 
function books_theme() {
 $path_to_theme = drupal_get_path('theme', variable_get('theme_default', 'fiction'));
 $themes = array(); 
$themes['books_list'] = array(
'template' => 'books-list',
'path' => $path_to_theme . '/templates/books/', 
    return $themes;



Step 6: Insert Themes File

Now, open the books.module file and include the file in it, and also add the below code to it and hit Save.

require_once (‘’); // at the top of books.module

Code snippet starts here


The codebelow should be added after books_menu() { }

// page callback function which is defined above in books menu item

function books_list() {
    // select all rows from books table
    $query = db_select('books', 'tr')
            ->condition('is_published', 1);
 $query generates below query
 SELECT tr.* FROM books tr WHERE tr.is_published = 1
// execute above query to get the results from database table.
$result = $query->execute();
//send data to custom theme template
//theme books_list created under sites\all\themes\custom\fiction\templates\books\books-list.tpl.php
$output = theme('books_list', array('data'=> $result)); 
return $output;


Code snippet ends here



Step 7: Code Insertion

Open the books-list.tpl.php file (sites\all\themes\custom\fiction\templates\books\books-list.tpl.php) in the text editor and add the below code to it and hit Save.

<h2>Create a Custom Module in Drupal 7 (Part1)</h2>
if(count($data) > 0)
    echo "<table><tr><th>Id</th><th>Title</th><th>Author</th><th>Description</th></tr>";
    $count = 1;
foreach($data as $v)
echo "<tr>";
echo "<td>" . $count . "</td>";
echo "<td>" . $v->books_title . "</td>";
echo "<td>" . $v->books_author . "</td>";
echo "<td>" . $v->books_description . "</td>";
echo "</tr>";
echo "</table>";    
    echo "No records found";


And that’s it!You’ve just created your custom module with a custom theme template. You can view it via the URL localhost/your/path/books.


USA408 365 4638


1301 Shoreway Road, Suite 160,

Belmont, CA 94002

Contact us

Whether you are a large enterprise looking to augment your teams with experts resources or an SME looking to scale your business or a startup looking to build something.
We are your digital growth partner.

Tel: +1 408 365 4638
Support: +1 (408) 512 1812