2

In Drupal 8, how do I give access to some nodes to users with specific roles, depending on a field value or a taxonomy value?

1
  • 1
    You will need to use a hook to override the access class of nodes. And provide your own class.
    – Eyal
    Apr 10, 2016 at 19:19

1 Answer 1

1

this is how i did it ( simplified for demo purpose )

function my_module_node_access(NodeInterface $node, $op, AccountInterface $account) {

global $user;
$user = \Drupal\user\Entity\User::load($account->id());

  if ($node->bundle() == 'my_content_type') {
    if ($op == 'view') {
        $visibility=$node->field_visibilite->getString();
        $target=$node->field_target->getString();


        if ($visibility ==='all')
            return \Drupal\Core\Access\AccessResult::allowed();

        if ($visibility ==='restricted') 
            if (($target === 'group1') && ($user->hasRole('group1')))
                return \Drupal\Core\Access\AccessResult::allowed();


}

 return \Drupal\Core\Access\AccessResult::forbidden();

}

Your Answer

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

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