I just want a field in a content type, so that when a user creates new content he/she can select whether that content can be viewed by other authenticated users or not. I prefer doing this with a selection field displaying 'hidden/visible' to other users and then set the actual access in a custom module using some hook.
This is what I tried so far in my custom module hook entity access with content type test_adgang and checking field_test_adgang, but no effect, meaning all authenticated users can still see, if field set to 'skjult' (hidden).
<?php
use Drupal\Core\Access\AccessResult;
function import_functions_entity_access(\Drupal\Core\Entity\EntityInterface $entity, $operation, \Drupal\Core\Session\AccountInterface $account) {
if ($entity->bundle() == 'test_adgang') {
if ($entity->field_test_adgang->value == 'skjult' && $operation == 'view' && in_array('godkendt bruger', $account->getRoles())) {
return AccessResult::forbidden();
} elseif ($entity->field_test_adgang->value == 'synlig' && $operation == 'view' && in_array('godkendt bruger', $account->getRoles())) {
return AccessResult::allowed();
}
}
}
?>