3

I have a view and I am trying to rewrite the links by appending a query string.

[name_1] is the name of the location. The various values for age[] are the taxonomy term IDs for an exposed filter, and friends is a profile field.

Under Rewrite results, I have checked Output this field as a link. This is my link:

location/[name_1]?age[]=27&age[]=31&age[]=35&age[]=39&age[]=43&age[]=44&friends=1

When I cut and paste the query string into the URL bar, it works fine. However, when I tell views to rewrite the string I get URLs that look like this:

location/mexico?friends=1&age[]=44

This gives me no results and a broken view.

It appears that the query strings are processed in reverse, and it only accepts the first value for age[]-- even though when I type the same thing into the URL bar it is OK.

I tried replacing the symbols in the rewriting string like this:

location/[name_1]age%5B%5D=27&age%5B%5D=31&age%5B%5D=35&age%5B%5D=39&age%5B%5D=43&age%5B%5D=44&friends=1

Now, when I access the view, I no longer get an error, but none of the age (taxonomy term) checkboxes are checked and the URL looks like this:

http://localhost:8888/en/location/mexico?age%255b%255d=44&friends=1

So, clearly most of the age[] values are still disappearing for some reason.

How can I rewrite the query string so that views outputs it properly?

EDIT: According to Jimajamma's comment, D6 Views suggests using %5B%5D, as I have already tried (I'm on D7). [] didn't work for me either (this combo doesn't even work in the URL bar). Perhaps this is a bug in Views?

EDIT 2: As per the answers, this appears to be a bug in Drupal Core/Views, not a configuration error. So I attempted to avoid using Views rewrite results output as a link feature and instead just rewrite results and code the link in HTML.

The only problem with this approach is that some of the terms have spaces in their names, and I was using the convert spaces to dashes option provided by the output as a link setting to get around this. So now the links for terms that have no spaces work properly, but the links for terms with spaces are broken...

6
  • Just a quick question - are you sure it's in views? The same link outputted via l() works all right? I don't see any place in views that could create this behaviour (that does not prove anything, I just want a little cross-test).
    – Mołot
    Jun 11, 2013 at 12:20
  • @Molot Using l(), the special characters ([]?) get garbled like this location/mexico%3Fage%3D27%26age%3D31%26age%3D35%26age%3D39%26age%3D43%26age%3D44%26friends%3D1. However, at least in this case nothing is cut from the link as it is in Views. Jun 12, 2013 at 11:20
  • Magic. I'll try to look again, but you encountered something either to deep or to obvious for me to spot.
    – Mołot
    Jun 12, 2013 at 11:31
  • 2
    I am gonna go out on a betting limb here and suggest that views is thinking your variable arrays are actually token replacements, eg the []s are confusing it and it is thing [....everything between the first one and the last...] is a field to be replaced/substituted and then not finding anything, collapsing it into just what it could decypher.
    – Jimajamma
    Jun 17, 2013 at 3:57
  • @Jimajamma Ok, sounds reasonable. So how might I get around that? Jun 17, 2013 at 13:15

3 Answers 3

2
+50

Issue is not with views. What views does is it calls drupal_get_query_array to build the query array. And as of now drupal_get_query_array does not support same name array in the query params. All it does is just explodes the query and saves it as follows;

$result[$param[0]] = isset($param[1]) ? rawurldecode($param[1]) : '';

So if you see the see it is going to be overridden and it is just going to store

array ('age' => 44, 'friends' => 1)

I have created a ticket and reported this issue and attached a patch as well at https://drupal.org/node/2023815

The same patch should work for 7.x as well.

Alternatives

So Views php / Custom module looks like a good alternative without hacking the core.

1
  • Awarding you the bounty for filing this as a bug against core and contributing a patch. Thanks for looking into this and getting the ball rolling! Jun 22, 2013 at 11:47
0

Odd that VIEWS is doing that. Here's a fresh approach for you.

My recommendation would be to use the views_php module.

  1. install/enable views_php, Then
  2. exclude the field rather than rewriting it as a link.
  3. add field "global php" to the view [not sure i got that name right, but it has php in it]
  4. in that php field write a small script to take the $data attributes and use the drupal l() function to let Drupal build the url.

Mind you.... The better solution would be to hook into this view and programmatically create the link rather than using views_php to run the eval() on each result. But nonetheless, the views_php module does exist for these quick-n-dirty use cases where the site builder may not be comfortable writing a custom module to hook into Views. Some one else may post the custom module solution here, OR maybe I will later.

1
  • I can write a custom module, but isn't there some way to get Views to support query strings out of the box? Jun 18, 2013 at 15:38
0

user l() like

<?php
l(<Title>,<URL|URI>, 
    array(
        'attributes' => array(
            'id' => 'my-id', 
            'class' => 'my-class'
        ), 
        'query' => array(  /* You Want this */
            'foo' => 'bar',
            'foo1' => 'bar1',
        ), 
        'fragment' => 'refresh',
        'html' => TRUE
        )
    );
?>
1
  • How do I use this in Views? My question is about using a views token plus a query string (that should not be substituted as a token). Jun 18, 2013 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.