[documentation] [Documentation bug] Improve drupal_*_css examples
dvessel
drupal-docs at drupal.org
Sun Nov 5 03:50:17 UTC 2006
Issue status update for
http://drupal.org/node/92876
Post a follow up:
http://drupal.org/project/comments/add/92876
Project: Documentation
Version: <none>
Component: Developer Guide
Category: bug reports
Priority: normal
Assigned to: Anonymous
Reported by: hass
Updated by: dvessel
Status: active
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.
* drupal_add_css() shows the added style when calling drupal_get_css()
/-without $css./
* Manually unsetting a style then you must call it with $css, e.g.
drupal_get_css($css).
* If you must do both then it's all manual then making the call through
drupal_get_css($css).
for page.tpl.php:
<?php
unset($css['type']['path']);
$css['type']['newpath'] = array('path' => 'newpath', 'media' => 'all');
print drupal_get_path($css);
?>
* The above applies only when you try to set it inside page.tpl.php or
$hook->page in _phptemplate_variables().
* 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
Previous comments:
------------------------------------------------------------------------
Sat, 04 Nov 2006 11:37:36 +0000 : hass
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.
------------------------------------------------------------------------
Sat, 04 Nov 2006 12:29:58 +0000 : hass
Addition:
drupal_add_css isn't working inside _phptemplate_variables.
<?php
function _phptemplate_variables($hook, $vars = array()) {
drupal_add_css((path_to_theme().'/css/layout_'.$filename.'.css'), 'module', 'all');
}
?>
------------------------------------------------------------------------
Sat, 04 Nov 2006 12:33:36 +0000 : hass
sorry, i must correct. it is not working in a page hook !???????????????
i'm lost...
<?php
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;
}
?>
------------------------------------------------------------------------
Sat, 04 Nov 2006 15:25:21 +0000 : kkaefer
Plese read the theme update page [1] 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();.
[1] http://drupal.org/node/64292#drupal-add-css
------------------------------------------------------------------------
Sat, 04 Nov 2006 16:22:42 +0000 : dvessel
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..
<?php
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.
------------------------------------------------------------------------
Sat, 04 Nov 2006 16:28:30 +0000 : dvessel
Whoops, I mean this as it's a multi-dimentional array.
<?php
// get new file and manually push into array.
$newDefaults = path_to_theme() .'new-defaults.css';
$vars['css']['core'][$newDefaults] = array('path' => $newDefaults, 'media' => 'all');
?>
------------------------------------------------------------------------
Sat, 04 Nov 2006 16:45:49 +0000 : kkaefer
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.
------------------------------------------------------------------------
Sat, 04 Nov 2006 22:02:45 +0000 : hass
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).
------------------------------------------------------------------------
Sat, 04 Nov 2006 22:07:54 +0000 : hass
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).
------------------------------------------------------------------------
Sat, 04 Nov 2006 22:21:44 +0000 : kkaefer
PHP offers functions to insert an element before another element in an
array. That will do it.
------------------------------------------------------------------------
Sun, 05 Nov 2006 01:00:18 +0000 : dvessel
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?
------------------------------------------------------------------------
Sun, 05 Nov 2006 01:23:28 +0000 : dvessel
@hass
You can do something like this for now.
<?php
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.
------------------------------------------------------------------------
Sun, 05 Nov 2006 01:26:49 +0000 : dvessel
Bah!!
I meant this to *prepend* to the beginning of theme group.
<?php
$layoutTest = path_to_theme() .'/css/layout_test.css;
array_unshift($vars['css']['theme'], $layoutTest = array('path' => $layoutTest, 'media' => 'all'));
?>
------------------------------------------------------------------------
Sun, 05 Nov 2006 01:28:13 +0000 : Heine
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.
------------------------------------------------------------------------
Sun, 05 Nov 2006 01:45:53 +0000 : dvessel
Fine, maybe it's not but look at it from a themer's point of view. It
/looks/ inconsistent. Just follow that section [2] 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.
[2] http://drupal.org//node/64292%2523drupal-add-css
------------------------------------------------------------------------
Sun, 05 Nov 2006 01:55:56 +0000 : Heine
Then this is a documentation issue.
------------------------------------------------------------------------
Sun, 05 Nov 2006 01:57:18 +0000 : Heine
Better title.
More information about the documentation
mailing list