How to throw 404 error in Drupal module?

webbear picture webbear · Jun 22, 2012 · Viewed 18.6k times · Source

I need to throw 404 error in module. Or may be there are any possibility to set required option for menu hook?

Answer

Ayesh K picture Ayesh K · Jun 22, 2012

It is easy. These should take care of watchdog, HTTP 404 response code and other related things.

For Drupal 6 & 7

In your module's page callback, do: return drupal_not_found();

For Drupal 8

In the class::method() referred to in the _controller definition (i.e. the page callback or the method responsible for generating output for the request), do:

throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();

References