0% found this document useful (0 votes)
52 views13 pages

Drupal Module

This document discusses how to create a basic Drupal module. It explains that a module contains a .module file with PHP functions and a .info file with module metadata. It provides examples of how to define permissions with hook_perm(), add menu items with hook_menu(), and write callbacks to output content. Specifically, it shows how to create a module that lists students from a database with pagination using these Drupal hooks and functions.

Uploaded by

Denny England
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views13 pages

Drupal Module

This document discusses how to create a basic Drupal module. It explains that a module contains a .module file with PHP functions and a .info file with module metadata. It provides examples of how to define permissions with hook_perm(), add menu items with hook_menu(), and write callbacks to output content. Specifically, it shows how to create a module that lists students from a database with pagination using these Drupal hooks and functions.

Uploaded by

Denny England
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Drupal Module Development

Module Development
On this training We will discuss the module structure of drupal. We will explain how the .module and .info files used for building a module. we will try to create a simple drupal module that will list all the students on the database with pagination.

Creating .module File


<?php function module1_perm(){ /* permission description here */ } function module1_menu(){ /* Menu item here */ } function module1_init(){ /* module1 initialize config here (Optional)*/ } Filename: module1.module

hook_perm for Permission


/** Implements hook_perm **/ function module1_perm(){ return array(access to module1); }

This function provides permission for the


current module. You can have many permission on the same module.

The hook_menu for Navigation


/** Implements hook_menu **/ function module1_menu(){ $items = array(); $items['module1'] = array( 'title' => 'Module1', 'page callback' => 'module1_main', 'access arguments' => array('access to module1'), type => MENU_NORMAL_ITEM ); $items['module1/listofstudent'] = array( 'title' => 'List of Students', 'access arguments' => array('access to module1'), 'page callback' => 'listofstudent', ); return $items; }

hook_menu Structure
$items['module1'] = array( 'title' => 'Module1', 'page callback' => 'module1_main', 'access arguments' => array('access to module1'), type => MENU_NORMAL_ITEM );

module1 a url represensation on the web browsers address bar.


e.g http://localhost/drupal/?q=module1 title the anchor (<a></a>) html represensation of the url. e.g <a href=http://localhost/drupal/?q=module1>Module1</a> page callback a function to call when this url is accessed. This will serve as the content of the url accessed. Access arguments - a permission that is set to this url. Must exists in the hook_perm.

Hook_menu Item Type


MENU_NORMAL_ITEM Default value. Display
at the main navigation. MENU_LOCAL_TASK Display the links by tab for the current module. MENU_CALLBACK can be used as the a ajax request handler as long as the function will not return any values.
For more menu type please refer to : http://api.drupal.org/api/drupal/developer%21hooks%21core.php/functi on/hook_menu/6

Module1 Page Callback


A page callback function must return a string to form a content. Drupal automatically generates the page structure and appends the content of the page callback function.
/** a page callback for accessing module1 */ function module1_main(){ return '<h1>This is my first module.</h1>'; }

List of Students Page Callback


function listofstudent(){ drupal_add_css(drupal_get_path('module','module2').'/module2.css'); $query = "SELECT CONCAT(lastname,', ', firstname, ' ', middlename) as name, studmajor FROM tblstudent ORDER BY lastname"; $count = "SELECT COUNT(*) FROM (".$query.") AS count_query"; $result = pager_query($query, 15, 0, $count_query); $comments = array(); $i = isset($_GET['page']) ? ($_GET['page'] * 15) + 1 : 1; $output = "<table><thead><tr><th>Student</th><th>Course</th></tr></thead><tbody>"; while($records = db_fetch_object($result)) { $output .= "<tr><td>".($i++).".) $records->name</td><td>$records>studmajor</td></tr>"; } $output .= "</tbody></table>"; $output .= theme('pager'); return $output; }

Creating .info file


name = Module1 description = Module1 example for Drupal Training core = 6.x package = "Drupal Training Modules" ; Information added by drupal.org packaging script on 2012-07-13 version = "6.x-1.x-dev" core = "6.x Filename: module1.info

Module1 module example


<?php /** * Grants Permission to this module. */ function module1_perm(){ return array('access to module1'); } /** * Implement a hook menu. * */ function module1_menu(){ $items = array(); $items['module1'] = array( 'title' => 'Module1', 'page callback' => 'module1_main', 'access arguments' => array('access to module1'), ); $items['module1/listofstudent'] = array( 'title' => 'List of Students', 'access arguments' => array('access to module1'), 'page callback' => 'listofstudent', ); return $items;

Module1 module example cont


function module1_main(){ return '<h1>This is my first module.</h1>'; } function listofstudent(){ drupal_add_css(drupal_get_path('module','module2').'/module2.css'); $query = "SELECT CONCAT(lastname,', ', firstname, ' ', middlename) as name, studmajor FROM tblstudent ORDER BY lastname"; $count = "SELECT COUNT(*) FROM (".$query.") AS count_query"; $result = pager_query($query, 15, 0, $count_query); $comments = array(); $i = isset($_GET['page']) ? ($_GET['page'] * 15) + 1 : 1; $output = "<table><thead><tr><th>Student</th><th>Course</th></tr></thead><tbody>"; while($records = db_fetch_object($result)) { $output .= "<tr><td>".($i++).".) $records->name</td><td>$records>studmajor</td></tr>"; } $output .= "</tbody></table>"; $output .= theme('pager'); return $output; }

Drupal Module Development

ICT Center
We are making IT happen

Integrity

Commitment

Teamwork

You might also like