hook_requirements($phase)

Check installation requirements and do status reporting. This hook must be located in .install file. Use parameter $phase to check installation requirements ($phase == ‘install’ ) and status reporting ($phase == ‘runtime’).

Parameters

$phase: There’re 2 phases to check the requirements during the installation and runtime.

‘install’: the module is being installed.
‘runtime’: the runtime requirements are being checked and shown on the status report page.

Return value

A keyed array of requirements.

  • ‘title’: the name of the requirement.
  • ‘value’: the current value (e.g. version, time, level, …). During install phase, this should only be used for version numbers, do not set it if not applicable.
  • ‘description’: description of the requirement/status.
  • ‘severity’: the requirement’s result/severity level, one of:
    • REQUIREMENT_INFO: For info only.
    • REQUIREMENT_OK: The requirement is satisfied.
    • REQUIREMENT_WARNING: The requirement failed with a warning.
    • REQUIREMENT_ERROR: The requirement failed with an error.

Usage sample:

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

    if (is_numeric($cron_last)) {
      $requirements['cron']['value'] = $t('Last run !time ago', array('!time' => format_interval(time() - $cron_last)));
    }
    else {
      $requirements['cron'] = array(
        'description' => $t('Cron has not run. It appears cron jobs have not been setup on your system. Please check the help pages for configuring cron jobs.', array('@url' => 'http://drupal.org/cron')),
        'severity' => REQUIREMENT_ERROR,
        'value' => $t('Never run'),
      );
    }

    $requirements['cron']['description'] .= ' '. t('You can run cron manually.', array('@cron' => url('admin/logs/status/run-cron')));

    $requirements['cron']['title'] = $t('Cron maintenance tasks');
  }
  
   return $requirements;
}
?>

Note: this hook must go into your .install file, in order to block the installation of the module unless requirements exist. If it’s in your .module file, only the runtime phase will execute. A lot of contributed modules get this wrong.