There must be at least two files to create a drupal module,
- module_name.info
- module_name.module
In this example I will use module_name as "cities". Then my two files would be cities.info and cities.module
Create a folder inside sites/all/modules which has the same name as your module, in this case it's "cities". Inside cities create two files cities.info and cities.module
It is possible to save your module in module folder but it is not recommended as that folder is kinda reserved for drupal core module. Instead of messing with core guys it is advisable to maintain a separate place for custom modules.
cities.info
cities.info file contains all the necessary information for drupal to know about your module. name, description, core are the musts in this file. There are also set of optional info which you can include but here in this example I only use required information only.
; $Id$ name = cities description = This is a module that display list of cities core = 6.x
cities.module
what exactly is a hook?
Here is a good question if you are a newbie to drupal module development. Concept of hook is very important when it comes to drupal module development. Module system in drupal is based on hook, simply it's a php fuction which defines set of parameters and a result type. Hooks allow modules to interact with the drupal core.
In this example module I've used three hooks, hook_help, hook_perm and hook_block.
hook_help : provides help and additional information.
hook_perm : defines permissions for the module.
hook_block: creates a block or set of blocks.
We have to replace hook with our module name when using them.
This example block module cities will display a list of cities pulled out of a database.
<?php // $Id: cities.module$ /** * @file * This is a module that display list of cities */ /** * Implementation of hook_help(). */ function cities_help($path, $arg) { switch ($path) { case 'cities': return '<p>'. t('<b>help hook. This is a module that display list of cities.</b>') .'</p>'; } } /** * Implementation of hook_perm(). */ function cities_perm() { return array('access cites'); } /** * grabbing data from the database */ global $arr; $arr=array(); global $datas; $datas=""; $link = mysql_connect ('localhost', 'root', ''); $db = mysql_select_db ('cities', $link); $sql = "SELECT name FROM city"; $sel = mysql_query($sql); if (mysql_num_rows($sel) > 0) { $i=0; while($row = mysql_fetch_array($sel)) { $arr[$i]=$row['name']; $i=$i+1; } } foreach( $arr as $key => $value){ $datas=$datas." ".$value."<br/>"; } mysql_close(); /** * Implementation of hook_block(). */ function cities_block($op = 'list', $delta = 0, $edit = array()) { global $datas; if ($op == "list") { $block = array(); $block[0]["info"] = t('cities'); return $block; } elseif ($op == 'view') { $block_content = '<p>This is a block that display list of cities.</p>'; $block['subject'] = 'City List'; $block['content'] = $datas; return $block; } }
Omitting the closing php tag is a drupal coding standard which prevents runtime issues.
Don't forget to enable your module (administrator-->modules) and pick a suitable place (administrator-->block) for it. Available permissions for this module can be viewed and customized from administrator-->permissions
That's it, now with this basic knowledge in hand it's not far away when you write more complex modules.