2

I am developing a small module which is dependent on the php_ldap extension. I would like to make sure that this extension is enabled before activating the module and if it is not display a message telling the user that the ldap extension needs to be installed and enabled. I have tried using hook_enabled() for this purpose, but since this hook is fired after the module is enabled it will not work as expected:

function example_enable() {
  if (!function_exists('ldap_connect')) {
    // Here I would like to halt the activation of the module
    drupal_set_message(t('The Ldap extension needs to be enabled.'), 'error');
  }
}

I am not sure how to stop the user from enabling a module if a specific extension is not installed or loaded. I was thinking about using hook_install(), but I am not sure that would work either.

5
  • 1
    hook_requirements maybe? – Clive Mar 17 '17 at 8:09
  • @kiamlaluno - Why was the following tags removed from this question hooks, installation, dependencies? Is not the meaning to specify what the question is about. Seems very broad just to tag it as ´7? – Cyclonecode Mar 19 '17 at 9:01
  • You aren't installing Drupal, the question is not about hooks, and we don't need a new tag that has been never used before. – kiamlaluno Mar 19 '17 at 9:17
  • @kiamlaluno - I can understand the first two, but this is clearly a question about module dependencies so I think this tag is appropriate? If not perhaps you could give me some tips on tags that could/should be used. It seems very strange that the only tag sufficient would be 7? – Cyclonecode Mar 19 '17 at 10:46
  • I added back installation since that tag is not just for installing Drupal. For the other tag, since it was not created in all this time, I take we don't really need it, or it would have been created before. – kiamlaluno Mar 20 '17 at 12:53
1

The best solution for you is using hook_requirements .

function hook_requirements($phase){
       if ($phase =="install"){
         $requirements['php'] = array(
           'title' => 'LDAP extension',
           'value' => "LDAP",
          );
 if (function_exists('ldap_connect')) {
      $requirements['php']['description'] = 'YOU NEED TO install LADAP';
      $requirements['php']['severity'] = REQUIREMENT_ERROR;
     }
    }
return $requirements;

}

and another tricky solution (but NOT RECOMMENDED) is : Create Another module for php_ldap extension (let name it B), and on that module on hook_enable check the php_ldap extension is exists if not, uninstall module(uninstall B module) , and set your main module dependent on first one(I mean Module B) . with this method you will be sure if the extension Available Module B can be installed and consequence Your main Module.

Something like following snippet could help you to implement that

function MODULEB_enable() {
  if (!function_exists('ldap_connect')) {
        module_disable(array('MODULEB'));
       drupal_uninstall_modules(array('MODULEB'));
       drupal_set_message(t('The Ldap extension needs to be enabled.'), 'error');
  }
}
4
  • +1, It seems a little unnecessary to have to create another module just for this purpose, but I will check into hook_requirements() hopefully this hook is what I need. – Cyclonecode Mar 17 '17 at 8:14
  • @Cyclonecode yep, thanks for your mentioned I add details about hook_requirements – Yuseferi Mar 17 '17 at 8:15
  • The hook_requirements() was exactly what I needed =) – Cyclonecode Mar 17 '17 at 8:46
  • 1
    @Cyclonecode good, I'm happy to helping another Drupalist – Yuseferi Mar 17 '17 at 8:54

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.