How create install script for drupal module and create tables

Sponsors

Drupal is most popular CMS in the world for creating the web application. For custom purpose we need to create the drupal modules. We need the custom tables for creating the drupal. In this article I will tell how you can create the sample drupal module with custom two table. When you activate the drupal module then tables which are defined in the module will be get installed. When you unactive the drupal module tables will get deleted from database.

For creating the drupal module three files are necessary and that as follow:

1. yourmodule.info (you can use your module name

2. yourmodule.install

3. yourmodule.module

First .info file is important and in that file we will put module related information. mymodule.info file has following content.


; $Id$
name = My module
description = This module is to test the install feature
core = 6.x

For creating custom tables you can use following code in mymodule.install file.


<?php
function mymodule_schema() {
 $schema['mymodule_test'] = array(
 'description' => t('The base table for saved articles.'),
 'fields' => array(
 'id' => array(
 'description' => t('The primary identifier'),
 'type' => 'int',
 'unsigned' => TRUE,
 'not null' => TRUE),
 'uid' => array(
 'description' => t('The user identifier.'),
 'type' => 'int',
 'unsigned' => TRUE,
 'not null' => TRUE,
 'default' => 0),
 'name' => array(
 'description' => t('The name.'),
 'type' => 'varchar',
 'length' => '100',
 'not null' => TRUE,
 'not null' => TRUE),
 ),
 'primary key' => array('id'),
 );

 $schema['mymodule_test1'] = array(
 'description' => t('The base table for saved articles.'),
 'fields' => array(
 'id' => array(
 'description' => t('The primary identifier'),
 'type' => 'int',
 'unsigned' => TRUE,
 'not null' => TRUE),
 'uid1' => array(
 'description' => t('The user identifier.'),
 'type' => 'int',
 'unsigned' => TRUE,
 'not null' => TRUE,
 'default' => 0),
 'name' => array(
 'description' => t('The name.'),
 'type' => 'varchar',
 'length' => '100',
 'not null' => TRUE,
 'not null' => TRUE),
 ),
 'primary key' => array('id'),
 );

 return $schema;
}

function mymodule_install() {
 // Create my tables.
 drupal_install_schema('mymodule');
}

function mymodule_uninstall() {
 // Drop my tables.
 drupal_uninstall_schema('mymodule');
}
?>

.Module file is most important. In this file you can put the code which for you written the module. Now in this file Now I am giving you the sample menu code. mymodule.module file has following content.


<?php
function mymodule_menu() {

 $items['mymodule'] = array(
 'title' => 'mymodule View',
 'page callback' => 'mymodule_view',
 'access arguments' => array('access content'),
 );

 return $items;
}

function mymodule_view()
{
 return 'test';
}

?>

If you have some more doubts then please content me and comment on this article.

Incoming search terms:

You may like following Articles!

This entry was posted in drupal, tutorials and tagged , , by Wordpress API. Bookmark the permalink.

About Wordpress API

API means Application Program interface. In Wordpress API we are giving all the information about wordpress API. We worked for many wordpress plugins and themes. We have expertise in PHP, Databases, Graphic Design, Ruby on Rails, Java, Wordpress, Drupal and many CMS. We always give you the best solutions about wordpress and other related topics in This Blog. We will give you always fresh tips and tricks and techniques about wordpress in this blog.

6 thoughts on “How create install script for drupal module and create tables

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> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>