Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Allow referencing external CSS files through a call to drupal_add_css.....
$css = 'http://www.csszengarden.com/001/001.css';
drupal_add_css($css, 'external');
Here is a similar issue here to add 'inline' to drupal_add_css.
Comment | File | Size | Author |
---|---|---|---|
#30 | drupal-external-css.patch | 10.26 KB | mfer |
#27 | external-css.patch | 4.56 KB | mfer |
#25 | 264876.patch | 5.27 KB | RobLoach |
#23 | 264876.patch | 5.25 KB | RobLoach |
#14 | 264876.patch | 5.22 KB | RobLoach |
Comments
Comment #1
mfer CreditAttribution: mfer commentedsubscribe
Comment #2
jhedstromsubscribe
Comment #3
Jeff Burnz CreditAttribution: Jeff Burnz commentedTagging.
Comment #4
jhedstromThe attached patch allows external CSS to be added via drupal_add_css(). Tests included.
Comment #5
RobLoachGreat work on this! Really glad that someone kicked it off. The problem with the external CSS is that we're kind of out of luck if we want the external CSS to appear before or after anything else as right now we're just adding it to
$output
. That might be out of scope for this issue though and get into #92877: Add numeric weight to drupal_add_css territory.Other then that, I don't think you need to use
unset($types[$type][$data]);
here because when building the CSS Cache, it checksif ($type == 'module' || $type == 'theme') {
, so I don't really have to remove it from the array. Great work! It's getting there.Comment #6
jhedstromI removed the unset() call from drupal_get_css().
It seems that this patch could go in before the weight one, or after, so long as there is coordination.
Comment #7
mr.baileysThis section should also include the new "external" keyword
Should we add a little something about the exclusion of external stylesheets during aggregation?
Typo.
Some excess whitespace.
Example:
Other than this looks good. All tests pass, and I played around with it on a test installation and it works as expected.
Comment #8
jhedstromThis patch addresses the first 5 issues mentioned by mr.baileys. I've left out validating the link itself. I'd say we either leave this to the calling function, or it belongs in a separate issue (drupal_add_js/drupal_get_js doesn't validate external links either). The alternative, I suppose, would be to pass $media through the url() function.
Comment #9
alexanderpas CreditAttribution: alexanderpas commentedsubscribe
Comment #10
RobLoachVery nice. Added a couple of things to the patch:
$type != 'inline'
when looking for the right-to-left stylesheet, I changed it to an explicit change for either a module CSS file or a theme CSS file. This is because if we're adding an "external" type, it was checking for a RTL stylesheet for it, which would most definitely not be there.unset()
call to remove that CSS from the$types
array. I should've thought of this before, and you brought it up earlier. But what was happening was that during serialization, the inline and external styles would've been considered during the aggregation. Meaning if you had a different inline CSS added on every page request, the MD5 hashed aggregated filename would change everytime, resulting in a new aggregated file every page load. Removing those values from the arrays fixes this.Thanks! It looks great. #92877: Add numeric weight to drupal_add_css will likely fix a lot of the confusing code workflow here.
Comment #11
alexanderpas CreditAttribution: alexanderpas commentedtestbot is happy and I'm also happy ;)
Comment #12
webchick+ $path = 'http://example.com/style.css';
Hm. It makes me really scared to commit code like this knowing that test bot is going to run it 70,000 times per hour. Though I see we already committed a string like this to the JS tests. (it was probably me, too :P)
I would prefer to see this be an absolute link to the site running the tests, unless for some reason this will circumvent the logic we're trying to test. This would also allow us to do more substantiative checks than just "Did > 0 bytes get returned"
Also, just a question -- Why is it desirable to not aggregate external CSS?
(RobLoach answered on IRC that basically this is how we treat JS at the moment. In order to cache external files we would need to come up with an expiry mechanism.)
Comment #13
RobLoachThis patch adds a call to url() for both the external CSS and JavaScript. Do we need this though? Should validation be in drupal_add_css/js(), or be on the system using the external URL?
Example.com actually isn't queried here because the SimpleTest just tests against the constructed HTML and doesn't actually render the HTML on the page. This just checks against the markup, rather then hitting the URL itself.
If we were testing the aggregation of this external URL, then example.com would be queried, but that's not the case. Which brings up the case for pushing the aggregation of these external scripts to a separate issue. We could put it in this issue, but I think tackling external aggregation of both CSS and JavaScript at the same time is probably a better idea since they could gain benefit from using the same cache expiry mechanism.
Comment #14
RobLoachWhoops, changing $data is not a good idea.
Comment #16
lilou CreditAttribution: lilou commentedSetting to 'needs review' - testbot was broken.
Comment #17
alexanderpas CreditAttribution: alexanderpas commented@webchick
example.com, example.net and example.org are specifically made for this purpose.
it contains only an index.html and a robots.txt, all other pages return 404
See also RFC 2606, Section 3 and 5
Comment #19
alexanderpas CreditAttribution: alexanderpas commentedhttp://testing.drupal.org/node/37
http://testing.drupal.org/pifr/file/1/264876_1.patch
Comment #21
deekayen CreditAttribution: deekayen commentedtrying out a new testing bot that apparently wasn't working
Comment #23
RobLoachUpdated after #92877: Add numeric weight to drupal_add_css went in.
Comment #25
RobLoachWhoops, it's $item['media'].
Comment #27
mfer CreditAttribution: mfer commentedThere was an error in the code comments of the previous patch. An extra / was at the end of a code comment.
The CSS solution essentially the same solution that has already been implemented for external js files. To be consistent, I think we should go this route for now.
I don't like the use of url() here. It doesn't actually add anything to the picture. Because 'external' is set to true all it really does is add an extra fragment and query string onto the end if one is provided. In this case they aren't so there is no benefit. url() with external passed in doesn't do any validation in D7. See http://api.drupal.org/api/function/url/7
If we want to check for a valid url we can use something like valid_url (http://api.drupal.org/api/function/valid_url/7) which uses a not so simple (or fast) regex or we can use something like the PHP filters which doesn't validate to spec. So, it will tell you it's roughly the right format but it still may not be.
url validation is hard. valid_url is to spec (RFC 3986) but the spec doesn't reflect reality. For example, the spec doesn't allow for international characters in urls. Well, you can go to the site http://☃.net/ (it routes) or http://例え.テスト/メインページ. There is an issue for this already.
For the time being I would remove the url() call and expect the caller to make sure it's valid.
The attached patch removes the extra / and the calls the url().
Comment #28
RobLoachSounds good with me! The patch is simple, provides documentation and its tests pass. Setting to RTBC.
Comment #29
webchickThis patch is great, since it is the final piece (I think?) that gives us symmetry between drupal_add_js() and drupal_add_css().
But, let's complete the symmetry:
in drupal_add_js() this is:
Can we make the descriptions of both of these a hybrid of the two that explains the parameter is asking for an absolute path, but also notes that files put in this way are not aggregated? (Unless they are in JS and not in CSS for some reason, and if so it'd be good to point that out specifically, too.)
in drupal_add_js():
In this case, I think drupal_add_js()'s description is actually more helpful, so let's duplicate it here.
Also, no need to break; on a default.
Too much symmetry. ;) I think you mean "CSS files." ;)
I don't see a corresponding test in the JS stuff. Can we please add one?
I realize you're probably trying to save a variable when you don't need one, but everywhere else we do:
So let's do the same here.
After that, this looks good to go.
Comment #30
mfer CreditAttribution: mfer commentedUpdated per webchicks comments. The only difference is that instead of $css = drupal_get_css() I used $styles. In many of the test $css is used for the css put into drupal_add_css. I made $styles usage consistent on all CSS tests.
Comment #31
alexanderpas CreditAttribution: alexanderpas commentedRTBC confirmed, next time, please put on need review first.
Comment #32
webchickHuh. I totally thought I'd committed this already!
Committed to HEAD. Thanks!
Comment #33
webchickWow I must be tired. :P This needs docs of course.
Comment #34
jhodgdonDocs added http://drupal.org/update/modules/6/7#add-css-external
I don't think the Theme update guide needs it, since themes specify CSS and JS files via .info