1

I following topic https://atendesigngroup.com/blog/custom-permissions-node-access-grants-drupal-8-and-drupal-7 to create custom permissions for node. It's working ok. Example I can edited node 1 with url: example.com/node/1/edit/. I wan't check user have permission edit node by code eg:

node_access('view', $node);

and Drupal 8

$node = Node::load(1);
$account = User::load(1);
$check = $node->access('edit', $account);

But It don't working. So how can check permission of special node?

2
  • See \Drupal\node\NodeAccessControlHandler::access for how the access check is actually doing it.
    – user21641
    Oct 1, 2017 at 10:09
  • The operations are view, update, delete (and create but usually don't have an entity yet then), not edit. But how do you know it's working when it is apparently not?
    – Berdir
    Oct 1, 2017 at 13:05

2 Answers 2

1

Just tested this and $node->access('update'); works for me. I let the optional $account parameter out so that it would check the currently logged in user.

The only difference was that I loaded my node with:

\Drupal\node\Entity\Node::load($nid);

To just use Node::load($nid); Make sure to include:

use Drupal\node\Entity\Node;

at the top of your file.

0

As Rubix05 mentions, the issue is that you are testing "edit" access when you should be testing "update" access.

$node = Node::load(1);
$account = User::load(1);
$check = $node->access('update', $account);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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