1

I'm having a strange behaviour while trying to use hook_node_access in Drupal 8 (Lightning to be more precise) and I can't figure out why is doing this. Let's suppose that I want to always allow people to edit a certain content type (I know I can setup role permissions to achieve this, but I'm just trying to understand why it's not working):

function mymodule_node_access($node, $op, $account) {
  $type = $node->bundle();
  if($type == 'service' && $op == 'update') {
      return AccessResult::allowed();
  }
}

I always get "access denied" while trying to edit a "service" node.

This is what I've tried to do in order to debug it:

  • in reports > status reports there are no node access permissions in use;
  • the current user does not have "bypass content access control" permission;
  • hook_module_implements_alter doesn't return any other module using hook_node_access.

Is there anything I'm missing?

  • 2
    Because there is already an access forbidden result, this takes precedence over any access allowed result. – 4k4 May 22 at 14:34
  • I thought about this, but hook_module_implements_alter doesn't return any other module using hook_node_access. Is there any other way a module can deny update access for a node? – Stefan May 22 at 14:47
  • 1
    Arghh... it seems that I had to check for hook_entity_access as well. Indeed, content_moderation module is denying access, but it's using hook_entity_access instead of hook_node_access. Now it makes sense, thank you! – Stefan May 22 at 15:01
  • 3
    Check not only hooks, entities have access control handlers. – 4k4 May 22 at 15:10
3

Although I've checked for other modules using hook_node_access, I forgot to check for hook_entity_access as well. It turned out that the "content moderation" module is denying access. Thanks @4k4 for making me revisit this!

Just for the record, if you need to see which other modules are using a certain hook, you can use something like this:

function mymodule_module_implements_alter(&$implementations, $hook) {
  if($hook == 'node_access') {
    //do whatever you need to do (e.g. use kint($implementations) to see what other modules are using this hook)
  }
}

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.