1

I'm writing a module that will call some external php libraries. When I was searching for a way to import the library I found the Libraries API. After reading some documentation about the module and core features I found some example code.

When I implement it in my module nothing happens. My movies.module file contains:

/**
* Implements hook_libraries_info()
**/
function movies_libraries_info()
{

  // This array key lets Libraries API search for 'sites/all/libraries/example'
  // directory, which should contain the entire, original extracted library.
  $libraries['imdb'] = array(
    // Only used in administrative UI of Libraries API.
    'name' => 'imdb', 
    'vendor url' => 'https://github.com/tboothman/imdbphp', 
    'download url' => 'https://github.com/tboothman/imdbphp/releases',
     // Default list of files of the library to load. Important: Only specify
    // third-party files belonging to the library here, not integration files of
    // your module. 
    'files' => array(
      // For PHP libraries, specify include files here, still relative to the
      // library path. 
      'php' => array(
        'bootstrap.php',
      ),
    ),
    // Optional: Register files to auto-load for your module. All files must be
    // keyed by module, and follow the syntax of the 'files' property. 
    'integration files' => array(
      'movies' => array(
        'php' => array('bootstrap.php'),
      ),
    ),
  );
  return $libraries;
}

No errors are thrown when enabling my module when the library is not installed.

How can my module load the external PHP library using the Libraries APi?

4
  1. Add a composer.json file to your movies module & name it your custom module's name:

    {
      "name": "drupal/movies",
      "type": "drupal-module",
      "license": "GPL-2.0+",
      "require": {
        "imdbphp/imdbphp": "*"
      }
    }
    
  2. Add a new line to the merge-plugin include array in the composer.json of your Drupal ROOT with your module's composer file path:

    "extra": {
      "merge-plugin": {
        "include": [
            "core/composer.json",
            "modules/custom/movies/composer.json"
        ]
      }
    }
    
  3. Enable the movies module

  4. Run composer update & composer dumpauto in Drupal ROOT. This will install & autoload the new dependencies in your vendor directory.

  5. Now you can use the classes provided by this library. In your case for example:

    $title = new \Imdb\Title(335266);
    
  • Is this the recommended method for installing a CSS library, too? – user1359 Apr 20 '18 at 18:08
-3

You can load libraries from the Libaries API in Drupal 7 in two ways:

1) You can use the function libraries_load()

2) You can use the #attached method of a render array as follows:

$form['myelement']['#attached']['libraries_load'][] = array('my_library');
  • 1
    I'm using Drupal 8 and nog Drupal 7. – JimmyD Jan 8 '17 at 10:51
  • Oh, my mistake. The D8 version doesn't even have a release yet, there's only a dev version. It won't be usable. – Jaypan Jan 8 '17 at 12:11
  • Hey @Jaypan, this answer looks like a perfect opportunity to use it for gaining this badge ... wanna try? – Pierre.Vriens Nov 10 '17 at 15:29

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.