1

my module uses several third part PHP libraries, loaded by using the library module supported by the x_autoload (several third part libraries are based on composer, so this support directly pointing to the composer file).

Just to provide you with an example:

function mymodule_libraries_info() {
  $libraries = array();
  ...
  $libraries['mongo-php-library'] = array(
      'name' => 'mongo-php-library',
      'vendor url' => 'https://github.com/mongodb/mongo-php-library',
      'download url' => 'https://github.com/mongodb/mongo-php-library/releases',
      'path' => 'src',
      'version arguments' => array(
          'file' => 'RELEASE-1.0',
          'pattern' => '/RELEASE (\d+\.\d+\.\d+).*/',
          'lines' => 1,
      ),
      'versions' => array("1.0.4"=>array()),
      'xautoload' => function($adapter) {
        $adapter->composerJson('composer.json');
      },
  );
  ...
  return $libraries;
}

Everithing works fine (I can use this library in my working code) except for the "uninstall" part.

In fact, I need that the hook_uninstall() can access to this library in order to perform several operations, but I receive a WD php: Error: Class 'MongoDB\Client' not found error.

I guess this is related to a specific problem: when I try to uninstall the module, it has already been disabled, so its libraries are no loger available when I call the "uninstall".

How can I deal with this problem?

2 Answers 2

0

Not knowing what "operations" you are performing with this composer lib, I suggest you might want to just think of when you actually want to perform these operations. Doing it on Uninstall seems like a bad idea. Why not utilize hook_disable, instead of hook_uninstall, which should allow you to do things just before the module gets disabled.

If you really want to do it in a hook_uninstall, then you can load your .module file using drupal_load() and call the functions in your .module file from hook_uninstall.

1
  • Thank you. That library is a MongoDB high-level driver... so it is used to create MongoDB collations when the module is being enabled, and to remove them when the module is definitively uninstalled, not just (maybe temporarely) disabled. In effect, I think the Drupal best practices expect to remove all the moodule-related data only when the module is uninstalled. In any case I'll try to use the drupal_load() into the unistall, but I'm not sure it is able to load a library defined into the mymodule_libraries_info() with composer. Commented Feb 17, 2017 at 17:49
0

For future readers I found a solution fitting my needs, and I'm here to share.

The Early-bootstrap class loading (expert only) page explain how to use the x_autoload for loading module classis when uninstalling a module.

There you can read:

Solution: Put this in your mymodule.module or mymodule.install: xautoload()->registerModule(__FILE__);

In any case it works only for module classes.

Starting from that I was able to find an interesting method to get what I desire: xautoload()->adapter->composerJson('sites/all/libraries/mongo-php-library/composer.json');

I know it is not very elegant for several reasons:

  • the library path is hardcoded -- sadly libraries_get_path(...) can't work, because the library is not declared
  • we can't be sure the x_autoload module is still enabled: it is declared as a dependency of my module, but a user can disable my module, then disable x_autoload, then try to uninstall mine

In any case this solution currently fits for me. In the future I can try to deep investigate, but for now it is a proper tradeoff.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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