0

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();
        }
    }
    
}

?>

2
  • 2
    You aren't returning the result of your hook. That's one problem. Check the docs of the hook you are using.
    – Kevin
    Commented Nov 6, 2021 at 18:22
  • Thanks, return was missing, works now :-)
    – drupalhorn
    Commented Nov 7, 2021 at 12:49

1 Answer 1

1

The Rabbit Hole module allows you to specify if users can view the node. The first setting is for the content type, but there is a setting there to allow that to be set on a per node basis. You can look at using that module, or look at what they're doing to solve this.

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.