This patch adds a very small utility function, drupal_add_basepath, to common.inc. I am breaking this patch out of my more ambitious dialogs patch (http://drupal.org/node/193311) since that probably won't make it into D6. It is just silly that there are so many contrib module out there right now that all have the same line of code:
drupal_add_js( array( 'mymodule' => array( 'base_path' => base_path() )), 'setting' );
Most modules that use Ajax need this line, and when I look at the source of a page, I will often see the base_path getting passed in two or more times.
In system.js you only get away with this line because you know Druapl.cleanURLsInstallCheck is only going to be bound on a single page with a known path.
var url = location.protocol +"//"+ location.host + location.pathname.replace(/\/[^\/]*$/, "/") + "admin/settings/clean-urls/check";
could become
var url = location.protocol +"//"+ location.host + Drupal.settings.base_path + "admin/settings/clean-urls/check";
Comment | File | Size | Author |
---|---|---|---|
#25 | base_path.patch | 2.56 KB | Wim Leers |
#22 | base_path.patch | 2.56 KB | Wim Leers |
#18 | base_path.patch | 2.17 KB | Wim Leers |
#17 | drupal_add_js_base_path_6.patch | 2.02 KB | RobLoach |
#14 | add_basepath_5.patch | 2.72 KB | RobLoach |
Comments
Comment #1
Wim Leers+1 for idea, -1 on the execution though.
- Code is not conform the Drupal coding standards.
- "bathpath" instead of "base_path".
- You should implement the enhancement to Drupal.cleanURLsInstallCheck you're suggesting.
Comment #2
starbow CreditAttribution: starbow commentedOk, I cleaned up the tabs and weird spelling (the danger of coding at 4pm). I ran it through coder and it didn't have any comments. And I patched install.php and system.js to implement the cleanup to Drupal.cleanURLsIncallCheck that I proposed. Works like a charm.
Comment #3
Wim Leers- Doxygen comment blocks start with "/**", not "/*".
- I think most of core uses "Javascript" instead of "javascript'. Sentences should be separated with one instead of 2 spaces. Dot missing at the end.
- "Needed for many Ajax calls" might be worded a bit better? I haven't got any suggestions though.
Comment #4
starbow CreditAttribution: starbow commentedThanks.
I changed it to:
Comment #5
Wim Leers- "URL", not "url"
- You may need it in jQuery .load() calls too, and I'm sure others exist as well. Perhaps something in the lines of "The base path of a site is needed in dynamic requests to the server (AJAX/AHAH), to build the URL to Drupal paths."?
Sorry for all the nitpicking, but it wouldn't even be considered for core inclusion if all these details are right :)
Comment #6
starbow CreditAttribution: starbow commentedNo apology needed. I really appreciate you taking the time to let me know what I need to do.
Comment #7
starbow CreditAttribution: starbow commentedComment #8
starbow CreditAttribution: starbow commentedChanged the name of the function to drupal_add_base_path_to_js(), to make it clearer what it does.
Comment #9
Wim Leers- Comments should wrap at col 78.
- Only one return after a function.
Then it seems ready to me.
Comment #10
profix898 CreditAttribution: profix898 commentedJust a thought: Why dont we add this automatically on every page that uses javascript (at least one call to drupal_add_js)? I mean many pages will use this to add a base path settings for custom js, so we could simply add this by default as we do with jquery.js and drupal.js ...
Comment #11
Wim Leersprofix898: cool idea. That might work.
Comment #12
Wim LeersI actually think that makes a lot of sense. It's used virtually everywhere, the overhead is negligible and it's much cleaner than adding another function.
Let's move the code there?
Comment #13
starbow CreditAttribution: starbow commentedI don't know about adding the functionality to drupal_add_js. Obviously, base_path is very useful to have, but there is plenty of javascript to doesn't need it, and it seems pushy to require it for everyone.
This patch just has the two cleanup points Wim pointed out in comment #9.
Comment #14
RobLoachI agree that sticking it in whenever you use Javascript is a bit much. Not all instances will use base_path.
The patch works well with the clean URLs check. Looks good to me! Here's the patch with the correct line endings.
Comment #15
Wim LeersI disagree. The overhead of adding base_path into Drupal.settings is negligible, but the result is cleaner than what this patch is doing right now.
Comment #16
RobLoachSounds good to me, let's add base_path in there.
Comment #17
RobLoachThe attached patch adds
Drupal.settings.base_path
when it initializes Drupal.settings. It also uses Drupal.settings.base_path in the Clean URLs check as well as at installation. Mind testing?Comment #18
Wim LeersI thought this patch was already committed a long time ago... Since it's not, finally a reroll!
It also fixes a missing semi-colon in system.js.
Comment #19
Wim LeersComment #20
yched CreditAttribution: yched commentedLooks like #17 and #18 crossposted.
patch in #18 has :
while #17 has :
which one is correct ?
Comment #21
yched CreditAttribution: yched commentedHmm.
Comment #22
Wim LeersAfter a rough grep(grep "location\.pathname" * -r), I couldn't find any other places in core that could benefit from this. So, here's the only missing other occurrence where Drupal.settings.basepath can be used (the one that Rob didn't miss and I did).
Comment #23
Gábor HojtsyChange looks sensible and fixes bug like http://drupal.org/node/201248 so it would be great to see some more testing :)
Comment #24
RobLoachyched asks if it's better to have the array key defined rather then not defined? It doesn't really matter because Drupal just loops through them all.
This looks good! Just tested it during install as well as in the Clean URLs check page. A couple more tests in other systems would be good too.
Comment #25
Wim LeersRenamed "base_path" to "basePath" (so the end result is
Drupal.settings.basePath
) for consistency with the rest of the JS.I prefer my version, because otherwise you have the same thing there twice, which might cause confusion. ("Which one really affects the name of the setting?")
Comment #26
RobLoachWhatever floats your boat! We're going to also have to add this to Converting 5.x modules to 6.x.
Comment #27
Wim LeersAfter it is committed ;) :)
Comment #28
yched CreditAttribution: yched commentedTested, works perfectly for me too. +1 for RTBC.
Comment #29
Gábor HojtsyLooks good, thanks, committed.
Comment #30
Wim LeersYay :) Documentation is already updated: http://drupal.org/node/114774#base_path.
Comment #31
quicksketchI didn't participate in this issue, but great job everyone! Thanks for making it basePath also. Now I can take basePath out of like 5 of my modules :D
Comment #32
(not verified) CreditAttribution: commentedAutomatically closed -- issue fixed for two weeks with no activity.