2

I need to use an alternative function to replace drupal_goto(). The reason is that this function is causing me a problem to run cron.php. The code is:

    if(is_int($order_id)) {
$path = 'admin/commerce/orders/' . $order_id;
drupal_goto($path);
}
else {
  drupal_goto('admin/commerce/orders/');
}
  • How is it causing cron problems? While @clive's solution looks good, I don't see why it would cause you less cron problems, as the difference is rather small. Perhaps your question should focus on the cron problem instead? – Letharion Mar 12 '12 at 18:16
  • I came into this by the Berdir's answer in this question: drupal.stackexchange.com/questions/25025/… – chefnelone Mar 12 '12 at 18:35
7

The problem is not drupal_goto() but your usage of it in a node. THere is a long standing issue in Drupal core to deal with this but I honestly think doing this is just wrong.

Instead of putting it in a node, you should create a custom module and put it in a page callback. See What are the downsides of using 'custom' PHP code in blocks, nodes, views-args, etc? why it's a bad idea in general to use PHP in nodes, blocks and so on. You've seen one of those yourself, namely that it can be extremely hard to find out where the problematic code is actually coming from.

That said, the last comment in the linked issue points to a Field redirection module that is however currently limited to user/node references and links. And it's not dynamic as your code is. But again, for something like that you really should be creating a custom module.

  • If so, then show your complete function/file, there must be a reason why it's run during a search index/cron run. – Berdir Mar 12 '12 at 20:06
  • 2
    +1 , Need to avoid using PHP code in node ,echo & print are acceptable but this type of code will some day give trouble, and you only get this in error logs Fatal error in eval d code – GoodSp33d Mar 13 '12 at 5:17
  • There was a mistake in the function. Now I don't have problem running cron.php. Thanks. – chefnelone Mar 14 '12 at 15:05
3

I'm not sure why you're having problems with drupal_goto(), but if you need a no-frills alternative, some old school PHP would do it:

function my_goto($path) {
  header('Location: ' . $path, TRUE);
  exit;
}

This doesn't differ very much from the drupal_goto() function itself, except that it doesn't set a response code, and it pays no attention to any destination you have set in the query string (which drupal_goto() does).

  • 1
    your answer was also useful in a unexpected way : while I was wondering myself why in some cases drupal_goto('node/' . $view->result[0]->nid) were redirecting to a mysterious /All url.... instead of correct path, I realised I had a view exposed filter named destination with default set as All ...!!!! Grmfff... ;) – Kojo Jun 23 '16 at 16:56

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.