This tutorial describes how to create a module for Drupal 6. It is an update to the tutorial for Drupal 5. Please see comments there, also. Much of this tutorial is valid for Drupal 5 as well, but you should check the API documentation, as well as the documentation on how to update your modules from one version of Drupal to another.
A module is a collection of functions that link into Drupal, providing additional functionality to your Drupal installation. After reading this tutorial, you will be able to create a basic block module and use it as a template for more advanced modules and node modules.
This tutorial will not necessarily prepare you to write modules for release into the wild. It does not cover caching, nor does it elaborate on permissions or security issues. Use this tutorial as a starting point, and review other modules and the Drupal handbook, the documentation on writing secure code and the Coding standards for more information.
This tutorial assumes you have:
- Basic PHP knowledge, including syntax and the concept of PHP objects
- Basic understanding of database tables, fields, records and SQL statements
- A working Drupal installation
- Drupal administration access
- Webserver access
This tutorial does not assume you have any knowledge about the inner workings of a Drupal module. This tutorial will not help you write modules for versions of Drupal earlier than 5.
For caching, see Cache API and caching tutorials
This tutorial goes through all of the steps necessary to create a module that deploys a block listing recent blog posts and forum discussions. Here is an outline of what is covered in each of the tutorial pages:
- Getting Started: Creating a directory for your module and the initial .module file
- Telling Drupal about your module: Creating a .info file for your module, and implementing
hook_help()to provide a brief description of your module in Drupal's help system
- Telling Drupal who can use your module: Implementing
hook_perm(), which defines permission types for your module
- Declaring block content: Start of implementation of
hook_block()-- the part that tells Drupal our module deploys a block
- Generating the block content: Finish implementing
hook_block()-- the part that actually displays the block content. This involves running a database query to retrieve recent blog and forum posts, and then displaying them.
- Installing, enabling and testing the module: Getting the initial module working
- Creating a module configuration (settings) page: Modifying the module so that the user can define what "recent" means, by implementing
hook_menu()and using the Drupal Form API to define a settings page
- Generating page content: Adding a full page view to the module, in case there are more recent posts than will fit easily in the block - part 1: write a function that generates the page
- Letting Drupal know about the new function: Part 2 of adding the full page view to the module -- using
hook_menu()to give the page a URL
- Adding a 'more' link and showing all entries: Part 3 of adding the full page view to the module -- linking the new URL as a "more" link in the block