2

My question is simple.

Is there a straightforward method to load a library e.g. ibeginshare (it contains 2 css, an images folder and 1 js) which I copied in sites/all/libraries to use it in my module?

I have created a module that implements hook_block_info and hook_block_view and I want to load this library in its hook_init.

Is it mandatory to use hook_library and define there everysingle file then call it via drupal_add_library? I am confused. What if I have dozens of js/css files ? What if I don't want necessarly to share the library with other modules?

NOTE I managed to load the file like this:

METHOD 1

function MYMODULE_init() {
  drupal_add_js('sites/all/libraries/ibeginshare/share.js');
}

I would like a more elegant way so I tried:

METHOD 2

function MYMODULE_init() {
  drupal_add_library('MYMODULE', 'ibeginshare');

}

/**
 * Implements hook_library().
 * @return string
 */
function MYMODULE_library() {
  $libraries['ibeginshare'] = array(
    'title' => 'ibeginshare',
    'website' => '',
    'version' => '1.2',
    'js' => array(
      'sites/all/libraries/ibeginshare/share.js' => array(),
    ),
    'css' => array(
      'sites/all/libraries/ibeginshare/share.css' => array(),
    ),
     'css' => array(
      'sites/all/libraries/ibeginshare/share-print.css' => array(),
    ),
  );

  return $libraries;
}

this loads all libraries but the plugin doesn't initialize. I wonder why. I think will work if I add a weight property in the hook_library but anything I tried didn't work.

1
  • Seems you want these js and css files loaded on every page, if this is the case, I would probably do it from your module's .info file using scripts[] and stylesheets[all][]. I can't help you with the HOOK_library problem though, I've never used it.
    – 2pha
    Feb 22, 2013 at 13:48

1 Answer 1

2

Assuming you are on Drupal 7, drupal_add_library() will do just that for you. From the function's API page:

Adds multiple JavaScript or CSS files at the same time.

A library defines a set of JavaScript and/or CSS files, optionally using settings, and optionally requiring another library. For example, a library can be a jQuery plugin, a JavaScript framework, or a CSS framework. This function allows modules to load a library defined/shipped by itself or a depending module, without having to add all files of the library separately. Each library is only loaded once.

http://api.drupal.org/api/drupal/includes!common.inc/function/drupal_add_library/7

Note that you first need to define and register the library with hook_library().

http://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_library/7

2
  • But as I understand drupal_add_library($module, $name) needs a module name as argument which means it needs a manual implementation of hook_library in a module where you manually type all your js/css paths. There is no automatic scanning of libraries/mylibrary folder Feb 25, 2013 at 9:14
  • That is correct - or is at least my understanding too. (You need to define the contents of the library "manually" with hook_library first). I must have missed the part of your question ("Is it mandatory to use hook_library... ?"). So a simpler answer would be, "As far as I know, yes". Feb 25, 2013 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.