1

I have a view in D7 with a few exposed filters. This view generates a URL/page like this: example.com/node?location=XXX&check-in=XXX&check-out=XXX&lang=XXX.

I am looking to build a link passing in a couple of the current page's query parameters from above. I am using the l() function and currently have the following:

print l("link_name","http://www.example.com/path", array('attributes'=>array('target'=>'blank'), 'query'=>array('lang'=>'en', 'currency'=>'USD', 'standardCheckin'=>$view->get_exposed_input(), 'standardCheckout'=>$view->get_exposed_input())));

I am looking to pass in the check-in and check-out parameter values from the views-generated page in to my link function's standardCheckin and standardCheckout keys. What I currently have obviously does not work. I'm using the view::get_exposed_input function to pull in the values that are in the exposed filter, but it is pulling in ALL of the query parameters. This function does not seem to have any query parameters to limit the query. I also looked at using drupal_parse_url and drupal_get_query_parameters, but can not get those to work either (again, getting all parameters - can not limit to just the two I need).

I can pull the values from either the URL or from the exposed filter. My PHP is a bit limited, but what is the best function to use (and a nudge syntactically on how to limit the query would be appreciated)? Or is there a better Drupal method for achieving this?

0

Maybe something like this would be sufficient?

$url = drupal_parse_url('example.com/node?location=XXX&check-in=XXX&check-out=XXX&lang=XXX.');
$desired_params = array_intersect_key($url['query'], array_flip(array('check-in', 'check-out')));

array_intersect_key() will return the values in the first array whose keys also exist in the second array, while array_flip() inverts the values in the array with their keys, actually generating something like the following (in my example):

array(
  'check-in' => 0,
  'check-out' => 1,
)

You can probably use the same approach or a very similar one with view::get_exposed_input, which is probably the best way of getting the data you need.


I didn't get that you also needed to rename the parameters. Then I don't think there's any advantage in filtering the parameters, just pull out the ones you need by hand.

$params = drupal_parse_url('...');
l("link_name","http://www.example.com/path",
  array('attributes' => array('target' => 'blank'),
  'query'=>array(
    'lang' => 'en',
    'currency' => 'USD',
    'standardCheckin' => $params['query']['check-in'],
    'standardCheckout'=>$params['query']['check-out']
   )
  )
);
  • I'm not sure using array_intersect_key will work. If I understand correctly, the keys have to match in both arrays. In my example, I'm trying to match standardCheckin and standardCheckout in first URL with check-in and check-out in second URL. They won't match, correct? Then, want to pull the values from first URL and insert into second URL. – Norman Aug 1 '14 at 17:37
  • I didn't get that you also needed to rename the parameters. Then I don't think there's any advantage in filtering the parameters, just pull out the ones you need by hand. $params = drupal_parse_url('...'); l("link_name","http://www.example.com/path", array('attributes'=>array('target'=>'blank'), 'query'=>array('lang'=>'en', 'currency'=>'USD', 'standardCheckin'=>$params['query']['check-in'], 'standardCheckout'=>$params['query']['check-out']))); – RB_ Aug 4 '14 at 11:32
  • This works! Using drupal_parse_url with l() function is giving the exact results I was looking for. Thank you! (Note: not sure how to make your comment the answer.) – Norman Aug 7 '14 at 18:55
  • Integrating the comment in the answer itself. :) – RB_ Aug 8 '14 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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