Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have a views page with several exposed filters. I have created a link using a view php field as per below:

$url = '/admin/dashboard/applications/create/' . $row->nid . '?destination=' . current_path() . '&' . $_SERVER['QUERY_STRING'];
$link = '<a class="create-app" href="' . $url . '" title="Create application">App+</a>';

This leads to a form page with a confirmation 'Submit' or 'Cancel' button. The resulting URL on that page being something like this:

http://www.site.com/admin/dashboard/applications/create/154575?destination=admin/dashboard/enquiries&nid=&field_enquiry_fname_value=&field_enquiry_email_value=&field_enquiry_country_tid=&field_enquiry_campaign_id_value=&field_enquiry_emailed_value=0&field_enquiry_stage_1_tid=All&field_enquiry_enabled_value=1

When the redirect is done, the querystring after ?destination=admin/dashboard/enquiries is removed. I think this happens because it is not URL encoded.

I then tried url encoding the link as per below:

$query_string = array('destination' => current_path() . '&' . $_SERVER['QUERY_STRING']);
$url = url('admin/dashboard/applications/create/' . $row->nid, array('query' => $query_string));
$link = '<a class="create-app" href="' . $url . '" title="Create application">App+</a>';

This creates the following url:

http://www.site.com/admin/dashboard/applications/create/154575?destination=admin/dashboard/enquiries%26nid%3D%26field_enquiry_fname_value%3D%26field_enquiry_email_value%3D%26field_enquiry_country_tid%3D%26field_enquiry_campaign_id_value%3D%26field_enquiry_emailed_value%3D0%26field_enquiry_stage_1_tid%3DAll%26field_enquiry_enabled_value%3D1

The issue now is that when the redirect is done back to the views page, views does not recognize the URL query and does not load the view, and falls back to the parent menu callback.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Instead of Trying to generate destination manually, can use drupal_get_destination(). So your above code would be like this:

$url = url('admin/dashboard/applications/create/' . $row->nid, array('query' => drupal_get_destination()));
$link = '<a class="create-app" href="' . $url . '" title="Create application">App+</a>';

drupal_get_destination() gets the current path + encode the query strings and make them as destination. this will generate below URL:

http://www.site.com/admin/dashboard/applications/create/154575?destination=admin/dashboard/enquiries%3Fnid%3D%26field_enquiry_fname_value%3D%26field_enquiry_email_value%3D%26field_enquiry_country_tid%3D%26field_enquiry_campaign_id_value%3D%26field_enquiry_emailed_value%3D0%26field_enquiry_stage_1_tid%3DAll%26field_enquiry_enabled_value%3D1

instead of your URL.

http://www.site.com/admin/dashboard/applications/create/154575?destination=admin/dashboard/enquiries%26nid%3D%26field_enquiry_fname_value%3D%26field_enquiry_email_value%3D%26field_enquiry_country_tid%3D%26field_enquiry_campaign_id_value%3D%26field_enquiry_emailed_value%3D0%26field_enquiry_stage_1_tid%3DAll%26field_enquiry_enabled_value%3D1

The difference is the encode of '?'. I have tested it in local D7 installation.

share|improve this answer
    
This has worked, views now recognizes the query string and the exposed filters are applied. Thank you –  davewilly Jul 8 '14 at 8:07

What you could do is this:

Either alter the Submit/Cancel form directly or call hook_form_alter to add a hidden item

$destination = drupal_get_destination()
$form['destination'] = array(
  '#type' => 'hidden',
  '#value' => $destination['destination']
);

Then, you have two scenarios. If you control the form submission handler, you can simply do

function MY_MODULE_blah_blah_form_submit($form, &$form_state)
{
   ...
   $destination = urldecode($form_state['values']['destination']);
   $path_parts = explode('&', $destination);
   // The first path_part is your URL, the rest will make up the query_string.

   $url = array_shift($path_parts);
   $query = array();
   foreach($path_parts as $path_part)
   {
      list($key,$value) = explode('=', $path_part);
      $query[$key] = $value;
   }
   $form_state['redirect'] = array(
      $url,
      array(
          'query' => $query,
      ),
   );
}

Or create a second submission handler for the form, which will fire after this one, and implement the above yourself.

Some useful (and more detailed information): https://api.drupal.org/api/drupal/includes%21form.inc/function/drupal_redirect_form/7

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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