The documentation for hook_node_access() states that the first argument $node
can be NodeInterface
object or a string:
\Drupal\node\NodeInterface|string $node: Either a node entity or the machine name of the content type on which to perform the access check.
If this is correct then the function stub provided in node.api.php is incorrect, because it accepts only a NodeInterface argument and will throw an exception if a string is passed:
function hook_node_access(\Drupal\node\NodeInterface $node, $op, \Drupal\Core\Session\AccountInterface $account) {
$type = $node
->bundle();
// ...
}
This should be changed to check whether $node is an instanceof NodeInterface, and then determine $type accordingly.
Alternatively, if it can only ever be NodeInterface then the documentation is wrong and the part about it being a string should be removed.
I'm happy to make this change but I do not know what the real behaviour is. Can hook_node_access() really be invoked with a string argument? All the contrib modules I have looked at assume that $node will be a Node object because they have copied the example code.