Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drupal_add_css not working

I need to use drupal_add_css to call stylesheets onto single Drupal 6 pages. I don't want to edit the main theme stylesheet as there will be a set of individual pages which all need completely new styles - the main sheet would be massive if i put it all in there.

My solution was to edit the page in PHP editor mode and do this:

<?php 
drupal_add_css("/styles/file1.css", "theme");
?>

<div id="newPageContent">stuff here in html</div>

But when I view source, there is nothing there! Not even a broken CSS link or anything, it's just refusing to add the CSS sheet to the CSS package put into the page head.

Variations don't seem to work either:

drupal_add_css($path = '/styles/file1.css', $type = 'module', $media = 'all', $preprocess = TRUE)

My template header looks like this, I've not changed anything from the default other than adding a custom JavaScript.

<head>
    <?php print $head ?>
    <title><?php print $head_title ?></title>
    <?php print $styles ?>
    <?php print $scripts ?>
    <script type="text/javascript" src="<?php print base_path() ?>misc/askme.js"></script>    
    <!--[if lt IE 7]>
      <?php print phptemplate_get_ie_styles(); ?>
    <![endif]-->
</head>

Why is this function not working?

like image 254
MrFidge Avatar asked Sep 11 '09 12:09

MrFidge


5 Answers

It is not quite clear where you are selecting the template that you have in your example. If you are selecting it from a module then you can just use drupal_add_css in the module rather than the template.

If you have your own theme you can use template_preprocess_page and put logic in there to add the relevant CSS (you can also use it to select the template to use).

like image 75
Jeremy French Avatar answered Nov 11 '22 21:11

Jeremy French


I have noticed something weird and it might fix your problem:

drupal_add_css( drupal_get_path('theme','themname') . '/working.css','module' ,'all' , false );

drupal_add_css( drupal_get_path('theme','themname') . '/path/to/folder/notworking.css','module' ,'all' , false );

The first one will work ebcause the style it in the main them folder The second line will not work because the style is in a sub folder !

Edit:

i think it did not work because i did not write the path the the style file properly :S so please disregard my answer

drupal_add_css( drupal_get_path('theme','test') . '/pages/subpage/style.css','theme');

is working

like image 25
Christophe Avatar answered Nov 11 '22 21:11

Christophe


This function wont work in templates. The reason is that the variable $styles which will hold all the stylesheet html will already have been generated at this point, so drupal_add_css wont work as it adds to that. if you want to do this in your theme, you would probably have to add the css file manually

<link rel="stylesheet" ... />

The other way would be to use drupal_add_css in a module, but you might have a hard time adding the correct css files on the pages you want.

like image 31
googletorp Avatar answered Nov 11 '22 22:11

googletorp


It's possible to use drupal_add_css() inside your template.php file; this page has a good example of how to do just that.

like image 32
neoncube Avatar answered Nov 11 '22 21:11

neoncube


Thanks for the link, wyrmmage. That's very useful. I think the rest of the code in the page is unneccessary. You probably just need these since drupal 6 already automatically check for file existence:

drupal_add_css(path_to_theme() . '/css/yourcss.css', 'theme');
// Add the following to regenerate $styles. 
// This is needed for template_preprocess_page() since css is already generated at this point.
$variables['styles'] = drupal_get_css(); 
like image 2
VictoriaChan Avatar answered Nov 11 '22 21:11

VictoriaChan