I have two weird issues with hook_node_access().
I have created to modules, say module1 and module2. They both reside under site/all/modules/custom.
In module1 I have among other stuff:
function module1_node_access($view, $node) {
// code omitted
return false;
}
Note the parameters, $view and $node; I tried first with $node, $op, $account, but it dies with a "missing argument 3 for module1_node_access". Besides that, it works, and setting the return value to false produces a Drupal "not authorized" message as expected.
Now, in module2, I am able to use the hook_node_access parameters as expected according to the API:
function module2_node_access($node, $op, $account) {
// code omitted
echo "module2_node_access()";
return false;
}
But the return value is completely ignored. I know the module2_node_access function is being called.
I have tried to return NODE_ACCESS_DENY instead with no change.
Any clues? Thanks.
Comments
Why are you echoing the
Why are you echoing the function name? Don't you want to call the function?
Contact me to contract me for D7 -> D10/11 migrations.
Just to show we're being called
Don't worry about te echo. It's just to show that we're inside the function/we're being called.
I don't think you will see
I don't think you will see the echo in a function like node_access
if you really want to see if it works, you could use
drupal_set_message('I got called in hook_node_access');if it doesn't work, you could try clearing your cache
You're right about that...
Actually I use drupal_debug(...) which produces the expected output. The echo was just for the (bad) example.
I've cleared the cache several times. It's not that.
It could be 2 things I can
It could be 2 things I can think off.
Either your module name or your hook name have a spelling error.
Or you are logged in as superadmin(user id: 1) which overrides all access control.
If you are logged in with your admin account, try using a normal created account.
No spelling errors
Thank you for your suggestions. However, there are no spelling errors and the hooks are being called but in module1's case with incorrect number of parameters.
Also, I am logged as a regular user, not as admin, which is seen by the fact that a "return false" in module1_node_access(...) correctly forces Drupal to display a "unauthorized"-page.
Can you post the exact code
Can you post the exact code you are using for hook_node_access, maybe it will help solve te problem.
The Code
I appreciate the feedback. However, the warning happens when coursepermission_node_access(...) is being called by core.
An interesting thing is that I disabled "module1" that had the problem with being called by wrong number of paramters, and now module2 (coursepermission in the following) complains about the same thing.
Instead of being called with the paramters ($node, $op, $account), I get ($view, $node).
Anyway, here is the code:
This results in the following:
Being called by _menu_check_access
Just a little bonus info: My coursepermission_node_access(...) is being called by _menu_check_access(...). I stepped through the code to see this.
Maybe it has to do with if($callback == 'user_access') stuff, that appears to call a module hook with two parameters...?
A workaround
This workaround inserted at the beginning of the function appears to work:
I'm beginning to suspect this has to do with my theme, which is created by the Artisteer product.
Your problem is probably
Your problem is probably coming from your hook_form_alter(). Hooks are called automatically, and you have hard-coded yours in. This means it's probably not getting the parameters the function expects, since it's not being called in the place it's meant to be called.
Contact me to contract me for D7 -> D10/11 migrations.
No hooks are being called manually
Thanks for taking time to commenting on this.
However, I'm not calling any hooks manually. The code above is taken from core.
A suggestion
Hello,
Sorry to interrupt :) . You don't need the following:
I believe that is what Jaypan is referring too.
The main purpose of the Drupal's "menu" system is to map requests to code that services the request with a response. The "hook" system doesn't require you to map a url/path before the implemented "hook" is called.
The errors you were getting were likely due to that fact that you overrode a request path with a "named" wildcard argument.
Thanks, goofus
Thank you, goofus, for clarifying on this. I'll will check it out. :)