A Drupal module is a PHP file that extends the functionality of your site. There are important differences between a Drupal module and a PHP page which can be confusing when you first start out.
- A module does not contain the main flow of control. Instead, Drupal will call specific, specially named functions defined in your module. These callback functions are called hooks, and there are several types, which we will discuss later.
- A drupal module needs to be activated using Drupal's administration UI. Before it is activated, a module's functionality cannot be used, and it does not use any resources.
- An activated module is loaded in its entirety on every page view, whether or not it is needed.
How do I get started?!
Which directory your module is located in has nothing to do with which URLs it's responsible for serving on your site. A single Drupal module may correspond to millions of separate pages on your website, or none at all. The way to define which URL paths your module handles is through Drupal's menu system. At a minimum, you need something like this:
mymodule_menu($may_cache)
{
$menu = array();
if ($may_cache)
{
$menu[] = array(
'path' => 'some/path',
'title' => t('My Example Page'),
'callback' => 'mymodule_hello',
'access' = user_access('access content'),
'type' => MENU_CALLBACK
);
}
return $menu;
}
function mymodule_hello()
{
$html = '<p>Hello World!</p>';
return $html;
}
This module creates a page at http://example.com/some/path using the default theme, a title of My Example Page and a body of Hello World!
Memory Use
Unfortunately, many Drupal modules (including those in core) are written as a single large .module file. The problem with this convention is that with stock distributions of PHP the entire .module file must be loaded, parsed, and stored in memory on every page view, even if a lot of the module is UI for administration or configuration which gets used a few times and then rarely if ever after that.
There are a few things that you can do to reduce the memory usage of your Drupal modules, and potentially speed up your Drupal site (or at least allow more simultaneous visitors):
1. Load on demand
If a function or class in your module does not need to be available on every page of your site, then load it on demand. Instead of
function mymodule_display_page($parameter)you could instead do:
{
//entire large function body included here
}
function mymodule_display_page($parameter)The
{
include_once dirname(__FILE__).'/display_page.inc.php';
return real_mymodule_display_page($parameter);
}
real_my_module_display_page() function would be defined inside the display_page.inc.php file (which you put in the same directory as your .module file and it would only be loaded when needed.
2. Move configuration functions into a different module
Remove configuration functions that are not frequently needed on a production site into a separate module. For example, if you have a settings form which would usually only be used while the site was under development, create a separate module for it.
Drupal 5 has a module dependency-tracking feature which makes this managable. Your configuration module, mymodule_config would let Drupal know that it depends on mymodule by adding a dependencies line in the mymodule_config.info file:
name = Configuration UI for mymoduleHandle table creation and upgradeds in mymodule.install, which would get installed automatically if you enabled the
dependencies = mymodule
mymodule_config module.