Creating Drupal Module
-
Here is a sample Drupal module that will enable us to add and display Student Information.
- Create the
student.infofile - Create the
student.installfile - Create the
student.modulefile - Save all these files to
drive:\your\wamp\www\sites\all\modules\student\ -
To install this newly created module, click
Administer -> Site building -> Modulesthen scroll down to the bottom of the page to see the Student Module listed as one of the modules in Drupal that you can enable and install.Enable Student Module by checking its checkbox then click the
Save configurationbutton to install it.You will then observe from the Navigation menu that you now have links to
Add Student Informationand view theStudents List.
; $Id$ name = Student Module description = A sample Drupal module. core = 6.x
<?php
// $Id$
/**
* Implementation of hook_schema()
*/
function student_schema()
{
$schema['student_info'] = array(
'description' => 'Student Information',
'fields' => array(
'id' => array(
'description' => 'The primary identifier for student_info.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE),
'student_id' => array(
'description' => 'Student ID Number',
'type' => 'varchar',
'length' => 5,
'not null' => TRUE),
'first_name' => array(
'description' => 'First Name',
'type' => 'varchar',
'length' => 20,
'not null' => TRUE,
'default' => ''),
'middle_name' => array(
'description' => 'Middle Name',
'type' => 'varchar',
'length' => 20,
'not null' => TRUE,
'default' => ''),
'last_name' => array(
'description' => 'Last Name',
'type' => 'varchar',
'length' => 20,
'not null' => TRUE,
'default' => '')
),
'primary key' => array('id'),
'unique keys' => array('student_id' => array('student_id')),
);
return $schema;
}
/**
* Install Student module
*/
function student_install() {
drupal_install_schema('student');
}
/**
* Uninstall Student module
*/
function student_uninstall() {
drupal_uninstall_schema('student');
variable_del('student_id');
variable_del('student_fname');
variable_del('student_mname');
variable_del('student_lname');
}
<?php
// $Id$
/**
* Hook help
*/
function student_help($path, $arg) {
$output = '';
switch ($path) {
case "admin/help#student":
$output = '<p>' . t("Student Module") . '</p>';
break;
}
return $output;
}
/**
* Hook menu
*/
function student_menu() {
$items = array();
$items['student_add'] = array(
'title' => 'Add Student Information',
'description' => 'Add Student Information',
'page callback' => 'drupal_get_form',
'page arguments' => array('student_add'),
'access arguments' => array('add student information'),
'type' => MENU_NORMAL_ITEM
);
$items['students_list'] = array(
'title' => 'Students List',
'description' => 'List all Students',
'page callback' => 'students_list',
'access arguments' => array('list students'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Hook perm
*/
function student_perm() {
return(array('add student information', 'list students'));
}
/**
* Add Student Information
*/
function student_add()
{
$form = array();
$form['student_info']['student_id'] = array(
'#type' => 'textfield',
'#title' => t("ID Number"),
'#default_value' => variable_get('student_id', ''),
'#size' => 10,
'#maxlength' => 5,
'#description' => t("5 digit Student ID"),
'#required' => TRUE
);
$form['student_info']['student_fname'] = array(
'#type' => 'textfield',
'#title' => t("First Name"),
'#default_value' => variable_get('student_fname', ''),
'#size' => 30,
'#maxlength' => 20,
'#description' => t("First Name"),
'#required' => TRUE
);
$form['student_info']['student_mname'] = array(
'#type' => 'textfield',
'#title' => t("Middle Name"),
'#default_value' => variable_get('student_mname', ''),
'#size' => 30,
'#maxlength' => 20,
'#description' => t("Middle Name"),
'#required' => TRUE
);
$form['student_info']['student_lname'] = array(
'#type' => 'textfield',
'#title' => t("Last Name"),
'#default_value' => variable_get('student_lname', ''),
'#size' => 30,
'#maxlength' => 20,
'#description' => t("Last Name"),
'#required' => TRUE
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t("Save")
);
return $form;
}
/**
* hook submit
*/
function student_add_submit($form, $form_state)
{
$id = $form_state['values']['student_id'];
$fname = $form_state['values']['student_fname'];
$mname = $form_state['values']['student_mname'];
$lname = $form_state['values']['student_lname'];
$sql = sprintf("INSERT INTO student_info(student_id, first_name, middle_name, last_name) " .
"VALUES('%s', '%s', '%s', '%s')", $id, $fname, $mname, $lname);
$result = db_query($sql);
if ($result === FALSE) {
variable_set('student_id', $id);
variable_set('student_fname', $fname);
variable_set('student_mname', $mname);
variable_set('student_lname', $lname);
form_set_error('student_info', t("Error saving student info."));
} else {
drupal_set_message(t("Student Info successfully saved."));
}
}
/**
* List all students
*/
function students_list()
{
$sql = "SELECT * FROM student_info";
$result = db_query($sql);
if ($result === FALSE) {
return FALSE;
} else {
$mypage = '
<table>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
</tr>
';
while ($data = db_fetch_object($result)) {
$mypage .= '
<tr>
<td>' . $data->student_id . '</td>
<td>' . $data->first_name . '</td>
<td>' . $data->middle_name . '</td>
<td>' . $data->last_name . '</td>
</tr>';
}
$mypage .= '
</table>';
return $mypage;
}
}
Here is how the Add Student Information page look like:
And here is how the Students List page look like:
A Seminar-Workshop in Web Site Development using Drupal at Misamis University:



Post new comment