Community Documentation

hook_requirements

5 install.php hook_requirements($phase)
6 install.php hook_requirements($phase)
7 system.api.php hook_requirements($phase)
8 system.api.php hook_requirements($phase)

Check installation requirements and do status reporting.

This hook has three closely related uses, determined by the $phase argument:

  • Checking installation requirements ($phase == 'install').
  • Checking update requirements ($phase == 'update').
  • Status reporting ($phase == 'runtime').

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 the administration overview page.

Parameters

$phase: The phase in which requirements are checked:

  • install: The module is being installed.
  • update: The module is enabled and update.php is run.
  • runtime: The runtime requirements are being checked and shown on the status report page.

Return value

A keyed array of requirements. Each requirement is itself an array with the following items:

  • title: The name of the requirement.
  • value: The current value (e.g., version, time, level, etc). During install phase, this should only be used for version numbers, do not set it if not applicable.
  • description: The description of the requirement/status.
  • severity: The requirement's result/severity level, one of:

Related topics

▾ 18 functions implement hook_requirements()

color_requirements in modules/color/color.install
Implements hook_requirements().
file_requirements in modules/file/file.install
Implements hook_requirements().
image_requirements in modules/image/image.install
Implements hook_requirements() to check the PHP GD Library.
install_check_requirements in includes/install.core.inc
Checks installation requirements and reports any errors.
install_verify_requirements in includes/install.core.inc
Installation task; verify the requirements for installing Drupal.
node_requirements in modules/node/node.module
Implements hook_requirements().
openid_requirements in modules/openid/openid.install
Implements hook_requirements().
requirements1_test_requirements in modules/simpletest/tests/requirements1_test.install
Implements hook_requirements().
simpletest_requirements in modules/simpletest/simpletest.install
Implements hook_requirements().
system_requirements in modules/system/system.install
Test and report Drupal installation requirements.
unicode_requirements in includes/unicode.inc
Return Unicode library status and errors.
UpdateScriptFunctionalTest::testRequirements in modules/system/system.test
Tests that requirements warnings and errors are correctly displayed.
update_check_requirements in ./update.php
Check update requirements and report any errors or (optionally) warnings.
update_extra_requirements in ./update.php
Returns (and optionally stores) extra requirements that only apply during particular parts of the update.php process.
update_fix_d7_requirements in includes/update.inc
Perform Drupal 6.x to 7.x updates that are required for update.php to function properly.
update_requirements in modules/update/update.install
Implements hook_requirements().
update_script_test_requirements in modules/simpletest/tests/update_script_test.install
Implements hook_requirements().
_system_sort_requirements in modules/system/system.module
Helper function to sort requirements.

File

modules/system/system.api.php, line 2949
Hooks provided by Drupal core and the System module.

Code

<?php
function hook_requirements($phase) {
  $requirements = array();
  // Ensure translations don't break at install time
  $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 = variable_get('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;
}
?>

Comments

Do not use url(), l(), or other path.inc functions.

Calling (e.g.) the url() function within a hook_requirements('install') implementation results in a fatal error, if the module is installed from a profile.

Issue reported here: http://drupal.org/node/1149580

Also see Prevent path functions from using database tables that have not yet been created.

Automatic loading of .module file.

Various pages mention the automated loading of the .module file if hook_requirements is satisfied. The load occurs only for hook_install but not for hook_uninstall or hook_update_*.

Make sure you return an array no matter what

Install profiles will fail with a fatal error if your implementation of this hook does not return an array. Even if all your requirements are met, make sure that you return at least an empty array.

@see: http://drupal.org/node/1314616#comment-5483884

Login or register to post comments