25
votes

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

4

4 Answers

34
votes

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

27
votes

For Drupal 8

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
// then
throw new NotFoundHttpException();
7
votes

MENU_NOT_FOUND should be returned in page callback functions.

Page callback functions wanting to report a "page not found" message should return MENU_NOT_FOUND instead of calling drupal_not_found(). — http://api.drupal.org/api/drupal/includes!common.inc/function/drupal_not_found/7

2
votes

Look into the drupal_add_http_header() function to play with the HTTP header attributes. Also make sure you stick this at the top of your module's code to make sure it executes first. Also, you might find this helpful. https://www.drupal.org/project/generate_errors