I was working on a big website with many contrib and custom modules. And I had to debug a very annoying redirect that started happening sometime in the recent past, not sure when. Some pages simply just redirected to other URLs.
I figured out that the problem was one of a 301 redirect. My browser told me that at least. But good luck figuring out where in the code I can find the culprit. Xdebug breakpoints everywhere but to no avail. A search for
drupal_goto in the custom modules directory didn't help either, and God be with anyone trying to search through a contrib folder of that size.
Then it hit me. Isn't there a hook invoked inside
drupal_goto? At this point I was assuming (and hoping really) that the redirect was happening somehow with a
drupal_goto. And it turns out there is one: hook_drupal_goto_alter.
Armed with a new dose of hope, I implemented the hook and cleared the cache. Inside, I added the obligatory
$test = ''; statement and put a breakpoint on it. Let's see what happens. After loading one of the offending pages, the breakpoint halted the execution and the Xdebug call stack in my PHPStorm immediately pointed out the problem: Global Redirect. There was some URL rewriting happening on the site so GR got a bit confused and was redirecting back to the original path. The details of the issue are however not important.
My point is that using this hook, I could see exactly who and why was calling
drupal_goto. I didn't use it for anything else, apart from learning why the redirect is happening which in turn allowed me to write some code that prevented that.
Awesome. I learned about a new hook. And maybe now you as well.
Danny founded WEBOMELETTE in 2012 as a passion project, mostly writing about Drupal problems he faced day to day, as well as about new technologies and things that he thought other developers would find useful. Now he now manages a team of developers and designers, delivering quality products that make businesses successful.