2

I have a custom almost empty module where I'm tring to insert a js file using hook_libraries_info(). But when I load the home I don't see my script inserted. It works when I use a simple drupal_add_js.
What I'm doing wrong? I've seen other examples like masonry and nivo-slider and those are similar. Do I need to init somehow? I'm new at module development, so probably is something simple.

DETAILS
Libraries module is enabled:
- Libraries 7.x-2.1

The location of the library folder is this:
- sites/all/libraries/test/test.js

test.module

<?php
//drupal_add_js('sites/all/libraries/test/test.js'); when uncommented it works

/**
 * Implements hook_libraries_info().
 */
function test_libraries_info() {  // it never works
  $libraries['test'] = array(
    'name' => 'Test', 
    'vendor url' => 'http://www.example.com/', 
    'download url' => 'http://www.example.com/', 
    'files' => array(
      'js' => array(
        'test.js',
      ),
    ), 

  );
  return $libraries;
}
5

hook_libraries_info(), documented in libraries.api.php, is just a hook that informs the Libraries API module of any existing external library. Its purpose is not loading those libraries when a Drupal page is shown; for that, the module needs to call libraries_load() (defined in libraries.module) when necessary.

libraries_load_files() is the function that actually load in the page the CSS and Javascript files, respectively with drupal_add_css() and drupal_add_js().

  // Load both the JavaScript and the CSS files.
  // The parameters for drupal_add_js() and drupal_add_css() require special
  // handling.
  // @see drupal_process_attached()
  foreach (array('js', 'css') as $type) {
    if (!empty($library['files'][$type])) {
      foreach ($library['files'][$type] as $data => $options) {
        // If the value is not an array, it's a filename and passed as first
        // (and only) argument.
        if (!is_array($options)) {
          $data = $options;
          $options = array();
        }
        // In some cases, the first parameter ($data) is an array. Arrays can't
        // be passed as keys in PHP, so we have to get $data from the value
        // array.
        if (is_numeric($data)) {
          $data = $options['data'];
          unset($options['data']);
        }
        // Prepend the library path to the file name.
        $data = "$path/$data";
        // Apply the default group if the group isn't explicitly given.
        if (!isset($options['group'])) {
          $options['group'] = ($type == 'js') ? JS_DEFAULT : CSS_DEFAULT;
        }
        call_user_func('drupal_add_' . $type, $data, $options);
        $count++;
      }
    }
  }
0
2

You are missing the version part which is necessary:

version arguments' => array(
      'file' => 'readme.txt',
      // Best practice: Document the actual version strings for later reference.
      // 1.x: Version 1.0 
      'pattern' => '/Version (\d+)/', 
      'lines' => 5,
    ), 

http://drupalcontrib.org/api/drupal/contributions!libraries!libraries.api.php/function/hook_libraries_info/7

1
  • The link the dead
    – mbomb007
    Jan 24 '19 at 20:43

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.