Adding CSS with Drupal right now is not straighforward and it's equally hard to override and force our CSS files as well (I don't want drupal.css nor event.css to load for instance).
So here's a patch to clean this up and make it super straightforward.
Modules simply use drupal_add_css('mycss.css', 'module'); to add their CSS. It's processed in correct order so that core CSS always comes first, then module CSS, then theme CSS. Right now to ensure this requires some crazy uses of theme_style_import() and what not. Hard to use and hard to explain how to use. This patch fixes that.
Not only that, but it allows for cool tricks like this:
$css['core']['misc/drupal.css'] = '';
print drupal_get_css($css);
//print $styles
Instead of using the $styles var with *all* styles I load the new $css var, take out drupal.css file and then output it myself. Voila, no drupal.css. Makes it really easy to get rid of all *core* or all *module* CSS in one fell swoop too.
Too handy not to go in, this is the first step towards making theming a lot easier: super easy CSS and overridable CSS support.
Comment | File | Size | Author |
---|---|---|---|
#44 | css_13.patch | 8.2 KB | kkaefer |
#42 | css_12.patch | 8.02 KB | kkaefer |
#41 | css_11.patch | 7.92 KB | kkaefer |
#39 | css_10.patch | 7.04 KB | kkaefer |
#38 | css_9.patch | 5.5 KB | rkerr |
Comments
Comment #1
Crell CreditAttribution: Crell commentedOh dear. Looks like you and I are tackling the same problem at the same time in two entirely different ways. :-)
http://drupal.org/node/73949
Comment #2
m3avrck CreditAttribution: m3avrck commentedCrell, your patch seems to address the issue slightly, but isn't as comprehensive. It still doesn't fix the larger problem of overriding CSS and make sure the CSS files are loaded in a proper, overridable way. However, it is a nice patch for 4.7, but for the next release, I think an entirely new approach is a much better alternative.
Comment #3
Crell CreditAttribution: Crell commentedHow does it not allow for overriding? The patch I submitted allows a theme author to override a CSS file for a given module with no PHP code needed. As I recall, there was talk of using the exact same method for template files (split all theme files out to template files, then copy/paste/modify to create a theme).
Comment #4
eaton CreditAttribution: eaton commentedAfter looking it over, this appears to be pretty flexible.
Am I correct in assuming that the real benefit here is consistency in function naming? IE, we don't have modules calling 'invisible' theme functions to do something that's not output related? Other than that, it seems to be a different way of doing what can be done now...
Comment #5
m3avrck CreditAttribution: m3avrck commentedYes, the biggest benefit is clear and consistent behavior, plus a simpler function call. It also solves the CSS mess.
Not only that, but it stores the list of CSS files in an array for easy manipulation.
Win-win for everyone :-)
Comment #6
eaton CreditAttribution: eaton commentedI think you're right; maintaining things in a list of properly organized CSS files is a win, and moves things closer to the emerging Drupal standard: 'store stuff in structured arrays until the very last moment necessary.'
If I understand correctly, *previously* themes would override theme_get_style() and pull out stylesheets they didn't want included. With this patch, they would (in template.php, or on a page-by-page basis in page.tpl.php) just reach into the variables collection and do whatever they like to $css['core'][] or $css['module'][] arrays. Then, when the styles are output, those changes are reflected.
Is that correct? I think the new way makes more sense and avoids the strange 'use this theme function even though it doesn't output anything' effect. My only question is whether the benefits outweigh 'yet another task for module developers in their 4.7->4.8/5.0 conversion list.'
I lean towards yes.
Comment #7
rkerr CreditAttribution: rkerr commentedWow, I didn't realize something as simple as adding a new CSS file was currently so complicated. This approach looks to be much more straight forward.
Comment #8
m3avrck CreditAttribution: m3avrck commentedJeff, you are correct, in page.tpl.php to get rid of drupal.css, all you need is:
Comment #9
kkaefer CreditAttribution: kkaefer commented*subscribe*
I am all for this.
Comment #10
kkaefer CreditAttribution: kkaefer commentedI tested the patch. It works. +1 for this.
Comment #11
eaton CreditAttribution: eaton commentedI dare to set this rtbc. It's a small change but one that can remove a lot of confusion and inconsistency.
Comment #12
Anonymous (not verified) CreditAttribution: Anonymous commentedw00t mr ted has done it again!
+154
:P
Comment #13
kkaefer CreditAttribution: kkaefer commentedThere is still an issue: theme_stylesheet_import() is called *before* any theme can override it, thus making the override function worthless. The creation of the HTML code should take place in drupal_get_css() rather than in drupal_add_css().
Comment #14
m3avrck CreditAttribution: m3avrck commentedtimcn, good catch. Updated patch to fix this, with even more flexibility as the array doesn't store the themed() CSS import (so easy to swap names of CSS files if need be).
Comment #15
merlinofchaos CreditAttribution: merlinofchaos commentedThis is awesome. +1.
Comment #16
rkerr CreditAttribution: rkerr commentedRock on \m/ +1
Comment #17
m3avrck CreditAttribution: m3avrck commentedFixed missing $media variable and new 'module' default.
Comment #18
m3avrck CreditAttribution: m3avrck commentedForgot media again, good to go now.
Comment #19
m3avrck CreditAttribution: m3avrck commentedMissing theme arg and cleaned up foreach loop.
Comment #20
m3avrck CreditAttribution: m3avrck commentedLast one I swear :-)
Comment #21
m3avrck CreditAttribution: m3avrck commentedSorry I lied :-P
Comment #22
m3avrck CreditAttribution: m3avrck commentedThis is not my day.
Comment #23
eaton CreditAttribution: eaton commentedI'm so never setting anything RTBC again. ;)
That said, the new approach to allow theming of the individual items is good.
Comment #24
kkaefer CreditAttribution: kkaefer commentedLooks solid. I can't find any errors. Ready to commit!
Good job, m3avrck!
Comment #25
Crell CreditAttribution: Crell commentedUsage question here.
Where is the "correct" place in a module, then, to queue up a CSS file? Does this fix the "dump it into hook_menu()" problem?
If a module adds a CSS file, then when overriding it do I need to know where the module is located, so I know its path?
The override example in #8 is PHPTemplate centric, if I understand it correctly. How would other themes handle it?
Are we going to properly slice and dice drupal.css finally? :-)
Comment #26
merlinofchaos CreditAttribution: merlinofchaos commentedThe proper place is either 1) when you know you need the CSS file or 2) in hook_menu if you want it to always be there.
Yes, this is a prelude to splitting up drupal.css
Comment #27
Crell CreditAttribution: Crell commentedWould it work in hook_init()? And what about the override ability?
Generally a module has just one CSS file it tosses into the mix. Themes need to be able to easy cherry pick that list, regardless of where it's coming from.
Comment #28
m3avrck CreditAttribution: m3avrck commentedYes you can easily override CSS files, see the top post and this one
This patch is ready :-)
After this goes in: refactored CSS files :-D
Comment #29
Crell CreditAttribution: Crell commentedI did look at those, which is why I'm asking. You have:
That is explicitly specifying the path to drupal.css, relative to drupal root. Does that mean that the code to disable the default location module CSS file (for instance) will be different if the location module is installed in /modules/ or /sites/all/modules/ or /sites/default/modules/? That's what I'm asking about.
If no, and you can override the incoming CSS regardless of where the module is, great. If you do have to know the path, however, then it means you can't make portable themes that override a module's CSS file, since they'd be dependent on each installation's path setup. That would be a show stopper.
I haven't worked with non-PHPTemplate themes enough (read: at all) to know if there is any impact on them. Someone else will have to chime in on that front.
(I'm not trying to be a stick-in-the-mud, really! :-) )
Comment #30
merlinofchaos CreditAttribution: merlinofchaos commentedAssuming the file I'm looking for is SOMEPATH_I_DON'T_KNOW_ABOUT/foo.css and that is in the variable $path:
basename($path) == 'foo.css';
Thus:
Comment #31
m3avrck CreditAttribution: m3avrck commentedConversly, you can use
Comment #32
drummdrupal_get_css(drupal_add_css())
- PHP code in default implementations of themeable functions should be as simple as possible, this isn't.
- This doesn't follow the convention of similar functions in Drupal.
I'm guessing the goal here is the ability to modify the CSS data structure before it gets to drupal_get_css(). I don't see that as necessary since there is theme_stylesheet_import() called later. But to maintain this functionality, I'm okay with keeping the argument with a default value of NULL that signals it should default to drupal_add_css().
Comment #33
rkerr CreditAttribution: rkerr commentedChanged drupal_get_css($css) to drupal_get_css($css = NULL) and if $css is null, then $css = drupal_add_css() as drumm suggested.
How's this look now?
Comment #34
m3avrck CreditAttribution: m3avrck commentedLooks good to me, that's a nice change I overlooked :-)
Comment #35
rkerr CreditAttribution: rkerr commentedOops. Patch doesn't apply. I'll have to reroll... That's what you get for simply editing a patch file instead of making it properly! :)
Comment #36
rkerr CreditAttribution: rkerr commentedTry this one.
Comment #37
merlinofchaos CreditAttribution: merlinofchaos commented+ RewriteBase /~rowan/drupal-cvs/HEAD/drupal
Probably not what you want in drupal =)
Comment #38
rkerr CreditAttribution: rkerr commentedHmm yeah right :) ... removed that .htaccess chunk.
Comment #39
kkaefer CreditAttribution: kkaefer commentedSeveral changes:
drupal_get_css()
anddrupal_add_css()
functions tocommon.inc
as they aren't theme functions anymore.drupal_add_css
inphptemplate.engine
as it isn't necessary anymore.theme_get_styles()
todrupal_get_css()
inchameleon.theme
(which has been forgotten).Comment #40
Dries CreditAttribution: Dries commentedIn chameleon we have:
Is that correct? Looks like we're almost able to remove theme_stylesheet_import as well?
Comment #41
kkaefer CreditAttribution: kkaefer commentedYou're right. I remove the call to theme('stylesheet_import') in chameleon.theme and replaced it with drupal_add_css(..., 'theme');.
I also removed theme_stylesheet_import() and replaced the only remaining call to this function with the source code itself. Now that we can control what styles are added, we don't need to theme it anymore. We don't theme
link
andscript
tags either.Comment #42
kkaefer CreditAttribution: kkaefer commentedAdded an (optional) to some arguments and explained what it does when no argument is given.
Comment #43
Dries CreditAttribution: Dries commentedIn the code comments and PHPdoc some sentences end with a point, while others don't. Please try to make this consistent/professional. Otherwise, great work refining the documentation! Rock on. Given the documentation gets some additional love, this appears to be ready.
Comment #44
kkaefer CreditAttribution: kkaefer commentedChanged the documentation a bit.
Comment #45
m3avrck CreditAttribution: m3avrck commentedChanges look solid to me!
Can't wait to get this in so I can cook up my next CSS patch (hopefully next day or two). :-D
Comment #46
Dries CreditAttribution: Dries commentedCommitted to CVS HEAD. Marking this 'code needs work' until the upgrade instructions in the handbook have been updated. Set as 'fixed' once this is properly documented and communicated.
Good job, guys.
Comment #47
rkerr CreditAttribution: rkerr commentedAdded very brief comments to the theme and module converting pages:
Converting 4.7 themes to HEAD
Converting 4.7 modules to HEAD
Comment #48
kkaefer CreditAttribution: kkaefer commentedI wrote a patch to port the same system (core, module, theme) to JavaScript files: http://drupal.org/node/76637.
Comment #49
m3avrck CreditAttribution: m3avrck commentedUpdated docs:
http://drupal.org/node/64279#drupal-add-css
http://drupal.org/node/64292#drupal-add-css
Comment #50
democritus-1 CreditAttribution: democritus-1 commentedWhich themes does this work in? Tested on? I'm using a customized blue theme
[mostly shopping cart and user membership changes, plus graphics.]
seattleonly.com
I followed the instructions: patch -p0 < css_13.patch
result- site unusable:
Fatal error: Call to undefined function: theme_add_style() in /virtual/seaonly/dev_site/modules/nice_menus/nice_menus.module on line 26
Thankfully I ran a backup before I patched. I now have to reload the entire site.
Thanks :)
Comment #51
Tobias Maier CreditAttribution: Tobias Maier commenteddemocritus: this is a patch for the current drupal developer version NOT for drupal 4.7
You can't use this patch for drupal 4.7
it has no effects on you current site - only if you disabled drupal.css...
Comment #52
webchickYou don't have to reload the entire site. Just use the -R (reverse) argument to "unapply" a patch:
Comment #53
(not verified) CreditAttribution: commented