Experimental project

This is a sandbox project, which contains experimental code for developer use only.

Objective

  • Form which demonstrates how information is stored in database table
  • Retrieval of information submitted by the form on a particular page

Project Files:-

  • employee_registration.info
  • Stores the Name,Description and Version of module
  • employee_registration.module
  • Demonstrate the process of Form generation
  • employee_registration.install
  • Database schema of the Form

Below are few important hooks and drupal functions i have used in my project:-

  • hook_permission
  • hook_menu
  • hook_form
  • hook_form_validate
  • hook_form_submit
  • hook_form_submissions
  • hook_schema
  • drupal_form_get
  • drupal_set_message
  • form_set_error

--------------------------------------------------------------------------------------------------------------------------
employee_registration.info

name = "employee_registration"
description = "Registration form"
version = 7.x
core = 7.x
--------------------------------------------------------------------------------------------------------------------------
employee_registration.install


function employee_registration_schema() {
$schema = array();
//The name of table in database is registration
$schema['registration'] = array(
'description' => 'an example table',
'fields' => array(
't_id' => array(
'description' => 'the primay identifier for my table registration',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'empid' => array(
'description' => 'the field to enter a number',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'fullname' => array(
'description' => 'the field to enter a short string',
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'default' => '',
),
'message' => array(
'description' => 'the field to enter a long string',
'type' => 'text',
'not null' => TRUE,
),
),
'indexes' => array(
'employee_registration_mynumber' => array('empid'),
),
'primary key' => array('t_id'),
);
return $schema;
}


-------------------------------------------------------------------------------------------------------------------------
employee_registration.module

<?php

//hook_Permissions used to set permission to access submit employee registration form
function employee_registration_permission()
{
return array(
'submit employee_registration' => array(
'title' => t('submit employee_registration'),
'description' => t('submit the employee_registration form'),
),

//hook_Permissions used to set permission to access employee registration submissions
'access employee_registration submissions' => array(
'title' => t('Acesss employee_registration submissions'),
'description' => t('access the employee_registration submissions'),
),
);
}

//define a menu
function employee_registration_menu()
{
$items = array();
$items['employee_registration'] = array(
'title' => 'Registration Form',
'type' => MENU_NORMAL_ITEM,
'access arguments' => array('submit employee_registration'),
'page callback' => 'drupal_get_form',
'page arguments' => array('employee_registration_form'),
);
$items['employee_registration_submissions'] = array(
'title' => 'Employee Registration Submissions',
'type' => MENU_NORMAL_ITEM,
'access arguments' => array('access employee_registration submissions'),
'page callback' => 'employee_registration_submissions',
);
return $items;
}
//Creation of Form
function employee_registration_form($form,&$form_state) {
$form['empid'] = array(
'#type' => 'textfield',
'#title' => t('Employee ID'),
'#size' => 10,
'#maxlength' => 10,
'#required' => TRUE,
'#description' => t('Please enter your employee identity number'),
);
$form['fullname'] = array(
'#type' => 'textfield',
'#title' => t('Fullname'),
'#size' => 10,
'#maxlength' => 10,
'#required' => TRUE,
'#description' => t('please enter your full name'),
);
$form['message'] = array(
'#type' => 'textarea',
'#title' => t('Body'),
'#required' => TRUE,
'#description' => t('Enter your message'),
'#default value' => '',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Add item'),
);

return $form;
}
// form vlidation for employee id
function employee_registration_form_validate($form, &$form_state)
{
if (!is_numeric($form_state['values']['empid'])) {
form_set_error('empid', t('you must enter a valid employee identity number'));
return FALSE;
}
return TRUE;
}
//Query to retrieve employee submissions from database table registration
function employee_registration_form_submit($form,&$form_state) {
$t_id = db_insert('registration')
->fields(array(
'empid' => $form_state['values']['empid'],
'fullname' => $form_state['values']['fullname'],
'message' => $form_state['values']['message'],
))
->execute();

drupal_set_message(t('your form entry has been added'));

}
function employee_registration_submissions()
{
$results = db_query("SELECT * FROM {registration}");
$header = array(t('ID'),t('Emp-ID'),t('Fullname'),t('Message'));
$rows = array();
foreach($results AS $result) {
$rows[] = array(
$result->t_id,
$result->empid,
$result->fullname,
$result->message,
);
}
return theme('table',array('header' => $header,'rows' => $rows));
}
--------------------------------------------------------------------------------------------------------------------------

Project information