I can’t seem to figure out how to implement hook_menu_alter() to control access to a node. I’ve started with a bare Drupal 7 install, created two nodes, and created a module with only this code:
function mymodule_menu_alter(&$items){
$items['node/2']['access callback'] = TRUE;
}
This should simulate an actual callback function returning TRUE, which is where I started off. But I’m trying to show the simplest possible case, here.
If I set the access callback to “FALSE”, it works as expected: I get “access denied” on node/2. But if I set it to TRUE, shouldn’t I just get normal access to the node? Instead, when I go to node/2, I get a page similar to (but not exactly the same as) the default front page: a list of node teasers (In this case, showing the two nodes I created).
I have cleared the cache (because I know hook_menu_alter() isn’t called on every page view, but IS when the cache is cleared). I’ve also rebuilt permissions, to no avail. I’m sure I’m missing something dumb here, but I just can’t think of it.
I also tried it with an actual callback:
function mymodule_menu_alter(&$items){
$items['node/2']['access callback'] = 'mymodule_access_check';
}
function mymodule_access_check() {
return TRUE;
}
node/2 isn’t a menu item. The menu item for a node is actually node/%, so to change it’s callback you would have to do this:
Note I added $op (which will be ‘view’) and $node (which will be loaded node object) to the functions args because they will be passed to it by ‘access arguments’, so in your mymodule_access_check function you have access to the node information.