A views field can be configured to "Output this field as a link". When configured this way, views' link can be given a "Link path". If Token module is enabled, tokens can be used for the "Link path". If a token for a Link field is used as the "Link path", a variety of problems occur in the href="" attribute of the link that views renders;

  1. With the "URL, as link" formatter, long URLs get truncated. E.g. href="http://demo.open-realty.org/index.php?price-min=4.0E%2B16&price-max=1.7E%2B17&community_featu..."
  2. With the "URL, as plain text" formatter, Drupal-internal paths get put through url() once by each of theme_link_formatter_plain() and views_handler_field::render_as_link(). An extra forward slash character gets prefixed to the path, each time it is passed through url(). E.g. href="//node/1".
    As per the specification, browsers parse "//" as the separator between the protocol and host parts of the URL, assume default protocol of "http", and treat "//node/1" as equivalent to "http://node/1". In most cases this does not resolve at all, let alone point to the destination that was intended.

In summary, there is no formatter for Link fields that is compatible as a token in the "Link path" of views fields that are configured to "Output this field as a link".

The attached patch adds the formatter "URL, as absolute URL". This always returns an absolute URL and is thus always compatible as the $path argument for url(), and is thus also as the "Link path" for a views field that is configured to "Output this field as a link".

It uses <front> as the path for link fields which do not have a URL and only have a title.

Comments

danmed’s picture

Hello Bevan,
I was encountering the same issues.
I applied your patch and it works for me. Thanks a lot.

If maintainers agree, I think this could be commited as people using Views along with Link may want indeed to be able to retrieve the full URL for adding the link to pieces of text, images, ...

jcfiala’s picture

Status: Active » Needs review

Hey folks!

When you've got a patch in your post, you want to set the status to 'needs review'. That flags people that there's a patch associated with a problem now. Then, when a few people agree that it's a good patch (or someone well known says it's a good patch), then the status gets switched over to 'reviewed & tested by the community', which means then I know it's definitely a patch I need to spend some time with.

Thanks!

dkingofpa’s picture

StatusFileSize
new1.26 KB

@Bevan, Thanks for the patch. This is what I needed to effectively rewrite the output of my field (URL, as link and URL, as plain text were definitely not working). I agree with everything except

It uses <front> as the path for link fields which do not have a URL and only have a title.

By using <front> when only a title is specified, the views "hide if empty" checkbox is ignored and a URL is returned. This is not expected behavior. If you need that functionality, I'm sure there are other approaches in views to solve your issue.

I updated the patch so "URL, as absolute URL" returns an empty string if it is empty, thus honoring the "hide if empty" checkbox. Other than that, the patch worked for me.

dragonwize’s picture

Status: Needs review » Reviewed & tested by the community

Thanks Bevan and dkingofpa. Works here and coding looks fine.

Bevan’s picture

@dkingofpa; I agree that makes more sense.

shaundychko’s picture

Looks good to me. Thanks!

This issue appeared as an error message in our db log:

parse_url(http:///research/facilities) [function.parse-url]: Unable to parse URL in ...modules/views/handlers/views_handler_field.inc on line 608.

but otherwise didn't cause any problems... it's nice to make that error message go away though.

sammys’s picture

StatusFileSize
new1.37 KB

Here's a patch for 7.x also. I have a feeling the plain formatter should be modified to deal with the problem instead of adding this formatter. I don't have time to debug it so I ported the patch instead.

shaundychko’s picture

I should mention that, while it seemed to work initially, the patch in #3 doesn't quite work at removing the error message in #6. It's been impossible to debug though since the php error occurs for anonymous users sporadically, and hasn't been reproducible.

jcfiala’s picture

Version: 6.x-2.9 » 7.x-1.0
Status: Reviewed & tested by the community » Needs review

Cheers! This has been merged in with the 6.x-2.x branch!

I'm going to set this to 'needs review' and '7.x-1.0' so that folks can test out sammys' patch in #7 for use in that branch.

Thanks Bevan, dkingofpa!

bancarddata’s picture

Tested #7 against 7.x-1.0, Views 3.1, Drupal 7.12 - the "URL, as absolute" formatter works perfectly for me! Thanks all!

dqd’s picture

Version: 7.x-1.0 » 7.x-1.x-dev
Status: Needs review » Fixed

reviewed, tested, committed and pushed against latest 7.x --dev successfully with a x20 lines offset

Patch applied cleanly :-)

thanks to all!
(credits in the commit message)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.