Skip to main content
Category:

Code snippet that can be used to check if user has permission to special node in Drupal 8.

Example check if current user has permission to update the node:

 $node = \Drupal\node\Entity\Node::load(NID);
  $user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
  $check = $node->access('update', $user);

Example check if current user has permission to view the node:

$node = \Drupal\node\Entity\Node::load(NID);
$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
$check = $node->access('view', $user);

Example check if current user has permission to create the node:

$node = \Drupal\node\Entity\Node::load(NID);
$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
$check = $node->access('create', $user);

 

Example check if current user has permission to delete the node:

$node = \Drupal\node\Entity\Node::load(NID);
$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
$check = $node->access('delete', $user);

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook