1

I feel like asking a silly question, but here it is anyway: I need to test if a given user (in my case, the logged in, current user) has access to a given node for one of the existing operations (view, create, edit or delete).

In Drupal 7 you could test this by using node_access function (https://api.drupal.org/api/drupal/modules!node!node.module/function/node_access/7.x) and it would be as simples as this:

if(node_access('view', $node, $account)) {
  // Perform something
}

Return value would be TRUE or FALSE. How can I achieve the same result on 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!

3

You want Node::access():

if ($node->access('view', \Drupal::currentUser())) {
  // User is allowed to view the node.
}
  • Thank you, @Jaypan, this is exactly what I needed! – Marcos Buarque Dec 9 '19 at 2:43
  • Glad to have helped :) – Jaypan Dec 9 '19 at 2:43

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.