Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Am i doing wrong to define, jQuery Accordion inside my .module file, just like this:

  drupal_add_library('system', 'ui.accordion');

Drupal no longer loading jquery itself.

share|improve this question
Drupal loads Jquery by default... Jquery UI Plugins needs to be loaded separately based on requirement as you did above... – Anil Sagar Feb 25 '14 at 14:16
Where you load the library from depends on what you are trying to do. To get the best possible answer you need to provide more information on what you are trying to do. – rooby Feb 25 '14 at 16:39
Are you doing that at the top level and not inside a function / hook? – MPD Feb 25 '14 at 23:23
@MPD, now i am doing it as suggested below and it is running fine. – Sven Feb 26 '14 at 10:40
up vote 3 down vote accepted

You can add below code in your theme template.php

function NAME_OF_THEME_preprocess_page(&$variables) {
  drupal_add_library('system', 'ui.accordion');
share|improve this answer
This is also not preferred; #attached in a render array should be used if we're being strict about it :) – Clive Feb 25 '14 at 14:23
@Clive : Interested to know why it's not preferred way to do... – Anil Sagar Feb 25 '14 at 14:32
I'm not 100% (I've only seen this mentioned in issues on d.o and perhaps a question or two here), but I think mostly because of render array caching. Theoretically the preprocess method should have the same effect, but I think they're trying to standardise how css/js/libraries are added to pages by using render arrays. This is certainly more true for Drupal 8, so maybe the ethos is being backported if not the full code – Clive Feb 25 '14 at 14:36
Thank you Clive for giving more details :-) – Anil Sagar Feb 25 '14 at 14:37
Looks like this method is working better for the OP, maybe this is the better approach for adding libraries globally – Clive Feb 25 '14 at 14:56

Yes that's wrong, you need to put it inside a hook function.

Which one depends on where you're trying to use it. To add it to a render array, for example a form, you might use

$form['#attached']['library'][] = array('system', 'ui.accordion');
share|improve this answer
API page says, This hook_init is not run on cached pages. To add CSS or JS that should be present on all pages, modules should not implement this hook, but declare these files in their .info file. – Anil Sagar Feb 25 '14 at 14:18
Fair enough @AnilSagar, though I've never had a problem using that method with caching enabled (and the docs have been wrong before). I've updated the answer with the globally accepted solution – Clive Feb 25 '14 at 14:24
liked the first answer, now .. i still don't know what excatly to put in my module file. :( – Sven Feb 25 '14 at 14:27
Sorry @Roland, Anil was right to bring that up as the docs do explicitly say not to do that. hook_page_alter() might be appropriate: function MYMODULE_page_alter(&$page) { $page['#attached']['library'][] = array('system', 'ui.accordion'); }. It'll need a cache clear to work – Clive Feb 25 '14 at 14:28
Flush the cache is enough... – Anil Sagar Feb 25 '14 at 14:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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