Using named anchors with #redirect in forms

Working on a site using the jstabs module this evening I came across a bit of a challenge passing named anchors to the #redirect element of a form. The desired url for redirection in this case was /user/myuser#profile-tab-7.

Asking in #drupal led to the following tidbit from chx. (This is one of the many reasons Drupal is wonderful since Google searching didn't produce results and my experiments and requests of friends didn't produce the answer.) Anyway the challenge is that #redirect causes the form to call drupal_form_redirect() which in turn calls drupal_goto(). The drupal_goto() function takes the path, query and fragment as it's first three arguments.

drupal_goto($path = '', $query = NULL, $fragment = NULL, $http_response_code = 302)

How then to pass these from #redirect. Putting the values in a string doesn't cut it as the special characters end up getting encoded in the URL and it doesn't work. The answer chx pointed out is to use array() to pass the values. The resulting code ends up looking like this:

      $form['#redirect'] = array('user/' .$user->uid, NULL, 'tabs-profile-7 ');


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

This is good to know. I am

This is good to know. I am wondering if the Profesionall Drupal Developer book mentions this? I will have to check later... But thanks for explaining the Drupal process #redirect goes through.

Pro Drupal Development

As a matter of fact I just looked and Pro Drupal Development does mention this.... If only I'd remembered to look while I was hunting down the solution. PDD proves its great value once again :)

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Lines and paragraphs break automatically.
  • Use [# ...] to insert automatically numbered footnotes. Textile variant.
  • Use <fn>...</fn> to insert automatically numbered footnotes.
  • Allowed HTML tags: <a> <b> <dd> <dl> <dt> <i> <li> <ol> <u> <ul><p> <img> <table> <tr> <td><strong><em><sup><div><fn><h1><h2><h3><h4><blockquote><img style="">
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Syntax: [amazon title|cover|info asin] Example: [amazon cover B0007M123K]
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options