Closed (works as designed)
Project:
Documentation
Component:
Correction/Clarification
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
4 Nov 2006 at 11:37 UTC
Updated:
31 Aug 2008 at 09:14 UTC
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
Comment #1
hass commentedAddition:
drupal_add_css isn't working inside _phptemplate_variables.
Comment #2
hass commentedsorry, i must correct. it is not working in a page hook !??????????????? i'm lost...
Comment #3
kkaefer commentedPlese 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 usedrupal_add_css()to add your theme-specific styles, you have to recalculcate$stylesby calling$styles = drupal_get_css();.Comment #4
kkaefer commentedComment #5
dvessel commentedHow 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..
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.
Comment #6
dvessel commentedWhoops, I mean this as it's a multi-dimentional array.
Comment #7
kkaefer commentedDid 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.
Comment #8
hass commentedi 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).
Comment #9
hass commentedi 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).
Comment #10
kkaefer commentedPHP offers functions to insert an element before another element in an array. That will do it.
Comment #11
dvessel commentedYeah 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?
Comment #12
dvessel commented@hass
You can do something like this for now.
There should be a way to give weight to each style but that's another issue.
Comment #13
dvessel commentedBah!!
I meant this to *prepend* to the beginning of theme group.
Comment #14
heine commentedIt 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.
Comment #15
dvessel commentedFine, 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.
Comment #16
heine commentedThen this is a documentation issue.
Comment #17
heine commentedBetter title.
Comment #18
dvessel commentedOkay, 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.
for page.tpl.php:
Comment #19
dvessel commentedCorrection for #5. The function would just overwrite the same style so it's not a good idea.
Comment #20
hass commentedi 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... :-(
Comment #21
hass commentedWhy does Drupal
drupal_add_cssnot 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.Comment #22
kkaefer commented@hass: if you need this kind of functionality, provide a patch in a new issue.
Comment #23
kkaefer commentedAh, sorry, I didn't see the link.
Comment #24
hass commentedComment #25
dokumori commentedChanged the component to reflect the new component categorization. See http://drupal.org/node/301443
-dokumori
Comment #26
dokumori commented