I am querying a third party database that has both web site urls and email address in a table. If I setup my url field in Views to "Output this field as a link" with the token, [website_url] (a field from the table), and set the "Use absolute path" flag, my rendered links will very.
If "website_url" = 'http://www.example.com', the link will render fine. However, if "website_url" = 'www.example.com', the link renders as 'http://localhost/www.example.com'. I could set my path and token to 'http://[website_url]' and make it work, but I am working with thrid party data and I do not have control this fields. The data could easily be a mix of any of the following:
'http://www.example.com'
'www.example.com'
'example.com'
I want my links to just work no matter what the user enters. The patch below fixes this issue.
I also have an 'email' field. I would like that field to render as a mailto:name@example.com email link. To do this, you just have to add 'mailto:' to the "Prefix text:" field setting in Views. However, when working on the above issue, I discovered that there was an error in the render_as_link() function. It uses the following code to skip the link creation process:
if (empty($path)) {
return $text;
}
// Parse the URL and move any query and fragment parameters out of the path.
$url = parse_url($path);
If you use a "Prefix text:" like 'mailto:' or even 'http://', $path is no longer empty. The following is a better was to handle this:
// Parse the URL and move any query and fragment parameters out of the path.
$url = parse_url($path);
if (empty($url['path'])) {
return $text;
}
The above change is also included in the patch.
Comment | File | Size | Author |
---|---|---|---|
#20 | views-869172-7_0.patch | 3.98 KB | merlinofchaos |
#13 | snapshot9.png | 34.76 KB | dawehner |
#8 | views-869172-7.patch | 3.36 KB | dawehner |
#7 | views-869172-7.patch | 3.09 KB | bendiy |
#6 | views-869172-6.patch | 3.07 KB | bendiy |
Comments
Comment #1
bendiy CreditAttribution: bendiy commentedSorry, wrong file, ignore this one.
Comment #2
bendiy CreditAttribution: bendiy commentedHere is the real patch.
Comment #3
merlinofchaos CreditAttribution: merlinofchaos commentedWhat if someone has "node/1" as the path and checks absolute. That's going to break with this patch, won't it?
Comment #4
bendiy CreditAttribution: bendiy commentedYou're right. For some reason I was thinking absolute==external domain, not root path of the server. I need more coffee.
It looks like this is not going to be as easy as I had hoped. The PHP parse_url is not very good at what it is supposed to do. http://php.net/manual/en/function.parse-url.php I'm really looking for the $url['host'] value, not the path. However, without a scheme, it will put www.example.com in the $url['path'] value, not $url['host'] value.
You also have to keep in mind IP addresses as hosts, 192.168.0.1, could be a link. There are also the urls like this:
The regular expression that matches all of these cases scares me.
Different Solution Idea
How about using a new flag like the current "Use absolute path"? It could be "External Server" and the code could check that value and prevent the 'http://localhost/www.example.com' situation I described above.
Comment #5
merlinofchaos CreditAttribution: merlinofchaos commentedIt could certainly work okay as a checkbox and assume that the path is always external and then treat it appropriately.
The regex to discover paths is scary, and it gets worse every time TLDs get added since really that's the basis for detecting. And if you happen to have a filename that looks like a TLD? No regex will ever get that one right. It's definitely best to try to have the data be explicit.
Comment #6
bendiy CreditAttribution: bendiy commentedAttached is a patch that adds an 'External server URL' check box to the field handler. When checked, fields will be checked for a scheme in the URL path. If one is found, it will work just like it always has. If no scheme is found, a default of 'http://' will be added. If this box is check, internal URLs will work as well, as long as they have the full URL with the site's domain, http://example.com or example.com, not just an absolute path.
The fix for 'mailto:' prefixes with no emails rendering as blank links has also been included. It checks for empty($url['path']) and empty($url['host']) because 'http://www.example.com' will have an empty path, but will have a host value.
Please review.
Thanks!
Comment #7
bendiy CreditAttribution: bendiy commentedI've rerolled this patch for 6.x-3.x-dev. See attached.
Comment #8
dawehnerVery good patch. I like the inline documentation a lot!
Sadly you missed to add it to option_definition, so i rerolled both patches.
This patch fixed this. The rest was rtbc from my perspective.
I updated also some comment styles.
Comment #9
bendiy CreditAttribution: bendiy commentedGreat. Thanks for fixing that. I hope it gets committed soon.
Comment #10
strellman CreditAttribution: strellman commentedCould we make this to allow URLs like:
Skype:skype.name?call
Views keeps making this into a relative URL when I need it to be an absolute.
I am trying to rewrite the output of a skype_field and turn it into a Skype button link, which would be a great integration for a social networking site. Maybe I will have to try computed_field, but Views would have been much easier. I got my hopes up when I found the check box "Use absolute path" on 6.x-3.x-dev but it didn't work, no URL at all is produced.
Here is the output rewrite:
Comment #11
strellman CreditAttribution: strellman commentedFigured out my own question. Adding // before skype: worked, but only in the chrome browser.
Ended up using computed field snippet http://drupal.org/node/957646
Be sure to use Raw Text format.
Be sure to save computed field to the database.
Comment #12
Rj-dupe-1 CreditAttribution: Rj-dupe-1 commentedThe patch doesn't apply cleanly due to views_handler_field.inc not having a $form['alter']['absolute'] = array(...) also note that as strellman discovered, this patch does not allow arbitrary URL schemes (seems to expect/enforce http as the protocol).
Comment #13
dawehner@Rj
Are you sure you are at the right version of views?
The current dev has this.
Comment #14
Rj-dupe-1 CreditAttribution: Rj-dupe-1 commented@dereine:
Hmm; I drush dl views-6.x-3.x-dev so unless there was a cache somewhere I'm pretty sure it was fresh. I then had a look in case the patch had been applied but it didn't seem to have this functionality in the UI. I then tried to patch it, which partially failed so I hand-applied the patch. I didn't specifically look for existing code but I also didn't notice any. After patching, I had the checkbox described above but not the functionality that I was looking for (arbitrary URL schemes), so I deleted it all and tried a few other ideas.
None of the other ideas have panned out so far, so I might try to develop a patch to add support for arbitrary URL schemes and will look at 3.x-dev a little closer to make sure... but I can't imagine the steps above working if the version I downloaded had this patch already applied.
Comment #15
dawehnerPlease please try it again. it's there 100%.
Back to needs review.
Comment #16
dawehnerComment #17
bendiy CreditAttribution: bendiy commentedMake sure an clear your Views cache. Did you have Views 2 installed before? I'm not sure how the upgrade path works. Try and fresh install of Drupal and Views.
Also:
this patch does not allow arbitrary URL schemes (seems to expect/enforce http as the protocol).
Sounds like a feature request. This patch is for domains and mailtos, not web services "Skype:skype.name?call".
Comment #18
dawehnerDrupal by default allows this types in the url:
But it's a variable so you can allow skype and manymore if you need them.
Comment #19
dawehnerIn general this patch needs a rerole against 7.x-3.x
Worked.
Worked
with absolute still works.
without absolute still works.
Based on this RTBC, because the main part of this patch is not from myself.
Comment #20
merlinofchaos CreditAttribution: merlinofchaos commentedCommitted to 6.x-3.x with a small logic adjustment to make sure parse_url() is safe (and to eliminate an unnecessarily nested if).
Comment #21
dawehnerPorted and commited to the 7.x branch.
Comment #22
bendiy CreditAttribution: bendiy commentedThanks for finishing this. I haven't had time to work on Drupal for a while. Glad to see this get committed to both 6 and 7.