0

I have this very simple implementation of hook_node_access

function applx_node_access($node,$op,$account){
    drupal_set_message($node);
}

This returns

applx_application
forum
article
page
forum

And because of this i am not able to proceed further. I'm not sure why it prints the name of all bundles.

6

$node contains all the content-type in your application as value. So, you need to set your node access based on the $node value.

function hook_node_access($node, $op, $account) {
  $type = is_string($node) ? $node : $node->type;
  if($type == 'forum') {
    //set your permission based on $op value.
  }
}
  • Well, how do i go about figuring out which content-type a user is trying to access so i can set the node access? – Haider Ali Feb 15 '16 at 19:11
  • Oh i see, its like an iteration. Thanks a lot man ;) You're a life(time) saver :) – Haider Ali Feb 15 '16 at 19:15
5

It happens because the first parameter of hook_node_access() could be either:

  • A string containing the content type of the node
  • A node object

It is a string for the content type when a node doesn't exist yet, for example because it is going to be created.

In fact, the first line of node_node_access() (the implementation of that hook made from the Node module), is the following one.

$type = is_string($node) ? $node : $node->type;

Your code needs to check the type of the value passed as first argument, in a similar way.

The hook is invoked multiple times because Drupal is checking what content types should be shown in node/add for the currently logged-in user. In fact, your hook implementation is getting the machine names of the content type defined in that site.

Notice that, in Drupal 8, the first parameter of that hook is always an object implementing the NodeInterface interface, so it is not anymore necessary to check the type of the first parameter.

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.