1

I need to test if a given user (in my case, the currently logged-in user) has access to a given node for one of the existing operations.

In Drupal 7, I could use the following code.

if (node_access('view', $node, $account)) {
  // User is allowed to see the node.
}

What is the equivalent code for Drupal 8?

I will be running this code from inside a _custom_access check for a specific route I am setting for my custom module.

Thank you!

1 Answer 1

3

You want Node::access():

if ($node->access('view', \Drupal::currentUser())) {
  // User is allowed to view the node.
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.