I'm new to Drupal and don't really understand how to set up drupal_add_library.

I've made a simple module that links out to a .js file. However, this .js file depends on a couple of generic functions. I've put this generic functions in a library and want to call them.

I've set up my code as such:

function products_preprocess_page(&$variables) {
  drupal_add_library($module, $name);
  drupal_add_js(drupal_get_path('module', 'products') . '/assets/myScript.js');

But am not sure what to put in $module and $name (I'm assuming name is the actual name of the .js you want to call from the library), or even if this is the right place to call it.

Can anyone point me in the right direction?


You can call it in that hook if you want it on every page (or add some logic to restrict the pages that it loads on), that's fine.

As per the API:

$module: The name of the module that registered the library.

$name: The name of the library to add.

If you're creating your own library, Jimmy Ko is correct: you need to declare it in hook_library.

Note that $name should exclude the file extension (e.g. .js)

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.