0

I've written code for a custom module in my .MODULE file that looks like this.

function cookie_authentication_node_access(NodeInterface $node, $op, AccountInterface $account) {
  if ($account->isAnonymous() && $node->gettype() == 'page') {
    return AccessResult::forbidden()->cachePerPermissions();
  }

return AccessResult::neutral(); 

}

Basically, using an EventSubscriber, I want to create an exception for my own rule I made above (letting anonymous users access a certain page)...

Would the 'EXCEPTION' KernelEvent be the right path, or is it only for real uncaught exceptions?

  • These are two different exceptions. What you want to do is to add an exception to the logic expression, for example && $node->id() != 123. The event is part of the PHP error handling, see php.net/manual/en/language.exceptions.php – 4k4 Oct 18 '19 at 15:58
  • Thanks. I'm also trying to redirect the anonymous user from an access form. For the access form, I built a config form for the password, but the actual access form has this code: drupal.stackexchange.com/questions/287398/… How would I go about referencing this in my .MODULE file (or maybe I need to make an EventSubscriber for this?) – yogurtexpress Oct 18 '19 at 17:58
  • The answer there is correct, you need to set a session value, a cookie doesn't work in Drupal. When you then use this session value here, add this cache context: ->addCacheContexts(['session']) – 4k4 Oct 18 '19 at 18:40

Your Answer

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

Browse other questions tagged or ask your own question.