Check installation requirements and do status reporting.
This hook has three closely related uses, determined by the $phase argument:
Note that this hook, like all others dealing with installation and updates, must reside in a module_name.install file, or it will not properly abort the installation of the module if a critical requirement is missing.
During the 'install' phase, modules can for example assert that library or server versions are available or sufficient. Note that the installation of a module can happen during installation of Drupal itself (by install.php) with an installation profile or later by hand. As a consequence, install-time requirements must be checked without access to the full Drupal API, because it is not available during install.php. For localization you should for example use $t = get_t() to retrieve the appropriate localization function name (t() or st()). If a requirement has a severity of REQUIREMENT_ERROR, install.php will abort or at least the module will not install. Other severity levels have no effect on the installation. Module dependencies do not belong to these installation requirements, but should be defined in the module's .info file.
The 'runtime' phase is not limited to pure installation requirements but can also be used for more general status information like maintenance tasks and security issues. The returned 'requirements' will be listed on the status report in the administration section, with indication of the severity level. Moreover, any requirement with a severity of REQUIREMENT_ERROR severity will result in a notice on the administration configuration page.
$phase: The phase in which requirements are checked:
A keyed array of requirements. Each requirement is itself an array with the following items:
Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.
function hook_requirements($phase) {
$requirements = array();
// Ensure translations don't break during installation.
$t = get_t();
// Report Drupal version
if ($phase == 'runtime') {
$requirements['drupal'] = array(
'title' => $t('Drupal'),
'value' => VERSION,
'severity' => REQUIREMENT_INFO,
);
}
// Test PHP version
$requirements['php'] = array(
'title' => $t('PHP'),
'value' => $phase == 'runtime' ? l(phpversion(), 'admin/reports/status/php') : phpversion(),
);
if (version_compare(phpversion(), DRUPAL_MINIMUM_PHP) < 0) {
$requirements['php']['description'] = $t('Your PHP installation is too old. Drupal requires at least PHP %version.', array(
'%version' => DRUPAL_MINIMUM_PHP,
));
$requirements['php']['severity'] = REQUIREMENT_ERROR;
}
// Report cron status
if ($phase == 'runtime') {
$cron_last = state()
->get('system.cron_last');
if (is_numeric($cron_last)) {
$requirements['cron']['value'] = $t('Last run !time ago', array(
'!time' => format_interval(REQUEST_TIME - $cron_last),
));
}
else {
$requirements['cron'] = array(
'description' => $t('Cron has not run. It appears cron jobs have not been setup on your system. Check the help pages for <a href="@url">configuring cron jobs</a>.', array(
'@url' => 'http://drupal.org/cron',
)),
'severity' => REQUIREMENT_ERROR,
'value' => $t('Never run'),
);
}
$requirements['cron']['description'] .= ' ' . $t('You can <a href="@cron">run cron manually</a>.', array(
'@cron' => url('admin/reports/status/run-cron'),
));
$requirements['cron']['title'] = $t('Cron maintenance tasks');
}
return $requirements;
}