4

I am trying to use hook_node_access to change some permissions when viewing a node type. The problem is that even when I call it in my module, it doesn't work.

function MyModule_node_access($node,$op,$account) {
  die("it works!");
}

I can 100% confirm that other hooks in the same module are working fine. I have disabled and reenabled the module. I have cleared all cache. I can confirm that my test user is not uid = 1, and it does not have the "Bypass content access control" permission. So why is hook_node_access not working for me? Does it not work by itself?

6

node_access is a hook like any other, so I can see no reason why it wouldn't be called if:

  • your hook implementation is named correctly
  • the module is activated
  • the file the hook implementation is in has been included
  • the 'module_implements' cache has been cleared
  • the page you are viewing is calling node_access() at some point (try a node/xxx page for example.)

You might try executing module_implements('node_access') and checking if your module is included in that list.

If so, node_access is not being called.

If not, the function YourModule_node_access does not exist, or is not included.

2
  • BINGO! You said "try a node/xxx page for example.". I was trying to view revisions (/node/XXX/revisions/XXX/view) for a particular node. Using "/node/XXX" works! THANK YOU! I also found there is a separate hook for revisions access which I will look into since I will need to override that as well. THANK YOU penten! – user785179 Jan 25 '13 at 17:30
  • I got the problem, I am viewing a node and module_implements('node_access') returns my module implementation, viewing a node and node.module enabled, why wouldn't that work ? – B2F Jul 11 '13 at 10:52
22

I used hook_node_access() and had the same issue: It wasn't firing and driving me nuts.

What worked for me was NOT to be logged in as an admin. I went in as a different role and all worked as expected.

0
6

From hook_node_access

The administrative account (user ID #1) always passes any access check, so this hook is not called in that case. Users with the "bypass node access" permission may always view and edit content through the administrative interface.

1
  • 1
    as @Jonathan Jae Rosero said, you can't be admin and expect it to work out... I would say, it's missing that fact in the documentation, where they say "user ID #1" I guess they mean, 'user admin role' – Dazag Mar 12 '19 at 11:14

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.