i tryed to add CSS inside my many page-*.tpl.php files. i cannot move them to templates.php, while it is possible that template_file will be detected as non existend by drupal. If i set template_file = page-test1.tpl.php and this file do not exist, i will add a wrong stylesheet to page.tpl.php (drupal fall-back) with drupal_add_css inside template.php.

i must add this CSS to page-*.tpl.php as in past! but this seems not possibly.

Comments

hass’s picture

Addition:

drupal_add_css isn't working inside _phptemplate_variables.

function _phptemplate_variables($hook, $vars = array()) {
   drupal_add_css((path_to_theme().'/css/layout_'.$filename.'.css'), 'module', 'all');
}
hass’s picture

sorry, i must correct. it is not working in a page hook !??????????????? i'm lost...

function _phptemplate_variables($hook, $vars = array()) {
  switch ($hook) {
    case 'page':
        drupal_add_css((path_to_theme() . '/css/layout_test.css'), 'module', 'all');
        break
  return $vars;
}
kkaefer’s picture

Status: Active » Closed (won't fix)

Plese read the theme update page for information on drupal_add_css(). If you want to insert a new stylesheet in your theme, you can do so by simply adding the HTML code for it after <?php print $styles; ?>. If you want to use drupal_add_css() to add your theme-specific styles, you have to recalculcate $styles by calling $styles = drupal_get_css();.

kkaefer’s picture

Status: Closed (won't fix) » Closed (works as designed)
dvessel’s picture

How about if you want to override a 'core' or 'module' style? Just adding with html won't maintain the right order unless you copy and manage it in your single "style.css" file. But even with that you could end up with core styles coming after module styles. Not really a big deal most of the time but it does break the point of proper cascading order of the new system.

For themers (like me), it's very confusing. I ran into the same issue but I worked around it by manually adding to the $css array from template.php similar to this..

function _phptemplate_variables($hook, $vars = array()) {
  switch ($hook) {
    case 'page':
      $defaultsCSS = drupal_get_path('module','system') .'/defaults.css';
        if ($vars['css']['core'][$defaultsCSS]) {
          unset($vars['css']['core'][$defaultsCSS]);
          // Manually push into the array.
          $vars['css']['core'] = path_to_theme() .'new-defaults.css';
        }
  }
  return $vars;
}

Oddly enough, if you call drupal_add_css inside $hook->node it works properly. It gets called earlier so there's something about the order of that function call preventing it from working inside "page".

If this is by design then it should be reconsidered. My head turned to knots trying to figure this out.

dvessel’s picture

Whoops, I mean this as it's a multi-dimentional array.

// get new file and manually push into array.
$newDefaults = path_to_theme() .'new-defaults.css';
$vars['css']['core'][$newDefaults] = array('path' => $newDefaults, 'media' => 'all');
kkaefer’s picture

Did you actually *read* the link I posted? $css is only used if you use it in your theme. If you don't have a drupal_get_css($css); somewhere in your theme, this is completely useless. The rendered version of that array is at $styles. So after adding a file, you have to recalculate this string.

hass’s picture

i know about this "theme update page" and it is totaly useless. i must add a style befor style.css and it should be of type 'theme' (clean and logical code!). i can workaround with type 'module' but this is awfull workaround (bad code logic), while this is not a module CSS - it is a theme CSS.

i'm not talking about deleting a style from the $styles array. i must add *some* css files in the correct position and order of type theme, nothing more. shouldn't be so complex, isn't it?

Adding this CSS by hand to the page.tpl.php won't help anything, if i add it after $styles - remember - it must be positioned befor style.css, this will result in a css class overwrite by drupal core or other modules. this IS what i MUST prevent. Finaly i must give a user the chance to change the style.css to overwrite a css framework setting without altering the css framework (what make framework updates difficult).

hass’s picture

i cannot edit my posting so i rewrite:

1. Adding this CSS by hand to the page.tpl.php won't help anything, if i add it after $styles - remember - it must be positioned befor style.css. In this case it will added after styles.css and the framework will overwrite style.css settings, but this file should be the last!

2. On the other side, if i add it befor $styles, this will result in a css class overwrite by drupal core or other modules.

This IS all what i MUST prevent. And finaly i must give a user the chance to change the style.css to overwrite a css framework setting without altering the css framework (what make framework updates difficult).

kkaefer’s picture

PHP offers functions to insert an element before another element in an array. That will do it.

dvessel’s picture

Priority: Critical » Normal
Status: Closed (works as designed) » Active

Yeah kkaefer, I did *read* that page and I am calling it through drupal_get_css($css).

Look at the last comment by me in that upgrade handbook and try running the code. It's very inconsistent.

I don't think it's critical but this the way it's supposed to work?

dvessel’s picture

@hass

You can do something like this for now.

function _phptemplate_variables($hook, $vars = array()) {
  switch ($hook) {
    case 'page':

      $layoutTest = path_to_theme() .'/css/layout_test.css;
      // Append $layoutTest to the beginning of 'theme' group.
      array_unshift($vars['css']['theme'], $layoutTest = array('path' => $layoutTest, 'media' => 'all'));
      
      break
  return $vars;
}

There should be a way to give weight to each style but that's another issue.

dvessel’s picture

Bah!!

I meant this to *prepend* to the beginning of theme group.

$layoutTest = path_to_theme() .'/css/layout_test.css;
array_unshift($vars['css']['theme'], $layoutTest = array('path' => $layoutTest, 'media' => 'all'));
heine’s picture

It is not inconsistent. PHP is not magic.
$css is a variable defined by the phptemplate engine. Subsequent calls to drupal_add_css or drupal_get_css do not magically change this $css variable.

dvessel’s picture

Fine, maybe it's not but look at it from a themer's point of view. It looks inconsistent. Just follow that section and try adding a css file the way it's shown. Then the immediate code example for unsetting a css file. When it's called through "drupal_get_css($css)" it looks like drupal_add_css() doesn't do a damn thing.

I found a workaround but that part should be made more clear if this is really the way it's intended to work.

heine’s picture

Project: Drupal core » Documentation
Version: 5.0-beta1 »
Component: base system » Developer Guide

Then this is a documentation issue.

heine’s picture

Title: drupal_add_css isn't working inside page.tpl.php » Improve drupal_*_css examples

Better title.

dvessel’s picture

Okay, a small list for anyone who can update the doc. I know it's obvious to some but we all can't be so smart.

  1. drupal_add_css() shows the added style when calling drupal_get_css() -without $css.
  2. Manually unsetting a style then you must call it with $css, e.g. drupal_get_css($css).
  3. If you must do both then it's all manual then making the call through drupal_get_css($css).
    for page.tpl.php:
    unset($css['type']['path']);
    $css['type']['newpath'] = array('path' => 'newpath', 'media' => 'all');
    print drupal_get_path($css);
    
  4. The above applies only when you try to set it inside page.tpl.php or $hook->page in _phptemplate_variables().
  5. Adding drupal_add_css() outside of $hook->page works fine inside _phptemplate_variables(). It will get called multiple times for each hook iteration but the function adds the style only once..
dvessel’s picture

Correction for #5. The function would just overwrite the same style so it's not a good idea.

hass’s picture

i don't understand why i should reorder the theme style array myself... it will be better to have this done with the drupal_add_css() together with a weight for example... then i don't need to think about how i build such a reorder function with PHP that should be implemented in core.

let me say, i don't like to put drupal_add_css inside _phptemplate_variables... i filed this problem only after i found it is not working. for the main issue, please read drupal_add_css don't have a 'weight'. i only tryed to workaround and placing it inside _phptemplate_variables hasn't worked, too. using drupal_add_css inside _phptemplate_variables creates more and more problems... :-(

hass’s picture

#10 submitted by kkaefer on November 5, 2006 - 00:21
PHP offers functions to insert an element before another element in an array. That will do it.

Why does Drupal drupal_add_css not offer me a way to insert my CSS in a higher order position then style.css? :-) why do i need to reinvent the wheel with a special function using PHP that should be part or core? remember the 'weight' feature i wrote about may do the trick.

kkaefer’s picture

@hass: if you need this kind of functionality, provide a patch in a new issue.

kkaefer’s picture

Ah, sorry, I didn't see the link.

hass’s picture

Status: Active » Closed (works as designed)
dokumori’s picture

Project: Documentation » Drupal core
Version: » 7.x-dev
Component: Developer Guide » documentation

Changed the component to reflect the new component categorization. See http://drupal.org/node/301443
-dokumori

dokumori’s picture

Project: Drupal core » Documentation
Version: 7.x-dev »
Component: documentation » Correction/Clarification