Module dependency checker
Do you have a module that depends on other modules? Have you ever wished there was a way to ensure they were installed before your module is enabled?? Well, I've figured out a fairly elegant way for contrib modules to do simple dependency checking, via hook_form_alter. Here's how it's done:
- Add this section of code to your module's hook_form_alter:
<?php
/* this is a check for module dependencies. the only way we
can ensure this check happening when the module is initially
enabled is to insert the check for when the form is initially
built, which will also be caught when the admin/module page is
reloaded upon submission. this means we never want to call this
function when the form has been submitted, so make sure there's
no $_POST. */
if ($form_id == 'system_modules' && !$_POST) {
modulename_system_module_validate($form);
}
?>
<?php
/**
* Validates module dependencies for the module.
*
* @param $form The form array passed from hook_form_alter.
*
* Set the $module variable to a string which is the name of the module, minus
* the .module extension. Set $dependencies to an array of module names which
* the module is dependent on--each element is a string which is the module name
* minus the .module extension. Note that this will not check for any dependencies
* for the modules this module depends on--only those that are explicitly listed in
* the $dependencies array.
*/
function modulename_system_module_validate(&$form) {
$module = 'modulename';
$dependencies = array('dependentmodule1', 'dependentmodule2', 'etc');
foreach ($dependencies as $dependency) {
if (!in_array($dependency, $form['status']['#default_value'])) {
$missing_dependency = TRUE;
$missing_dependency_list[] = $dependency;
}
}
if (in_array($module, $form['status']['#default_value']) && isset($missing_dependency)) {
db_query("UPDATE {system} SET status = 0 WHERE type = 'module' AND name = '%s'", $module);
$key = array_search($module, $form['status']['#default_value']);
unset($form['status']['#default_value'][$key]);
drupal_set_message(t('The module %module was deactivated--it requires the following disabled/non-existant modules to function properly: %dependencies', array('%module' => $module, '%dependencies' => implode(', ', $missing_dependency_list))), 'error');
}
}
?>I think the code comments explain the feature pretty well. The main trick is that you can't check the modules dependencies upon it's initial form submission in admin/modules because, well, it's not enabled yet... :): fortunately, there's a drupal_goto which sends you right back to the admin/modules page--so we can catch it there and manually disable the module in the database.
This works both when the module is initially enabled, and if somebody happens to try and disable any dependent modules later. To keep it simple, I just warn the user and disable the module. This solution is not foolproof (with layered dependencies it's possible that a module might pass when it shouldn't--but those are almost non-existent in Drupal ATM AFAIK), but it will work for probably 98% of the use cases--and providing this protection is as hard as dropping in the code above and setting your dependencies.

not in 5.x
This doesn't work in 5.0, nor is it necessary. I noticed this mentioned in the 5.0 changelog. "added support for module dependencies". Since I wasn't able to find documentation of this, I dug around in the source code, and found these remarks in the comments (excerpts below):
modules/system/system.module
Modules can be enabled or disabled and set for throttling if the throttle module is enabled. The list of modules gets populated by module.info files, which contain each module's name, description and dependencies. @sa _module_parse_info_file for information on module.info descriptors.
Dependency checking is performed to ensure that a module cannot be enabled if the module has disabled dependencies and also to ensure that the module cannot be disabled if the module has enabled dependents.
includes/module.inc
Information stored in the module.info file:
dependencies - A space delimited list of the short names (shortname) of other modules this module depends on.