4

I am using the following code in a Drupal 7 module.

define('BNA_ACCESS_REALM', 'billetsalget_nodeaccess');

function billetsalget_nodeaccess_node_access_records($node) {
  $producttypes = array('internal_product', 'external_product', 'call_product', 'event_product');
  $grants = array();

  if (in_array($node->type, $producttypes)) {
    $grants[] = array(
      'realm' => BNA_ACCESS_REALM,
      'gid' => $node->nid,
      'grant_view' => TRUE,
      'grant_update' => 0,
      'grant_delete' => 0,
      'priority' => 0,
    );
  }

  return !empty($grants) ? $grants : array();
}

function billetsalget_nodeaccess_node_grants($account, $op) {
  dpm($account);
  die(var_dump($account));
}

hook_node_access_records() is working and running as it should, adding data to the database.
As far as I can see hook_node_grants() is never invoked. I have tried with all sorts of stuff in there, but it just looks like it doesn't even run.


It didn't have anything to do with my code. I'm new to Drupal so I didn't know hook_node_grants() don't get invoked when I'm signed in as an administrator. Newbie mistake from my side :)

  • 1
    hook_node_grants() is invoked from these functions: api.drupal.org/api/drupal/modules%21node%21node.api.php/… if that helps – Clive Aug 21 '13 at 14:31
  • 1
    @Clive Especially part "node_access_rebuild in modules/node/node.module Rebuilds the node access database." - rebuilding should trigger it all right. Grants is not called on each access due to performance problems. martindilling have you tried to rebuild that database? – Mołot Aug 21 '13 at 14:46
  • Well... This is embarrassing xD This is my first project in Drupal so I didn't know that function don't run when signed in as an administrator -.-' So frustrating figuring that out after 2 days with that problem xD – martindilling Aug 21 '13 at 16:25
2

It didn't have anything to do with my code. I'm new to Drupal so I didn't know hook_node_grants() don't get invoked when I'm signed in as an administrator. Newbie mistake from my side :)

  • Ah yes - User 1 bypasses all checks that go though user_access() (it's hard-coded) so that explains it – Clive Aug 22 '13 at 12:58
  • Yea, really really annoying to find out that was the problem after 2 days :P but now it works as intended :D – martindilling Aug 22 '13 at 13:21
  • It's a mistake you only ever make once ;) – Clive Aug 22 '13 at 13:23

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.