0

I've installed the Libraries module. How can I add a 3rd party PHP library in my Drupal 7 project. How do I use hook_libraries_info()?

0

The Libraries project page has good documentation on how to use version 2.x API. Essentially, there are 2 components to it.

You're defining an implementation of hook_libraries_info(), that returns an array that defines the library's machine name, resources to load, & a means to validate the library version (either an version arguments element that defines the array passed to libraries_get_version() or a custom version callback function).

/**
 * Implements hook_libraries_info().
 *
 */
function MYMODULE_libraries_info() {

  // Define 'simple' as the machine name of library 
  // which will be stored in sites/all/libraries/simple
  $libraries['simple'] = array(

    // This gets passed to the $options array in libraries_get_version()
    'version arguments' => array(
      'file' => 'simple.js', 
      'pattern' => '/Version (\d+)/', 
      'lines' => 5,
    ),

    // Alternatively, MYMODULE_version_callback could be defined.
    // this function returns TRUE/FALSE if it finds a valid version of the library
    //'version callback' => 'MYMODULE_version_callback',

    // Defines the PHP,JS,CSS files to load for the library
    'files' => array(
      'js' => array('simple.js'), //this can be a path to the file location like array('lib/simple.js')
    ),
  );

  return $libraries;
}

The 2nd component is to load the library using libraries_load() wherever you need in your module.

//Load the 'simple' library
libraries_load('simple');

See the linked documentation for more information.

  • Ok Thank you very much. Can yo please help me on my following query also_ I have a custom module and I need to call a method, that I have written in the library from the module. How to do this? Is hook_libraries_info() in needed in this case in my module? Can I put a dependency in my module .info file so that while enabling the module its ask to add the library in sites/all/library folder? – Nabajit Roy Apr 29 '16 at 16:44
  • Read the linked documents. The 2.x API docs describe functions that you can use to test the status of the library so you can prompt the user if it's not there (or it failed to load). – Shawn Conn Apr 29 '16 at 16:54
  • I added the following code in my module as you said above but its not loading the library nor its showing any errors. Please help if (($library = libraries_detect($name)) && !empty($library['installed'])) { // The library is installed. Awesome! } else { // Something went wrong. :( // This contains a short status code of what went wrong, such as 'not found'. $error = $library['error']; // This contains a detailed (localized) error message. $error_message = $library['error message']; } – Nabajit Roy Apr 29 '16 at 18:07
  • 1
    Posting code snippets isn't very helpful here. My only guess is it's not detecting the library; it's likely libraries_get_version() is not matching your files. Try debugging that or defining your own version callback as the code comments above hint at. – Shawn Conn Apr 30 '16 at 20:28
  • Thanks Shawn. You guess is correct. It was libraries_get_version(), which was creating the issue. – Nabajit Roy May 1 '16 at 6:02

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.