Use hook_requirements to help admins find and install 3rd party code

A handy snippet for module developers that use external libraries. Drupal discourages inclusion of such libraries in modules, for good reasons. This, however, makes is hard for admins to install your module. I used hook_requirements to point out how, why and where a certain library can be found.

screeenshot of the help
The admin is then helped with usefull messages: how to fix the problem.

* Implementation of hook_requirements.
function belbios_requirements($phase) {
  $requirements = array();

  if ($phase == 'runtime') {
    //Check for availability of include
    $path = drupal_get_path('module', 'belbios'). '/lib/phpbelbios/';
    if (!file_exists($path)) {
      $requirements['belbios_library']['title'] = t('Belbios Library');
      $requirements['belbios_library']['value'] = $path;
      $requirements['belbios_library']['severity'] = REQUIREMENT_ERROR;
      $requirements['belbios_library']['description'] = t('The file is not in the place where it was expected to be.<br/>It should have been at %filepath.<br/>Please download it from !download_url, or include it in your SVN, including in SVN is explained in this !external_manual',
        '%filepath' => $path,
        '!download_url' => l(t('sourceforge'), ''),
        '!external_manual' => l(t('SVN externals HOWTO'), '')
    else {
      $requirements['belbios_library']['title'] = t('Belbios Library');
      $requirements['belbios_library']['value'] = t('Library found at @filepath', array('@filepath' => $path));
    return $requirements;

Note: code is Drupal 5

And yes, in an ideal world we would include an automatical-includer in our hook_install. But that is a lot of work. And, IMHO this hould not be handled by a CMS, but by your development framework; in this case I’d say that SVN should handle such inclusions. However, that is a new discussion. For now, I think a snippet like above will help many admins, if you add it to your module.

About the author: Bèr Kessels is an experienced webdeveloper with a great passion for technology and Open Source. A golden combination to implement that technology in a good and efficient way. Follow @berkes on Mastodon. Or read more about Bèr.