0

I need a quick solution to restrict access to nodes based on the contents of field. I've built the skeleton of a module and managed to restrict access to a custom route. Now, the service that redirects to access check is this:

services:
  access_check.untitled_check:
    class: Drupal\untitled\Access\UntitledAccessCheck
    tags:
      - { name: access_check, applies_to: _access_untitled_check }

This only applies to routes with _access_untitled_check in the requirements array.

How can I have all nodes checked? Can I add _access_untitled_check to nodes' requirements array?

3

There are several ways to do alter or hook into node access. You can find out more by reading the Node Access Rights page on api.drupal.org.

Briefly node access works like this (paraphrasing from the aforementioned documentation page):

  1. Users with 'bypass node access' are granted access.
  2. Invoke implementations of hook_node_access
  3. Invoke implementations of hook_node_grants

Finally,

To see how to write a node access module of your own, see node_access_example.module.

1
  • Ok, thank you for pointing me in the right direction. – Angus Mar 13 '16 at 18:33
0

This is the solution I found. No routes, no services, no controllers. Only hook_node_access as advised by @mradcliffe

function modulename_node_access(NodeInterface $node, $op, AccountInterface $account) {
  $bundle = $node->bundle();
  $user = $account->id();
  $refs = $node->field_with_references->referencedEntities();

  $condition = ... 

  return AccessResult::forbiddenIf( $condition );
}

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.