404 error from node access hook I have node_access hook in a module. I tried to prevent anonymous user from access unpublished node like the following:

function MODULE_NAME_node_access($node, $op, $account) {
   global $user; 
if ($op == 'view' &amp;&amp; $node-&gt;status == 0 ){
     return NODE_ACCESS_DENY;
  }  
}


This is working fine and redirect to access denied custom page. However, I would like it to be redirect to the custom 404 not found page. i.e the http response to be 404. I tried to find any replacement of NODE_ACCESS_DENY but I could not able to find anything.

I tried to use drupal_not_found() but it generates Server Error on the server.

Also, I would like to know, if there a method or property of the $user object that determine if the user is anonymous user or not. I find that there is user_is_anonymous() but I could not know how to use it in this context.
You can use drupal_goto("custom 404 page") to redirect a user to your custom page. For your second question you can check if user is anonymous using this condition. if($user-&gt;uid == 0)

Thus your hook can be rewritten like this. 

function MODULE_NAME_node_access($node, $op, $account) {
  global $user;
  if ($op == 'view' &amp;&amp; $node-&gt;status == 0 &amp;&amp; $user-&gt;uid == 0){
    drupal_goto("custom_404_page");
   }
}


See here for drupal_goto documentation 

Note: This function has been removed in Drupal 8. In Drupal 8 you can do this by this way. 

return new RedirectResponse(\Drupal::url('user.page'));


or this way 
`use Drupal\Core\Controller\ControllerBase;

class MyControllerClass extends ControllerBase {
   public function foo() {
   //...
   return $this-&gt;redirect('user.page');
  }
}`


See here for Drupal 8 redirect info