3 Tips For Using External Libraries With Drupal

There are more and more cases to use external libraries with Drupal. They may be jQuery plugins, CSS libraries, or PHP libraries already providing some form or function we want to use. But, so often we can’t include then on drupal.org because of policy issues or there may be multiple modules wanting to use the same libraries which can cause collisions. Here are 3 tips for easily working with outside libraries.

Use Libraries Module

The libraries module provides a missing link in Drupal. Modules go in the modules folder. Themes go in the themes folder. With the libraries modules libraries go in the libraries folder alongside the modules and themes folders.

That means there are several places you can place libraries and they will work. If you want to place a library in a profile it can be at:


Or, if you want a library available to all sites in a multi-site install you can place it in:


If you want a specific version of a library for a specific site it can be placed in:


This works just like modules and themes.

To get the path to the library the function to use is libraries_get_path.

$path = libraries_get_path('library_name');


hook_requirements can check to see if a library is available and even report the version.

The example below is a simple example to see if a library is installed. It will check when a user visits the status page and when the module is being installed.

function example_requirements($phase) {
  $requirements = array();
  // Ensure translations do not break at install time
  $t = get_t();

  $requirements['example'] = array(
    'title' => $t('Example Library'),

  $libraries = libraries_get_libraries();
  if (isset($libraries['example'])) {
    $requirements['example']['value'] = $t('Installed');
    $requirements['example']['severity'] = REQUIREMENT_OK;
  else {
    $requirements['example']['value'] = $t('Not Installed');
    $requirements['example']['severity'] = REQUIREMENT_ERROR;
    $requirements['example']['description'] = $t('Please install the example library %url.', array('%url' => 'http://example.com'));

  return $requirements;

An alternate example that reports the version and only checks at runtime can be see in the Zend module.

There are several different ways to take advantage of this function. Using it can help the user make sure everything is present and the sate it is in.

Autoload Classes

Autoloading classes and interfaces is common in PHP development. Most frameworks have something built in to aide with autoloading. When building Drupal modules it is a good idea to use autoloading as well. The simplest way is to take advantage of the autoload module.

The autoload module provides hook_autoload_info() to tell Drupal where classes and interfaces are. This is a registry hook to collect information for the build in PHP spl_autoloader. An example, working with the libraries module could look something like:

function example_autoload_info() {
  $path = libraries_get_path('example');
  return array(
    'exampleClass' => array(
      'file' => 'exampleClass.php',
      'file path' => $path,
    'exampleInterface' => array(
      'file' => 'exampleInterface.php',
      'file path' => $path,

The ‘file path’ property is optional. If it is not included the autoload module will assume the folder the module is in as the patch.