3

I'm trying to add a custom stylesheet although I'm using the Garland theme but so far I wasn't able to do it.

I've tried the following:

function mymodule_init(){
    $path = drupal_get_path('module', 'mymodule');
    drupal_add_js ( $path . '/extra/script.js' ); // script is loaded and works
    drupal_add_css( $path . '/extra/style.css' ); // css isn't.
}

I also have a form that I will display when accessing /myform, so I tried to add the CSS there:

function mymodule_formname($form_state){
    drupal_add_css( drupal_get_path('module', 'mymodule') . '/extra/style.css' );
    // $form definition here
    return $form;
}

No success. Finally also tried to include the CSS using the #attached form parameter but still no results. I flush the cache for each of the attempts just to be sure...

Am I missing something? Shouldn't this be possible?

  • 1
    There is no reason that this shouldn't work. Is CSS aggregation enabled? Try to turn it off. – Berdir May 6 '11 at 10:54
  • Berdir, By CSS aggregation you mean the Optimize CSS option on admin/settings/performance? If yes, it's disabled. – acm May 6 '11 at 11:11
  • 1
    Yes. Are you sure that the path to your CSS file is correct? Can you try with a different file? Do CSS files added by other modules (search for drupal_add_css usage) show up? – Berdir May 6 '11 at 14:07
  • The path is correct. I've tried with another file (the js, which works perfectly). Changing the name will not help. And there are no other modules except for the core ones... – acm May 18 '11 at 9:37
2

You can use context and context add assets to get the job done. If you are not familiar with context it's a way to set reaction blocks, theme HTML, theme pages, menus, etc based on conditions on menu items, paths, node types, taxonomy terms, etc. You can include a custom css file on your form with context add assets.

1

Adding Module Stylesheets using drupal_add_css()

Using hook_init() to include CSS and JS files

Kind of what you're trying to do... but yeah, you should be able to do it pretty easily inside your module. If it won't be added in hook_form_alter(), try it outside of there site-wide, or using hook_init() like you tried originally (just make sure you clear all caches on the performance page).

Are you using Drupal 6 or Drupal 7?

  • D6, and I cleared caches for every different thing I tried. I'm aware I should be able to do it the way I'm trying, the point is: I'm not. :-( And that's the real question. Thanks for the effort thou' – acm Jun 8 '11 at 11:00

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.