I've created a new user role (country_admin) and I have created a field for these users called user_country. This can be set when registering a user to UK or USA.

I have created a taxonomy 'Countries' and have added UK and USA as terms. I have also created a term reference on my articles so that articles can be assigned to a country.

The functionality I require is that only the user who has UK in the user_country field can post an article with the term set to UK.

I am looking at hook_node_access_records() and hook_node_grants() and have got the functionality working so UK users can only VIEW or UPDATE UK articles using this, however, in hook_node_grants() there doesn't seem to be an "ADD" operation.

How would I do this?

Current hook_node_grants() below

* Implements hook_node_grants().
function curator_scraper_node_grants($account, $op) {
    if ($op == 'view') {
        if (array_key_exists(DRUPAL_AUTHENTICATED_RID, $account->roles)) {

            //Let's get the country id the user is linked with
            $uid = $account->uid;
            $user = user_load($uid);
            $country = $user->field_user_country['und'][0];

            $grants[MYMODULE_REALM] = array(
                MYMODULE_GRANT_ID_PUBLIC, // Allow public data
                $country, // this should be users' country link
        else {
            // this is an anonymous user, give them the 'public' grant ID
            // that allows them access to public nodes
            $grants[MYMODULE_REALM] = array(
    if ($op == 'update'){
        //We can grant update permissions here similar to the above
        // but where is $op == "add"???
    return $grants;

  • It sounds like you are trying to reinvent the Organic Groups module. Have you reviewed what it does before writing your own code? Sep 5, 2014 at 8:46
  • In a nutshell, I would rather write my own code. I currently build WP plugins and am now learning Drupal module development. I'm not building a site - more of a learning exercise.
    – ElNariz
    Sep 5, 2014 at 9:18
  • OK, then, in that case I would suggest you look at altering the form (to remove options that don't apply because not allowed). IIRC hook_node_grants() only applies to items already in the database, and doesn't affect saving. Sep 5, 2014 at 9:28

1 Answer 1


You can use the Node access user reference module:

Gives content access permissions to users for content that references the users with User reference or Entity reference.

This is great if you want your content authors to be able to choose additional authors, or choose who can view their content, as well as being able to display the list of authors or viewers within your content, or in views displays.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.