Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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.

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 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 at 16:39
    
Are you doing that at the top level and not inside a function / hook? –  MPD Feb 25 at 23:23
    
@MPD, now i am doing it as suggested below and it is running fine. –  Sven Feb 26 at 10:40
add comment

2 Answers 2

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
1  
This is also not preferred; #attached in a render array should be used if we're being strict about it :) –  Clive Feb 25 at 14:23
    
@Clive : Interested to know why it's not preferred way to do... –  Anil Sagar Feb 25 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 at 14:36
    
Thank you Clive for giving more details :-) –  Anil Sagar Feb 25 at 14:37
1  
Looks like this method is working better for the OP, maybe this is the better approach for adding libraries globally –  Clive Feb 25 at 14:56
add comment

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
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 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 at 14:24
    
liked the first answer, now .. i still don't know what excatly to put in my module file. :( –  Sven Feb 25 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 at 14:28
1  
Flush the cache is enough... –  Anil Sagar Feb 25 at 14:31
show 5 more comments

Your Answer

 
discard

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.