Question

I'm writing a .info file for a module, and I want to include a script which has been installed in /sites/all/libraries:

So I have included the line below in my .info file:

scripts[] = /sites/all/libraries/zeroclipboard-2.2.0/dist/ZeroClipboard.js

However, with or without the leading slash on this path, I always get the same result: Drupal treats this as relative to the module directory and tries and fails to load

/sites/all/modules/custom/clipboard//sites/all/libraries/zeroclipboard-2.2.0/dist/ZeroClipboard.js?nsnrjh

So maybe I've answered my own question, but I can't find any definitive answer to this in the Drupal docs?

Thanks all,

MB

Was it helpful?

Solution

_system_rebuild_module_data is responsible for that. There is a whole section in code just to make sure:

// Prefix stylesheets and scripts with module path.
$path = dirname($module->uri);
if (isset($module->info ['stylesheets'])) {
  $module->info ['stylesheets'] = _system_info_add_path($module->info ['stylesheets'], $path);
}
if (isset($module->info ['scripts'])) {
  $module->info ['scripts'] = _system_info_add_path($module->info ['scripts'], $path);
}

My guess is that module should never assume that something exists if module itself does not install it. It's a way to introduce errors. Also, allowing modules to reach outside their directory might be a security breach.

If you need a script you can't include in your module, use Libraries API. Of course there are other ways, but most of them is problematic, and fixing all problems would pretty much result in cloning this ready module anyway.

OTHER TIPS

If you are creating custom module then you can use these function to add library or js file from lirary.

$info = libraries_load('ui.tabs');
drupal_add_library('system', 'ui.tabs');
Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top