I'm writing a wrapper module for a php library, and for some reason, can't get to actually doing something. When I try to call


I get an error message:

PHP Fatal error:  Call to undefined function libraries_load()

I do have set

dependencies[] = libraries (>=2.x)

in the module_name.info file and module_name_libraries_info() seems to be okay. When I run drush libraries-list, I do get the library listed correctly.

The version of the libraries api I have installed is 7.x-2.2

How do I fix this?

  • 2
    Where is that code specifically? – Clive Jan 21 '16 at 12:45
  • I tried to call libraries_load($library_name); as first statement in the module file' or alternatively from within a function. If I try to run libraries_detect() instead I get the same error. As if the libraries module is ignored. – Ernst Jan 21 '16 at 13:00

It's very rare to need to use the Drupal API outside of a hook/callback function.

That's what's causing the problem in this case - your module file is simply being included before that of the libraries module, so the interpreter doesn't know what libraries_load() is yet.

I'd recommend moving your code into an appropriate function that gets invoked when Drupal is properly bootstrapped, and all enabled modules are loaded.

Failing that, you might get some joy out of tweaking the weight field in the system table for the modules in question. If your module has a higher weight than Libraries, you might be able to get it working. But moving the code to a more reliable place would arguably be better.

  • So what would be the reliable place? I first tried tweaking the weight of my module and setting the libraries api to bootstrap, but that did not solve the issue. – Ernst Jan 21 '16 at 14:29
  • There's no one place, it depends what you need to run that code in response to/preparation for. If you can edit the question to break down what you're trying to accomplish (just broadly speaking) I can probably give you a better answer to that – Clive Jan 21 '16 at 14:45
  • I have a really weird workaround, based on your answer above of load order. I don't know why it works and changing values in the system table did not. I changed the name of my module (and all functions within it) to start with a t instead of an f, and now it can use libraries_load(). I consider this a bug, especially since the libraries module is a dependency of my module, and as such should load before it regardless of the name of my module. – Ernst Jan 22 '16 at 7:57
  • 1
    It's a simple one - l (for libraries) is before t in the alphabet, but after f. So yeah, that should have done the trick, albeit not exactly an ideal solution. I reckon it won't be classed as a bug because you're not supposed to access functions provided by one module directly in another module file anyway. That's what hooks are for. Trust me when I say don't bother trying to fight Drupal with ideas of best practice/how it "should" be done, you'll be banging your head against a wall. Do what it wants, in the way it wants it, and you'll find developing for Drupal a lot easier – Clive Jan 22 '16 at 8:58

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.