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



    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.


    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


    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.


    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.



    The following two tabs change content below.

    Umair Butt

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">