Drupal - How do I include a PHP library?

hook_library() is thought only for Javascript and CSS files; for PHP libraries, you should consider the Library API module.

This module introduces a common repository for libraries in sites/all/libraries resp. sites//libraries for contributed modules.

External libraries

Denotes libraries ("plugins") that are neither shipped nor packaged with a project on drupal.org. We do not want to host third-party libraries on drupal.org for a multitude of reasons, starting with licensing, proceeding to different release cycles, and not necessarily ending with fatal errors due to conflicts of having the same library installed in multiple versions. Drupal 7 only has built-in support for non-external libraries via hook_library(). But it is only suitable for drupal.org projects that bundle their own library; i.e., the module author is the creator and vendor of the library. Libraries API should be used for externally developed and distributed libraries. A simple example would be a third-party jQuery plugin.

The Library API module implements an API that can be used from third-party modules to load libraries installed on a Drupal site; the pro of using Library API is that it handles dependencies between libraries, and versioning.


I believe you are looking for the Libraries module.


I am not too sure if this operation is going to work for custom php libraries but I have used this procedure for importing custom css and js files into my project.

  1. Download and install the libraries module from here https://drupal.org/project/libraries and enable it (do not forget to download, install and enable all dependencies as needed)
  2. A directory sites/all/libraries should have been created. If not, just create the libraries directory in sites/all/
  3. now create your php includes director in the sites/all/libraries directory.
  4. Copy all your custom php files into the sites/all/libraries/includes directory.

In summary, I guess the libraries module should enable you to automatically import custom php, css and js libraries and so on into your project

Tags:

7