4

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

Rambazamba
  • 127
  • 8

2 Answers2

1

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

Try testing anonymously or as another role.

  • It's also never called for anyone who has the "Bypass content access control" permission. – Dalin May 17 '21 at 15:35
0

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

drupal node:access:rebuild
guanxiaohua2k6
  • 371
  • 1
  • 5