How to create a Custom Module in Drupal 7

drupa7

What do you learn learn from this post?

  1. You will learn how to create a custom module in Drupal 7.
  2. You will learn how to create custom table using hook_schema() in Drupal 7.
  3. You will learn how to insert data into the custom table using hook_submit() in Drupal 7.
  4. You will learn how to fetch data from custom table and show it as a table view in Drupal 7.

Basically, The module which we are going to create will have a custom form with a custom table in the Database.

Custom modules has to be stored in the following Drupal Path,

  • drupal_folder\sites\all\modules\

If you’re using xampp, then the path will be, C:\xampp\htdocs\drupal_folder\sites\all\modules

First create a folder called “custom_module” and the folder will have three files, they are,

  • custom_form.info
  • custom_form.install (this file is optional, it requires only if you are going to create a custom_table )
  • custom_form.module

1)  custom_form.info

This info file tells the Drupal and User about your module. It’s a very simple file with key-pair values, lets see the file:

There are plenty of other info you can put in this file, to create a simple module, this is enough.

2) custom_form.install

Like I already mentioned, this file is not always required to create a custom module, but in our tutorial it is, because we are going to create a custom table as well.

To create a custom table in Drupal, you can use hook_schema(), lets see the code:

If you see the code, you can easily understand that our table name is ‘custom_form’ and there are 4 fields in the table, they are

  1. custom_form_id -> It’s a primary key field as well as auto incremented.
  2. name -> VARCHAR with length 100
  3. email -> VARCHAR with length 100
  4. created -> Integer

This file executes and creates the table at the time of enabling the module in the Drupal admin backend.

3) custom_form.module

This is the main file which will have all the logic for creating a form, inserting value into the table etc.,

There are 4 Drupal hooks we have used in the above .module file, which are

  1. hook_menu() -> used to create a menu as well register a URL, also to include callback functions.
  2. hook_form() -> used to create custom form.
  3. hook_validate() -> This hook will trigger before form submit, so we can validate the fields.
  4. hook_submit() -> This hook will trigger at the time of form submission. (insert the values in the custom_table)

Now we are ready with a custom module, Let’s see how it looks in the Drupal Modules page:

module-page

Cool isn’t it?, lets enable the module. Just click the checkbox and click “Save configuration” button to enable the module.

Once the module is enabled, you can see there is a table created in the database, see the below screenshot (custom_form):

custom-table

You can also see there are two new menus created in the front end (My Custom Form and Custom Table View),

createdmenus

Click the My Custom Form, you will see two fields with a submit button.

 

custom-form

You can now enter the values in the form and click “Save” to store values in the database, to see the stored values click “Custom Table View” to see the list of records entered in the custom_table.

tableview

I hope you have learned the basics of how to create a custom module and custom table in Drupal 7.

For your convenience I have attached the module also in this post, Click the below button to download the module as a Zip.

download

 

Add a Comment

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