Drupal Custom Module — Create database table during installation
Drupal have three kinds of modules, core, contributed and custom. In this tutorial, you can learn how to create your own module in Drupal 7.
Creating module folder and files
Choose a short name for your module. For this example, we’ll choose “custom_block” as module name.Create a folder with module name (custom_block) in sites/all/modules/custom
directory.
Create a custom_block custom_block.info
file in the module directory for telling drupal about your module. custom_block.info
file contains the meta information of your module and it is required for your module.custom_block.info
file contains below code.
name = Make your own custom module
description = A custom module developement
core = 7.x
Create new file named custom_block.module and add begins with opening PHP tag.Omit the closing (?>) tag for avoiding the run-time issues on the certain server. Go to Modules listing page and scroll down of the list, you’ll see the Make your own custom module module under the OTHER category.Enable it.
Creating .install file
Your module needs a .install
file, which helps your module to create required table when the module is first enabled and removed when the module is uninstalled. Create a .install
file (custom_block.install
) into the root directory of your module. Using this file, we’ll create a products table into the database for storing the website product’s data.
hook_schema()
holds an array structure for representing one or more tables and their related keys and indexes.hook_schema()
is invoked before hook_install()
and after hook_uninstall()
is invoked. If the tables declared by hook_schema()
, then the tables will be automatically created when the module is first enabled and removed when the module is uninstalled.
<?php
function custom_block_schema(){
$schema['products'] = array(
'description' => 'The table for storing the products data.',
'fields' => array(
'id' => array(
'description' => 'The primary identifier for product.',
'type' => 'serial',
'not null' => TRUE,
'unsigned' => TRUE,
),
'name' => array(
'description' => 'product name.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
'created_date' => array(
'description' => 'created date time(yyyy-mm-dd H:i:s).',
'type' => 'varchar',
'mysql_type' => 'DATETIME',
'not null' => TRUE,
),
'status' => array(
'description' => 'product status(1=Unblock,0=Block).',
'type' => 'int',
'length' => 1,
'not null' => TRUE,
'default' => 1,
),
),
'primary key' => array('id'),
);
return $schema;
}
Update scripts implement the hook_update_N() hook and should be placed in your modules .install file. hook_update_N function is used to add, update or delete database tables, fields or contents at the time of updating your module.Example : You want to update name field type.
function custom_block_update_7001() {
db_change_field('products', 'name', 'name',
array(
'description' => 'Product name',
'type' => 'text',
'not null' => FALSE,
'size' => 'big',
)
);
}
Now run update.php and follow steps,after that database changes will apply.