12

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':      
      drupal_goto("products#".$node->nid);
  }
}

4 Answers 4

18

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));
1
  • for D7 example, you can use any parameter that url() function supports... such as URL params.
    – AKS
    Sep 11, 2012 at 5:55
7

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.

2

This also worked for me in Drupal 7

 drupal_goto( '/products/' . 'section', array( 'fragment' =>  'subsection', 'alias' => TRUE ) );
0

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 
$options['fragment']='';

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.