You are here

function hook_requirements in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Extension/module.api.php \hook_requirements()
  2. 7 modules/system/system.api.php \hook_requirements()
  3. 9 core/lib/Drupal/Core/Extension/module.api.php \hook_requirements()

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. 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.yml file.

During installation (when $phase == 'install'), if you need to load a class from your module, you'll need to include the class file directly.

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.

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

An associative array where the keys are arbitrary but must be unique (it is suggested to use the module short name as a prefix) and the values are themselves associative arrays with the following elements:

  • 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

23 functions implement hook_requirements()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

demo_umami_requirements in core/profiles/demo_umami/demo_umami.install
Implements hook_requirements().
experimental_module_requirements_test_requirements in core/modules/system/tests/modules/experimental_module_requirements_test/experimental_module_requirements_test.install
Implements hook_requirements().
file_requirements in core/modules/file/file.install
Implements hook_requirements().
image_requirements in core/modules/image/image.install
Implements hook_requirements().
install_check_requirements in core/includes/install.core.inc
Checks installation requirements and reports any errors.

... See full list

3 invocations of hook_requirements()
drupal_check_module in core/includes/install.inc
Checks a module's requirements.
SystemManager::listRequirements in core/modules/system/src/SystemManager.php
Displays the site status report. Can also be used as a pure check.
update_check_requirements in core/includes/update.inc
Checks update requirements and reports errors and (optionally) warnings.

File

core/lib/Drupal/Core/Extension/module.api.php, line 980
Hooks related to module and update systems.

Code

function hook_requirements($phase) {
  $requirements = [];

  // Report Drupal version
  if ($phase == 'runtime') {
    $requirements['drupal'] = [
      'title' => t('Drupal'),
      'value' => \Drupal::VERSION,
      'severity' => REQUIREMENT_INFO,
    ];
  }

  // Test PHP version
  $requirements['php'] = [
    'title' => t('PHP'),
    'value' => $phase == 'runtime' ? Link::fromTextAndUrl(phpversion(), Url::fromRoute('system.php'))
      ->toString() : 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.', [
      '%version' => \Drupal::MINIMUM_PHP,
    ]);
    $requirements['php']['severity'] = REQUIREMENT_ERROR;
  }

  // Report cron status
  if ($phase == 'runtime') {
    $cron_last = \Drupal::state()
      ->get('system.cron_last');
    if (is_numeric($cron_last)) {
      $requirements['cron']['value'] = t('Last run @time ago', [
        '@time' => \Drupal::service('date.formatter')
          ->formatTimeDiffSince($cron_last),
      ]);
    }
    else {
      $requirements['cron'] = [
        '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>.', [
          ':url' => 'https://www.drupal.org/cron',
        ]),
        'severity' => REQUIREMENT_ERROR,
        'value' => t('Never run'),
      ];
    }
    $requirements['cron']['description'] .= ' ' . t('You can <a href=":cron">run cron manually</a>.', [
      ':cron' => Url::fromRoute('system.run_cron')
        ->toString(),
    ]);
    $requirements['cron']['title'] = t('Cron maintenance tasks');
  }
  return $requirements;
}