1

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.

3 Answers 3

2

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.

1
  • 1
    This has worked, views now recognizes the query string and the exposed filters are applied. Thank you
    – davewilly
    Jul 8, 2014 at 8:07
1

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

0

From personal experience: when debugging destinations, make sure you look at any use of the 'Rules' module that might be overriding your edits.

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.