8.4.x node.api.php hook_node_access(\Drupal\node\NodeInterface $node, $op, \Drupal\Core\Session\AccountInterface $account)
8.0.x node.api.php hook_node_access(\Drupal\node\NodeInterface $node, $op, \Drupal\Core\Session\AccountInterface $account)
8.1.x node.api.php hook_node_access(\Drupal\node\NodeInterface $node, $op, \Drupal\Core\Session\AccountInterface $account)
8.2.x node.api.php hook_node_access(\Drupal\node\NodeInterface $node, $op, \Drupal\Core\Session\AccountInterface $account)
8.3.x node.api.php hook_node_access(\Drupal\node\NodeInterface $node, $op, \Drupal\Core\Session\AccountInterface $account)
8.5.x node.api.php hook_node_access(\Drupal\node\NodeInterface $node, $op, \Drupal\Core\Session\AccountInterface $account)
7.x node.api.php hook_node_access($node, $op, $account)

Controls access to a node.

Modules may implement this hook if they want to have a say in whether or not a given user has access to perform a given operation on a node.

The administrative account (user ID #1) always passes any access check, so this hook is not called in that case. Users with the "bypass node access" permission may always view and edit content through the administrative interface.

Note that not all modules will want to influence access on all node types. If your module does not want to explicitly allow or forbid access, return an AccessResultInterface object with neither isAllowed() nor isForbidden() equaling TRUE. Blindly returning an object with isForbidden() equaling TRUE will break other node access modules.

Also note that this function isn't called for node listings (e.g., RSS feeds, the default home page at path 'node', a recent content block, etc.) See Node access rights for a full explanation.


\Drupal\node\NodeInterface|string $node: Either a node entity or the machine name of the content type on which to perform the access check.

string $op: The operation to be performed. Possible values:

  • "create"
  • "delete"
  • "update"
  • "view"

\Drupal\Core\Session\AccountInterface $account: The user object to perform the access check operation on.

Return value

\Drupal\Core\Access\AccessResultInterface The access result.

Related topics

4 functions implement hook_node_access()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

book_breadcrumb_test_node_access in core/modules/book/tests/modules/book_breadcrumb_test/book_breadcrumb_test.module
Implements hook_node_access().
content_moderation_node_access in core/modules/content_moderation/content_moderation.module
Implements hook_node_access().
node_access_test_node_access in core/modules/node/tests/modules/node_access_test/node_access_test.module
Implements hook_node_access().
node_node_access in core/modules/node/node.module
Implements hook_node_access().


core/modules/node/node.api.php, line 327
Hooks specific to the Node module.


function hook_node_access(\Drupal\node\NodeInterface $node, $op, \Drupal\Core\Session\AccountInterface $account) {
  $type = $node->bundle();

  switch ($op) {
    case 'create':
      return AccessResult::allowedIfHasPermission($account, 'create ' . $type . ' content');

    case 'update':
      if ($account->hasPermission('edit any ' . $type . ' content', $account)) {
        return AccessResult::allowed()->cachePerPermissions();
      else {
        return AccessResult::allowedIf($account->hasPermission('edit own ' . $type . ' content', $account) && ($account->id() == $node->getOwnerId()))->cachePerPermissions()->cachePerUser()->addCacheableDependency($node);

    case 'delete':
      if ($account->hasPermission('delete any ' . $type . ' content', $account)) {
        return AccessResult::allowed()->cachePerPermissions();
      else {
        return AccessResult::allowedIf($account->hasPermission('delete own ' . $type . ' content', $account) && ($account->id() == $node->getOwnerId()))->cachePerPermissions()->cachePerUser()->addCacheableDependency($node);

      // No opinion.
      return AccessResult::neutral();