Creating Drupal Module

    Here is a sample Drupal module that will enable us to add and display Student Information.
  1. Create the student.info file
  2. ; $Id$
    name = Student Module
    description = A sample Drupal module.
    core = 6.x
    

  3. Create the student.install file
  4. <?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');
    }
    

  5. Create the student.module file
  6. <?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;
            }
    }
    

  7. Save all these files to drive:\your\wamp\www\sites\all\modules\student\
  8. To install this newly created module, click Administer -> Site building -> Modules then 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 configuration button to install it.

    You will then observe from the Navigation menu that you now have links to Add Student Information and view the Students List.

  9. Here is how the Add Student Information page look like:

    figure18

    And here is how the Students List page look like:

    figure19

A Seminar-Workshop in Web Site Development using Drupal at Misamis University:

  1. Slides
  2. Installation Guide for Drupal 6.15
  3. Creating the About Me Page
  4. Posting Blogs
  5. Rearranging and Changing the Look and Feel of your site
  6. Creating Drupal Module

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.