Rambazamba
Rambazamba

Reputation: 127

Drupal8 module development: hook_node_access not called

for my current site I have a content type that stores a user in an entity reference field. I want to allow the user referenced in this field to edit the data of that node. In drupal7 I would do that with a node_access hook - which I am not getting to work in drupal8.

Here is a demo code of my hook - for testing purposes I want to forbid everything. However it is never called, and no - I am not logged in as user1. Also reseted cache, uninstalled and installed the module again and rebuilt the permissions - nothing seems to make this hook work.

function mymodule_node_access(\Drupal\node\NodeInterface $node, $op, \Drupal\Core\Session\AccountInterface $account) {
    $access = new AccessResultForbidden();
    return $access;
}

My question is why is this hook never called - and if there is now with drupal 8 a better way to do so? Any help will be appreciated.

Best regards, Rambazamba

Upvotes: 4

Views: 1338

Answers (2)

This hook is never called for user 1, since he bypasses every access control.

Try testing anonymously or as another role.

Upvotes: 1

guanxiaohua2k6
guanxiaohua2k6

Reputation: 371

Try the following command. The tool 'drupal console' is required.

drupal node:access:rebuild

Upvotes: 0

Related Questions