Drupal 7 – Set Query Strings in the path for form_state[‘redirect’]

Previously we talked about adding query strings for drupal_goto().
Drupal – Escape the Hex Characters in drupal_goto()

When the form is submitted, we can decide the redirect path in the form_submit() function.
Redirect to node/1 after form submission

function <form id>_submit($form, &$form_state) {
  $form_state['redirect'] = 'node/1'
}

 

But if you want to redirect with query strings, the following piece of code will NOT WORK.

function <form id>_submit($form, &$form_state) {
  $form_state['redirect'] = 'node/1?abc=1&def=2#ghk'
}

 

You will be redirected to node/1%3Fabc%3D1%26def%3D2%23ghk. Here is the CORRECT way to add query strings in the redirect path.

function <form id>_submit($form, &$form_state) {
  $form_state['redirect'] = array(
    'node/1',
    array(
      'query' => array(
        'abc' => '1',
        'def' => '2'
      ),
      'fragment' => 'ghk',
    ),
  );
}

 

Done =)

Reference: Drupal forum – Redirecting with query string

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s