0

I want to use hook_node_view to stop a node from loading if it was not created by the user trying to access it. What should I include in the following code to kill the loading of the node?

function hook_node_view($node, $view_mode, $langcode) {

    global $user;
    $currUser = $user->uid;
    $nodeUser = $node->uid;

    if ($currUser != $nodeUser){    
        drupal_set_message('kill page');
  }
}
2

You can't prevent a node from loading but you can deny/grant access to it. Write a custom module and implement hook_node_grants($account, $op) && hook_node_access_records($node). Here is a good example.

or, you can implement hook_node_access($node, $op, $account) but this hook will not be invoked for node listings (e.g., RSS feeds, the default home page at path 'node', a recent content block, etc.)

4
  • is it possible to run a line of code in the if statement that terminates the load or custom renders the node to display nothing?
    – Emanegux
    Jun 23 '14 at 15:37
  • No, It is not the right place to do it, if you are not comfortable in implementing the hooks that I mentioned then you better look at @Gokul's answer mentioning Content Access Module
    – wolverine
    Jun 23 '14 at 15:43
  • can you explain why it is not the right place to do it or what best practice you are referring to? It seems inefficient to have to register two new hooks just to kill a node load
    – Emanegux
    Jun 23 '14 at 15:49
  • hook_node_view() is for adding your content to already built node, it is part of rendering process. Drupal has separate hooks for performing access control and should use those. If you don't want to use two hooks then use hook_node_access().
    – wolverine
    Jun 23 '14 at 16:05
1

It would be better to take a look at the Content Access Module

As the project page reads,

This module allows you to manage permissions for content types by role and author. It allows you to specifiy custom view, edit and delete permissions for each content type. Optionally you can enable per content access settings, so you can customize the access for each content node.

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.