0

I've been struggling a lot with a drupal_goto() not redirecting from a view to correct path in all displays.

This is the snippet code.

/**
 * Implements hook_views_pre_render().
 */
function HOOK_views_pre_render(&$view) {
  //if only on node matches, redirects to that node 
  if ( $view->name == 'FOO' && $view->current_display == 'BAR' && count($view->result)==1 ){
    if(is_numeric($view->result[0]->nid)){
      drupal_goto('node/' . $view->result[0]->nid);
    } 
  }
}

Instead, header('Location:node/' . $view->result[0]->nid); is working in any case. Why doesn't drupal_goto() work, sometimes?

2
  • Do you have destination parameter in query? You can take a look into drupal_goto() and find that it uses header('Location... too.
    – kala4ek
    Jun 24, 2016 at 4:18
  • @kala4ek, thank you. Indeed this is exactly what I explained in my answer :). I posted it to share experience.
    – Kojo
    Jun 24, 2016 at 8:34

1 Answer 1

1

I had an exposed filter with identifier named destination. Since drupal_goto() takes care of the destination parameter sent in URL with the following code, it was overwriting my code.

  // A destination in $_GET always overrides the function arguments.
  // We do not allow absolute URLs to be passed via $_GET, as this can be an attack vector.
  if (isset($_GET['destination']) && !url_is_external($_GET['destination'])) {
    $destination = drupal_parse_url($_GET['destination']);
    $path = $destination['path'];
    $options['query'] = $destination['query'];
    $options['fragment'] = $destination['fragment'];
  }
1
  • I feel too stupid but I share it here, who knows, it could be helpful. Credits once again to @Clive
    – Kojo
    Jun 23, 2016 at 17:39

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.