According to the API docs for drupal_add_css():
"Themes may replace module-defined CSS files by adding a stylesheet with the same filename. For example, themes/garland/system-menus.css would replace modules/system/system-menus.css. This allows themes to override complete CSS files, rather than specific selectors, when necessary."
(http://api.drupal.org/api/function/drupal_add_css/6)
However, this does not seem to be the case. Instead, both CSS files are included.
To reproduce:
- Create a module that makes use of a theme CSS file (included with drupal_add_css()). For example, a module named 'my' that uses a CSS file called 'my.css'.
- Create a CSS file in your chosen theme (I used bluemarine) that also contains a file my.css
- Modify the theme's .info file, adding: style[all][]=my.css
- Do whatever must be done to refresh the style.
What is expected:
The my.css file from the module should not be included in the <link/> elements in the HTML. Instead, the theme's my.css file should be included.
What happens:
Both are included.
Either the API documentation for drupal_add_css() should be fixed, or the behavior of the theme system should be fixed.
Details:
Version: Drupal 6, beta 2
OS: Ubuntu Linux 7.04
Webserver: Apache 2.2
PHP: 5.2.1
Browser: Firefox 2.0
Relevant snippets from module named philquotes:
function theme_philquotes_quote($text, $origin) {
$full_path = drupal_get_path('module', 'philquotes')
.'/philquotes.css';
drupal_add_css($full_path);
$output = '<div id="philquotes-text">'. t($text)
.'</div><div id="philquotes-origin">' . t($origin) . '</div>';
return $output;
}
And the CSS in the module's philquotes.css:
#philquotes-text:first-letter {
font-size: 18pt;
font-weight: bold;
}
In bluemarine, I added philquotes.css:
#philquotes-text {
color: pink;
}
And modified bluemarine.info, adding the following two lines:
stylesheets[all][] = style.css
stylesheets[all][] = philquotes.css
The resulting HTML included link tags for all three of the following:
href="/drupal/sites/all/modules/philquotes/philquotes.css"
href="/drupal/themes/bluemarine/style.css"
href="/drupal/themes/bluemarine/philquotes.css"
Based on the API docs, I did not expect the first CSS file to be included.
| Comment | File | Size | Author |
|---|---|---|---|
| #27 | css_compress_d6.patch | 908 bytes | desbeers |
| #22 | style_aggregation.patch | 894 bytes | dvessel |
| #16 | style_override_from-add-to-get_b7.patch | 5.5 KB | dvessel |
| #11 | style_override_from-add-to-get_b.patch | 5.51 KB | dvessel |
| #10 | style_override_from-add-to-get.patch | 5.22 KB | dvessel |
Comments
Comment #1
mbutcher commentedMinor correction:
Step #3 above should modify stylesheets[all][], not style[all][].
This is correct in the given code, and (obviously) the unanticipated result still obtains.
Comment #2
jjalocha commentedThis bug seems to be related to http://drupal.org/node/197124.
Comment #3
dvessel commentedI can't reproduce. I tried with system-menus.css and the one from the theme is the only one present.
Comment #4
jjalocha commentedI can't reproduce here with the following files:
'newtheme.info' file:
The 'style.css' file is empty.
'system-menus.css' file:
Result is as it should be: The browser's default bullets are applied, and links are displayed in red.
'modules/system/system-menus.css' is not in <head> section:
Comment #5
jody lynnCan't reproduce in Beta 4.
Comment #6
oadaeh commentedApparently, it matters when drupal_add_css() is called. If it is called in hook_init(), as is the case for system-menus.css and several (all? -- I only sampled them) other core modules, it works as expected. If it is called at some other time, like in hook_block() or a theme() function, you end up with both CSS files being included. FYI, I didn't test all functions to find out if the absolute of only hook_init() and all other functions was true. It could be there are other times when it works correctly.
Comment #7
dvessel commentedJust did a quick look over the flow and this is definitely a problem. If the module styles are added into drupal_add_css() first, it's not a problem. The problem arises when setting override styles through the .info file. They are added in at _init_theme() so a module pushing styles outside of hook_init() (which runs before init_theme()) will get added in a second time. Not good.
Either more complexity is added to drupal_add_css() or we delay the adding of styles defined in .info so it happens around template_preprocess_page(). Or can anyone think of a better idea?
Comment #8
moshe weitzman commenteddelay adding of styles sounds right to me.
Comment #9
dvessel commentedI just had a thought. How about moving the overriding into drupal_get_css. It happens much later and it doesn't run as often so it could save a few cycles.
I'll have a patch later tonight.
Delaying the .info style inclusion would make a mess of template_preprocess_page. The other option would be to hold the styles in a new function to be called in template_preprocess_page. The latter would be too much for 6.
Comment #10
dvessel commentedThis fixes 2 issues actually. The one mentioned here and themes overriding module styles where RTL files need to be explicitly set for it to override the module RTL style.
This is no longer needed. I also removed the mention of 'core' type for styles. It was considered while developing the CSS aggregation feature during 5.x-dev but it never got in. It's only 'module' and 'theme'.
Comment #11
dvessel commentedCorrection on the theme overriding module RTL styles. I forgot there was a good reason for that. RTL styles depend on cascade order so I put that back in.
This should behave the same as before. It's only done on get. I tested it and I think it's ready. If someone could verify and update the status, that'd be great.
This really simplifies drupal_add_css(). Count the number of style sheets on your pages, and that's the number of times it's called while drupal_get_css() typically gets called only once.
Comment #12
oadaeh commentedI'm not sure if this was an intended side effect or not, but after including this patch, it is no longer necessary to have "stylesheets[all][] = stylesheetname.css" for the sheet to be included, as is stated in http://drupal.org/node/171205#stylesheets. The file just simply needs to be in the theme directory to be included. Other than that, this seems to work as expected.
Comment #13
dvessel commentedoadaeh, the patch does nothing to automatically add style sheets. By default style.css does load on it's own but that's what always happened. http://drupal.org/node/171206#default-stylesheets
If it's not named style.css, then it could simply be the .info file being cached which can be cleared by resubmitting the theme select form. Not to be confused with the theme registry.
Comment #14
oadaeh commentedOkay. I re-installed my Drupal testbed (dual installation for this bug) and re-ran through all the tests I did before, and this time it worked as expected. Maybe I did have a cached .info file (though I was seeing the updated change on the screen right away), or maybe my test install was tweaked from my testing. One thing I noticed during the testing for this bug was that clearing the cache didn't always get me an updated page. I sometimes needed to go to the modules and/or themes admin pages for the theme changes to be reflected on the page.
I'm giving this the big RTBC, unless we want someone else's eyes on it.
Comment #15
dvessel commentedThanks for testing. This should apply to 7 also.
Comment #16
dvessel commentedForgot this has to be marked for 7. And the patch due to fuzz.
Patch #11 still applies to 6.
Comment #17
dries commentedI've committed this to CVS HEAD because it looks sound. I think it is sound for D6 as well but it could use another review.
Comment #18
dvessel commentedCould we get someone to test this? It'd be nice to get this in before 6.1.
Comment #19
gábor hojtsyI reviewed all the report and the code and it all looked fine. I stopped for a considerable time on the last hunk while trying to understand how things happen there, but it became understandable after looking at the code around. Committed to 6.x.
Comment #20
vladimir.dolgopolov commentedHere is the subsequent test for the issue I'd made for D6.
Comment #21
dvessel commentedArg! The bug comes back when aggregation is turned on.
Continuing on the loop isn't good enough since $types is passed to drupal_build_css_cache. Just need to unset it & continue but must account for RTL styles.
Patch coming..
Comment #22
dvessel commentedWell, rtl is already accounted for. Just needed to unset. This'll apply to both 6 & 7.
Comment #23
floretan commentedSuccessfully tested on 7.x-dev.
Comment #24
dvessel commentedThis is still a problem.
Comment #25
dries commentedI've committed this to CVS HEAD. Still needs to go into DRUPAL-6 so I'm lowering the version.
Comment #26
damien tournoud commented#250481: css optimizer uses both core and override css was a duplicate.
Comment #27
desbeers commentedAttached a patch for Drupal 6.
Comment #28
gábor hojtsyCommitted to 6.x. Thanks!
Comment #29
Anonymous (not verified) commentedAutomatically closed -- issue fixed for two weeks with no activity.
Comment #30
egfrith commentedI think the patches here may have caused a regression: #335106: sub-theme doesn't import parent's css
Comment #31
MGParisi commentedIssue http://drupal.org/node/417874 seems to be related to the issue. The
drupal_add_css(drupal_get_path('module', 'author_pane') . '/author_pane.css');is in the template_preprocess_author_pane(&$variables function. Should be easy for the module developer to change the code given this information, but this "seems" to be a continuation of the problem listed. Not sure, just sharing my experience...
Note: All Cache is OFF! No RTL is being used (currently).
I can fix it with inserting this into the themes .info file...
Will confirm with another user ASAP (Its sunday at Midnight, I doubt I will find a Advanced Forum user on the Drupal IRC site, but I will see.
Comment #32
pteague commentedJust installed drupal 6.4, author_pane-6.x-1.0-rc1 & advanced_forum-6.x-1.0-rc4.
Copied & modified the author_pane.css to the garland theme directory & modified a couple things. The author_pane.css from the garland theme isn't even being included, only the author_pane.css from the module directory.
Comment #33
MGParisi commentedUpdate: The original .css is removed if you put the css file name in the info. So I guess this works as stated... issue remains closed...