2

Here is my implementation of hook_node_grants:

/**
 * Implements hook_node_grants().
 */
function gcac_node_grants($account, $op) {

    // Declare $user as having global scope.
    global $user;

    // Permit a user to view the node if the node and user are assigned the same 
    // group.
    ** if ($user->field_group[LANGUAGE_NONE][0]['value'] == $node->group && $op == 'view') {
        $grants['gcac_view'] = GCAC_ALLOW;
    }

    // Permit the user to edit the node if the node and user are assigned the same 
    // group, and the user is the author.
    ** if ($user->field_group[LANGUAGE_NONE][0]['value'] == $node->group && $user->uid == $node->uid && ($op == 'update' || $op == 'delete')) {
        $grants['gcac_edit'] = GCAC_ALLOW;
    }
    return $grants;
}

I have added a field to my user accounts called 'Group' and populated it with a value for several accounts. Yet, here is the error that is being thrown:

Notice: Undefined property: stdClass::$field_group in gcac_node_grants() (line 21 of /homepages/99/d99999999/htdocs/node-access2/sites/all/modules/gcac/gcac.module).

As you can see I'm trying to access the 'Group' of the current user so that I can compare it to the 'Group' assigned no a node. I just want to access the value. Originally, I tried accessing $account->field_group[LANGUAGE_NONE][0]['value'], but I got the same error.

I can go grab it from the database, if I have to because I know I can access $user->uid, but surely the user module is providing my the field for me use? Thanks.

  • Could you somehow mark line 21 in your code block? – Mołot Aug 30 '13 at 12:03
  • Both lines that throw errors are now marked with **. The first is line 21. Thanks. – 24ma13wg Aug 30 '13 at 12:12
  • If anything mark them with /**/ - it will make your code valid and still let us see where exactly the problem is. But truth to be told I hoped only one of these 2 lines created error and we could find it by comparison... – Mołot Aug 30 '13 at 12:15
  • Added the Krumo I get, if this helps. – 24ma13wg Aug 30 '13 at 12:23
1

As per your error statement, field_group does not exist in $user object. The global $user object does not load the fields which are attached to user profile. if you want to load the complete user profile with attached fields, you need to use function "user_load" in your code.

global $user;

$account = user_load($user->uid);
if ($account->field_group[LANGUAGE_NONE][0]['value'] == $node->group && $op == 'view') {
  //Do the needful
}

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.