This patch lets custom_url_rewrite() implementations make changes to the querystring and fragment components of the URL. I have twice ran into this limitation: one when attempting to move comments to own tab (see http://groups.drupal.org/node/2931) and again when wanting to append authentication tokens to all feed URLs (see http://drupal.org/project/tokenauth) .
This patch moves the custom_url_rewrite() invocation from from drupal_get_path_alias() directly into url(). But the code is still in the same spot in the execution flow - just querystring and fragment are made easily available. Having done this, I was also able to cleanup path_nodeapi() which was specifically avoiding drupal_get_path_alias() because of the rewrite call. While cleaning that, I noticed that this code retrieved an arbitrary path for the node, not the one for the node's language. This is now fixed.
| Comment | File | Size | Author |
|---|---|---|---|
| #6 | mw_49.patch | 3 KB | moshe weitzman |
| #3 | mw_35.patch | 2.74 KB | moshe weitzman |
| #1 | mw_34.patch | 2.25 KB | moshe weitzman |
Comments
Comment #1
moshe weitzman commentedoops. file attached.
Comment #2
gábor hojtsyIt would be nice to get a list of places calling drupal_get_path_alias(), so we can see what is affected by moving the URL rewrite from the lookup function to url(). It sounds like a good API cleanup to have custom_url_rewrite() called from url(), as the name indicates, but the current use cases should be checked.
Comment #3
moshe weitzman commentedThis new patch splits custom_url_rewrite() into custom_url_rewrite_inbound() and custom_url_rewrite_outbound(). We were shoehorning two different functions into one - they need different arguments. Further, I couldn't keep straight which direction 'alias' was. I think the new names are clearer. Lastly, a site took a performance hit if it only wanted to rewrite inbound urls since their function would be called for every outbound link as well.
I thought Gabor might ask that. I think it is acceptable to to require admins to enter a system path or an alias but not a rewritten path in the block visibility textarea (for example). the other callers should be unaffected by this change. language has its own rewrite step so it doesn't care about this.
Current calls to drupal_get_path_alias() that are affected:
./includes/language.inc:127: $path = drupal_get_path_alias($path, $path_language->language);
./modules/block/block.module:707: $path = drupal_get_path_alias($_GET['q']);
./modules/path/path.module:258: if (drupal_get_path_alias($path) != $path) {
./modules/statistics/statistics.module:537: $title = drupal_get_path_alias($path);
Comment #4
gábor hojtsyAlthough the changes you offer seem to be logical and straightforward, I am not entirely comfortable with saying "yes, sure", as I don't have knowledge about how contributed modules utilize aliasing, especially i18n related modules. It would be nice to post grep into a contrib checkout and/or ask Jose Reyero and Roberto Gerola to comment on this change.
Comment #5
chx commentedI have concerns. First of all, the current system lets you access the path before and after aliasing. This is important because if someone adds an alias to a system path you are rewriting then you rewriter is screwed. Second the list(a, b) = foo(a,b) construct is ugly, just call foo(a, b) and get them by reference.
Comment #6
moshe weitzman commentedhere is a new patch, which addresses both concerns voiced by chx.
i have asked jose for a review also. i don't have robertg's email.
Comment #7
chx commentedI much love this patch. As path_nodeapi shows, things get simplified. Also, custom_url_rewrite will be easier to use and more powerful because it change options.
Comment #8
jose reyero commentedAbout the language modules using this mechanism, I think we shouldn't worry too much about them, as now that all is handled nicely by Drupal core, so they won't need that anymore.
Also, completely ok with the change in node module, I'd say that's actually a current bug, as we were missing node language for that aliasing and better if we don't do direct queries in there and use the api.
However, based in past experience messing with paths, languages and i18n, I think that if you want to allow some module full control over paths/aliases/urls, what we rather need is some hook in url similar to what we've done for the language system.
And then we might use one more hook in drupal_get_normal_path, and drupal_get_path_alias, but I'd put it before calling drupal_get_path alias, so it allows much more control over the process of path aliasing. This way your module can call itself drupal_lookup_path if needs to.
And same for drupal_get_normal_path().
(And btw, a module could also skip alias searching quieries for certain known paths allowing some optimization here)
Comment #9
moshe weitzman commentedJose's suggestion is a feature request above and beyond what this patch intends to do. This patch intends to " Let custom_url_rewrite operate on querystring and fragment". As chx has marked this as RTBC, I think we are ready for a committer.
Comment #10
gábor hojtsyAlthough the introduced asymmetry (of alias and original path lookups) does not look entirely logical, I don't know a better way to deal with it, so fixed the punctuation problems in the code comments and committed.
Please update the module update instructions. I leave this code needs work until it is done.
Comment #11
moshe weitzman commentedUpdated the upgrade docs and documented both of the new functions on api.drupal.org - http://api.drupal.org/api/HEAD/function/custom_url_rewrite_outbound and http://api.drupal.org/api/HEAD/function/custom_url_rewrite_inbound (these pages will start working sometime soon).
Comment #12
(not verified) commented