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.
I'm trying to add a URL to a link field with a query string of
page?content_type=foo&content_type=bar
but when the link renders I just get page?content_type=bar
I lose my foo, I've tried all the settings on the link field and I also tried the dev version, but can't seem to get this working, am I missing something or is this a bug?
Comment | File | Size | Author |
---|---|---|---|
#23 | duplicate_query_strings-2433057-23.patch | 3.61 KB | cilefen |
#13 | link_query_string_being_removed-2433057-13.patch | 1.58 KB | jessehs |
#10 | link_query_string_being_removed-2433057-10.patch | 2.8 KB | Peter Törnstrand |
#8 | link_query_string_being_removed-2433057-8.patch | 1.22 KB | Anonymous (not verified) |
Comments
Comment #1
dave.erwin CreditAttribution: dave.erwin commentedComment #2
DamienMcKennaThis also affects using ISField to output a youtube video, the "v=j54d-9_gsCQ" part in "http://youtube.com/watch?v=j54d-9_gsCQ" gets removed.
Comment #3
DamienMcKennaFYI I ended up replacing the Link-based field with one using the URL module.
Comment #4
DamienMcKennaI suspect that the query string should go in a 'query' element in the record's 'attributes' field, but it currently isn't happening.
Comment #5
dave.erwin CreditAttribution: dave.erwin commentedthanks Damien, I'm going to try the URL module now.
Comment #6
cbrasfield CreditAttribution: cbrasfield commentedFor what it's worth, the 'display_url' key of the array shows the entire url, as does the title.
Comment #7
saurabh.dhariwal CreditAttribution: saurabh.dhariwal at AddWeb Solution Pvt. Ltd. commentedHello,
Here's the solution:
I've tried using [ ] array format of controller (i.e. content_type) to the input name, later the url appears like: ?content_type[]=foo&content_type[]=bar
This will allow you to add multiple variables with same parameter name.
When the form is submitted, the destination URL looks different in different browsers.
Hope this helps!
Comment #8
Anonymous (not verified) CreditAttribution: Anonymous commentedThe problem stems from the function
_link_parse_str($query)
, called by_link_parse_url($url)
.It's splitting the query string up into components & storing it in an array for use by
_link_parse_url
, which creates URL parts (path, query string, fragment) for use in_link_sanitize()
, which runs those parts through the Drupal functionurl()
to create & display the URL.The problem arises from the fact that
_link_parse_str
is creating an array whose keys are the query parameter names. In the case here, the query parameter names are identical, so the array keys are identical. In that case PHP only keeps the last array key, hence the cut off query string when it's all done.Since Drupal's
url()
(https://api.drupal.org/api/drupal/includes!common.inc/function/url/7) can accept a query string as part of the 'path' parameter, why not just run the whole input URL into the 'path' parameter rather than splitting off the query string, making it an array, & passing it in as the 'query' parameter? What are the advantages of using_link_parse_str($query)
&_link_parse_url($url)
to get the query part as an array & passing it intourl()
as such rather than just plugging the whole URL into the 'path' parameter ofurl()
?I'm attaching a patch which solves this problem by taking the approach outlined above. Not sure if it has some liability since I'm not clear on why it's being done the way it is currently--I could be missing something here.
By the way, it is possible to cause
_link_parse_str
to create a multidimensional array which will create a URL which preserves both identical query parameters, but then those parameters are suffixed with the integer key/index of the query string values, e.g.: page?content_type[0]=foo&content_type[1]=bar. Simply change$query_array[$name] = $value;
to$query_array[$name][] = $value;
(or a more complicated version of that with if/then checks a la https://php.net/manual/en/reserved.variables.get.php#92439 ).PS: The URL module has this same problem; while it will display a URL such as page?content_type=foo&content_type=bar correctly, the HTML link it creates (href) actually is truncated exactly as discribed in this problem. It's using
drupal_parse_url()
to split up the URL parts & passing them into url just like this module.Comment #9
DamienMcKennaFYI if the URL module has a similar problem then it's possibly also a bug in D8 core too.
Comment #10
Peter Törnstrand CreditAttribution: Peter Törnstrand commentedReroll of patch against 7.x-1.3 ... sorry, it got a bit messy.Sorry, that patch was completely broken.
Comment #13
jessehsThis is a reroll of #8 for version 1.3. I ran into this problem with a modal edit of a link field using the Boxes module. The problem did not occur using the standard box edit form (non-modal).
Comment #15
jessehsHaha, I realized that the 1.4 release of the module fixes the problem I was having. I don't know if the original issue reported has been fixed.
Comment #16
dgtlmoon CreditAttribution: dgtlmoon commentedSee #1914072: Link Module displays malformed external URL
Comment #17
Jason Dean CreditAttribution: Jason Dean commentedReopening this issue, as it isn't a duplicate of https://www.drupal.org/node/1914072 and the patch in that issue doesn't fix this.
I populate my link field with this URL: buy-artwork/artworks?artist=All&subject[]=28&subject[]=47&subject[]=27&subject[]=29&sort_by=created
It is rendered as: buy-artwork/artworks?artist=All&subject[]=29&sort_by=created
So where there are multiple values for a query string ('subject' in this example), Link module only applies the last value.
Comment #18
chrlvclaudiu CreditAttribution: chrlvclaudiu as a volunteer commentedagree with @Jason Dean . Same issue for core menu, see https://www.drupal.org/node/1220410
Comment #19
Anonymous (not verified) CreditAttribution: Anonymous commentedThe patch I uploaded has been working for me for two years. Maybe somebody can test it or one of the re-rolls & mark it RTBC, & perhaps it can get into the module eventually?
Comment #20
cilefen CreditAttribution: cilefen as a volunteer commentedBy the way, I checked and Drupal 8's link module is not affected by this bug.
Comment #21
cilefen CreditAttribution: cilefen as a volunteer commentedComment #22
pwolanin CreditAttribution: pwolanin as a volunteer and at SciShield commentedThe bug seems to be that _link_parse_url() is a buggy function http://cgit.drupalcode.org/link/tree/link.module#n752
The patch should instead work to replace with direct use (or wrap) the core function drupal_parse_url($url)
https://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_...
Comment #23
cilefen CreditAttribution: cilefen as a volunteer commentedThank you for that suggestion, Peter.
Comment #24
drummI think this might be the same as #2333119: Output broken when using array parameters in query
Comment #25
cilefen CreditAttribution: cilefen as a volunteer commentedYes, they seem identical. I'm going to close this one and upload my patch on the other. Thanks.
Comment #26
cilefen CreditAttribution: cilefen as a volunteer commented