7

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MrFidge
  • 2,107
  • 11
  • 40
  • 63

9 Answers9

7

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).

Jeremy French
  • 11,707
  • 6
  • 46
  • 71
  • Hiya, I'm really sorry but I absolutely did have this working straight from a page the other week. I thought "oh great thats easy" and built the rest of my site. Now I go back to implement it and it's not working. Typical :P – MrFidge Sep 11 '09 at 13:46
  • 8
    Another note to this, if you use template_preprocess_page with drupal_add_css() make sure you add force it into the styles array like so: `drupal_add_css(path_to_theme(). "/css/page.css", "theme"); $vars['styles'] = drupal_get_css();` – joelpittet Mar 16 '11 at 19:30
6

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

Christophe
  • 207
  • 3
  • 2
4

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.

googletorp
  • 33,075
  • 15
  • 67
  • 82
  • really? I had this working a week ago on another site :o - I'd edited drupal_add_css into a page in PHP mode. – MrFidge Sep 11 '09 at 13:13
  • I think you are right as to why the function won't work but wrong that it can't be done in the theming layer. See my answer. – Jeremy French Sep 11 '09 at 13:26
  • @Jeremy I was a bit uncertain if you could or couldn't get it to work that way, that's why I said probably. But using preprocess_page wont help much if you want to do it per page. You might as well do it in a module then. – googletorp Sep 12 '09 at 14:48
  • I would add that the function can be made to work in _some_ templates and preprocess functions. What it won't work in is the page.tpl.php or hook_preprocess_page, because by then you're too late: the $styles variable has already been compiled by phptemplate_preprocess_page() (NB D6 terminology; D7, probably html.tpl.php and phptemplate_preprocess_html().) – J-P Sep 05 '12 at 08:55
3

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.

neoncube
  • 61
  • 3
2

Answer was very much to use the CSS Injector module - great little addon!

Here is an excerpt from its project page:

Allows administrators to inject CSS into the page output based on configurable rules. It's useful for adding simple CSS tweaks without modifying a site's official theme. The CSS is added using Drupal's standard drupal_add_css() function and respects page caching, etc. The 2.x brach leverages CTools so CSS can be included with Feature changes (ie. CSS that improves the look of a View can be packaged with the View).

Pierre.Vriens
  • 2,117
  • 75
  • 29
  • 42
MrFidge
  • 2,107
  • 11
  • 40
  • 63
2

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(); 
VictoriaChan
  • 370
  • 2
  • 5
1

This code inside template.php works for me:

function alagna_preprocess_page(&$vars) {
        drupal_add_css(path_to_theme() . '/header_1.css', 'theme');
        $vars['css'] = drupal_add_css();
        $vars['styles'] = drupal_get_css();
} 

explained:

  1. alagna is the theme name
  2. header_1.css is the css file required.
Julien Roncaglia
  • 17,397
  • 4
  • 57
  • 75
heaptree
  • 11
  • 1
1

drupal_add_css is expecting a path relative to base path whereas drupal_get_path does not return the path relative to base path.

global $base_path;
drupal_add_css($base_path . drupal_get_path('module / theme','name') . "/styles/file1.css", "module / theme");

You can choose between module and theme accordingly.

junedkazi
  • 519
  • 7
  • 18
0

Try this common.inc drupal_get_css($css = NULL)

Parameters

$css: (optional) An array of CSS files. If no array is provided, the default stylesheets array is used instead.

    $css    =   $vars['css'];
// unset the system css files
$unset_css = array
    'modules/system/system.css',
    'modules/system/system-menus.css',
);

foreach($unset_css as $css_f) {
    if(isset($css['all']['module'][$css_f])) {
        unset($css['all']['module'][$css_f]);
    }
}

// add css
$css['all']['theme'][drupal_get_path('theme', 'openpublish_theme') . '/css/style.css'] = true;
$vars['styles'] = drupal_get_css($css);
Peniel1127
  • 73
  • 7