1

How can I check if the library, which is required by my module, is present while the module is getting installed?

I have used hook_requirements with the libraries_detect function with REQUIREMENT_ERROR severity. This restricts the module from getting installed irrespective of the library is present of not.

Here is my code:


function hook_requirements($phase) {
  $requirements = array();
  $t = get_t();
  if ($phase == 'install') {
    if (function_exists("libraries_detect") && !libraries_detect('external-lib-api')) {
      $requirements['library_api'] = array(
        'severity' => REQUIREMENT_ERROR,
        'description' => $t(
          'The required Libarary is not installed.'
        )
      );
    }
  }

  return $requirements;
}

Am I doing something wrong in this? Is there any other way of doing it?

4

this is one of the working example from my module. I hope this helps you

function sfs_requirements($phase) {
  $requirements = array();
  $t = get_t();
  if ($phase == 'install' && function_exists('libraries_get_path')) {
     $plupload_path = libraries_get_path('plupload');
     $chosen_path = libraries_get_path('chosen');
     if (!$plupload_path) {
       $requirements['plupload'] = array(
         'severity' => REQUIREMENT_ERROR,
         'description' => $t('Secure File Share module requires !plupload, which is missing. Download and extract the entire contents of the archive into the %path directory on your server.',  array('!plupload' => l($t('Plupload Library'), 'https://github.com/moxiecode/plupload/archive/v1.5.8.zip'), '%path' => 'sites/all/libraries/plupload')), 
       );
     }
     if (!$chosen_path) {
        $requirements['chosen_js'] = array(
          'severity' => REQUIREMENT_ERROR,
          'description' => $t('Secure File Share module requires !chosen, which is missing. Download and extract the entire contents of the archive into the %path directory on your server.', array('!chosen' => l($t('Chosen Library'), 'https://github.com/harvesthq/chosen/releases/download/v1.3.0/chosen_v1.3.0.zip'), '%path' => 'sites/all/libraries/chosen')),
        );
      }
  }
  return $requirements;
}

Karthik

| improve this answer | |
0

You should change

hook_requirements($phase)  

into this

 Name_of_your_module_requirements($phase)

"library_api" is not a Library is a module https://www.drupal.org/project/libraries

for create a module dependency you can add a line into your module .info file like this :

dependencies[] = libraries  
| improve this answer | |
0

You may want to look at how module Forena is handling this, around line 429 of forena.module:

function forena_library_file($library) {
  $libraries = array(
      'dataTables' => 'dataTables/media/js/jquery.dataTables.min.js',
      'mpdf' => 'mpdf/mpdf.php',
      'SVGGraph' => 'SVGGraph/SVGGraph.php',
      'prince' => 'prince/prince.php'
  );
  $path = isset($libraries[$library]) && file_exists('sites/all/libraries/' . $libraries[$library]) ? 'sites/all/libraries/' . $libraries[$library] : '';
  return $path;
}

FYI: Forena interfaces with up to 4 libraries, which is why you'll 4 such entries around there.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.