2

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

<?php 
  drupal_add_library('system', 'ui.accordion');

Drupal no longer loading jquery itself.

4
  • 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?
    – mpdonadio
    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
3

You can add below code in your theme template.php

function NAME_OF_THEME_preprocess_page(&$variables) {
  drupal_add_library('system', 'ui.accordion');
}
4
  • 1
    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
  • 1
    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
  • 1
    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
7

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');
10
  • 1
    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
  • 1
    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
  • 1
    Flush the cache is enough...
    – Anil Sagar
    Feb 25 '14 at 14:31

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.