Is there any way to include a # in drupal_goto?

i want something like this

function MYMODULE_preprocess_node(&$variables) {
  $node = $variables['node'];
  switch ($node->type) {
    case 'product':      

For Drupal 6,

It should be 3rd parameter

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

drupal_goto("products", NULL, $node->nid);

For Drupal 7

drupal_goto($path = '', array $options = array(), $http_response_code = 302)
drupal_goto("products", array('fragment' => $node->nid));
| improve this answer | |
  • for D7 example, you can use any parameter that url() function supports... such as URL params. – AyeshK Sep 11 '12 at 5:55

In Drupal 6 drupal_goto use its third parameter for the fragmentation. If you wants to make the url like products#345 you should pass its fragment as third argument in drupal_goto function.

drupal_goto("products", NULL, $node->nid); // where $node->nid is the fragment.

In Drupal 7 you should pass fragment as key value pair of array on second parameter of drupal_goto function.

drupal_goto('products', array('fragment' => $node->nid)) ; // where $node->nid is the fragment.

Both of above will produce an url like products#123, where 123 is the value of $node->nid variable.

| improve this answer | |

This also worked for me in Drupal 7

 drupal_goto( '/products/' . 'section', array( 'fragment' =>  'subsection', 'alias' => TRUE ) );
| improve this answer | |

drupal_goto loses #zzz if there is a ?destination=foobar#zzz, as this is never returned by $_SERVER, remove this line as does nothing apart from breaking stuff:

//$options['fragment'] = $destination['fragment'];  // removed
is the same as 
| improve this answer | |

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.