Score:0

Allow user to control access to own content for other authenticated users

ro flag

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

?>

Kevin avatar
in flag
You aren't returning the result of your hook. That's one problem. Check the docs of the hook you are using.
drupalhorn avatar
ro flag
Thanks, return was missing, works now :-)
Score:1
cn flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.