I have a multi-site installation using domain access module and each sub-domain has its own theme. I have a javascript file which is common to all the themes. Right now I've placed the js file inside all the themes folder. Is there a way to place the js file in a common location so that all themes can share it ? Can I use Libraries API module for this purpose ?
2 Answers
Yes, you can use the Libraries API for this. The docs explain all of this fairly well. This is best for things that change infrequently.
For my custom Javascript libraries (which I work on continuously), I make module with an instance of hook_library
function mymodule_util_library ()
{
$path = drupal_get_path("module", "mymodule_util");
$libraries = array();
$libraries["mymodule_util"] = array(
"title" => "MyModule Javascript Utilities",
"version" => "0.0",
"js" => array(
$path . '/UTIL.js' => array(),
$path . '/UTIL.HTML5.js' => array(),
$path . '/UTIL.COLOR.js' => array(),
$path . '/UTIL.MATH.js' => array(),
),
'weight' => -10,
);
return $libraries;
}
Put the module in "site/all/modules" so all of your sites can see it, then enable it on the sites you need.
Then, wherever you need these JS libraries, you use drupal_add_library
drupal_add_library("mymodule_util", "mymodule_util", $every_page);
Typically, this is in a hook_block_view
or a preprocess function.
MPD's solution is very elegant. But since I'm new to Drupal and I don't know anything about module development (yet), I used a different method. The method I used to solve this problem was :
- I placed the js file in the main theme.
- I made the remaining themes as sub-themes to the main theme.
I know this is not a very elegant solution but this is an easy solution for newbies in Drupal.