function NodeHooks1::nodeAccess

Implements hook_ENTITY_TYPE_access().

File

core/modules/node/src/Hook/NodeHooks1.php, line 363

Class

NodeHooks1
Hook implementations for node.

Namespace

Drupal\node\Hook

Code

public function nodeAccess(NodeInterface $node, $operation, AccountInterface $account) {
    $type = $node->bundle();
    // Note create access is handled by hook_ENTITY_TYPE_create_access().
    switch ($operation) {
        case 'update':
            $access = AccessResult::allowedIfHasPermission($account, 'edit any ' . $type . ' content');
            if (!$access->isAllowed() && $account->hasPermission('edit own ' . $type . ' content')) {
                $access = $access->orIf(AccessResult::allowedIf($account->id() == $node->getOwnerId())
                    ->cachePerUser()
                    ->addCacheableDependency($node));
            }
            break;
        case 'delete':
            $access = AccessResult::allowedIfHasPermission($account, 'delete any ' . $type . ' content');
            if (!$access->isAllowed() && $account->hasPermission('delete own ' . $type . ' content')) {
                $access = $access->orIf(AccessResult::allowedIf($account->id() == $node->getOwnerId()))
                    ->cachePerUser()
                    ->addCacheableDependency($node);
            }
            break;
        default:
            $access = AccessResult::neutral();
    }
    return $access;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.