2

I want anonymous users to be able to access nodes that are promoted to the frontpage (they can't access to nodes that are not promoted) and hook_node_access does not seems to be called for anonymous users. So I tried a combination of hook_node_grants and hook_node_access_records to do it, but it doesn't seem to be working.

Here is the code I tried:

function myModule_node_grants(\Drupal\Core\Session\AccountInterface $account, $op) {

   return ['show_promoted_news' => [9999]];
}

function myModule_node_access_records(\Drupal\node\NodeInterface $node) {

  $grants = [];

  if ($node->isPublished() and $node->isPromoted() and $node->getType() == 'article') {

    $grants[] = [
       'realm'        => 'show_promoted_news',
       'gid'          => 9999,
        'grant_view'   => 1,
        'grant_update' => 0,
        'grant_delete' => 0,
        'langcode'     => 'en'
    ];
  }

  return $grants;
} 

Note: it seems the permission "access content" is overriding what I set here (anonymous users can't access published content) and adding a greater priority does nothing. I'm being forced to allow anonymous users to access content and overriding that when I don't want them to have access with hook_node_access().

  • it seems the permission "access content" is overriding what i set here (anonymous users can't access published content) and adding a greater priority does nothing. i'm being forced to allow anonymous users to access content and overriding that when i don't want them to have access with hook_node_access(). – José Trindade Jul 27 '17 at 14:40
0

You might get this to work by using the Group module. Below are some details about how to do so.

1. Configuration

  • Create a Group Type (pick whatever name you want).
  • For this case, there is no need for special (custom) Group Roles for the Group Type you created. Since you'll be able to configure what you want by using these Special Global Roles (hardcoded) that come with the Group module (they cannot be defined by the Drupal administrator, and they are always available for any Group Type):

    • "Outsider": a signed in user that did not join a group.
    • "Member": a signed in user that did join a group.
    • "Anonymous": a user that is not signed in.
  • Create a Group (pick whatever name you want) for the Group Type you created, and let's say you name it "Promoted nodes" (feel free to use another name if you prefer).

  • Grant the appropriate Group Permissions for the configured Group Type. To do so, you can also configure for each Content Type which Group Role has what kind of access (= none, view, create, edit, delete). Be aware: even though this looks similar to Drupal's permission configuration, these Group Permissions are not the same permissions (they are specific to the Group module). With the above setup in place, just grant "view" access to the Special Global Role named Anonymous (and pick whatever rights you want for the other roles).

2. Granting access

With the above Group module configuration in place, all you need to do is:

  • assign each promoted node to the Group "Promoted nodes".
  • remove the node from the group again if it is no longer promoted.

For sure this assign/remove can be done manually (whenever a node is edited), but it should be straight forward to somehow automate it using "promoted or not" as the trigger to do so (using the API that comes with the Group module).

More info

Refer to "How to create collections of content that can be accessed by users with various access levels?" for more info about all this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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