Access to field by node creation date in Drupal with CCK

718 views Asked by At

I'm looking for a particular behaviour that grant permission to see one or more cck field based on date time creation of node.

In particular i need to:

Grant to role A: Full access to all CCK (old and new) Grant to role B: Access to all CCK but ONLY to ones present in node OLDER than 1 Year Anonymous user: No access to CCK field

How can i get this result?

4

There are 4 answers

7
Nikit On

It's easy, create node-{YOURTYPE}.tpl.php, theme it, and add conditions to show fields dependency from dates...

0
Henrik Opel On

I would implement hook_nodeapi() in a custom module, and on $op == 'view' check for the proper node type and the user role. Depending on the role, I'd remove the field entry in question from the `$node->content' array.

Note that the custom module needs to be invoked after CCK for this to work, as otherwise the CCK fields are not yet available in the `$node->content' array. So depending on its name, one might need to alter the custom modules weight to a value higher than that of CCK.

If the same logic needs to be applied for node edit forms as well, one would do the same on $op == 'prepare'.

1
Sid Kshatriya On

You want to control permissions on a field level so I would avoid using hook_nodeapi() Instead I would suggest you use hook_field_access (or use a combination of hook_nodeapi and hook_field_access()

example in pseudocode:

mymodule_hook_field_access($op, $field, $account = NULL, $node = NULL){

    if($field['field_name'] == 'field_xyz'){
        switch($op){
            case 'view':
                if($node->created was less than a year ago && $account role is A){
                   return FALSE;
                }

                if($node->created was less than a year ago && $account role is B){
                 return TRUE;
                }

                return FALSE;
                break;
            case 'edit':
                ...
                ...
        }    
    }

    return TRUE;
}

See to see the hook being called http://api.lullabot.com/content_access

hook_field_access() is available in Drupal 6 from the CCK module. In Drupal 7 it is in core.

3
Dret On

Thanks to all for suggestions!

The hook_field_access() is the solution i'm looking for.

I previously used the $node->field_FIELDNAME[0]['#value'] but I don't like to put in my theme (or in template.php) functions for user access. There was also the problem of theming, this solution didn't give my possibility for an easy and clean HTML output using Semantic CCK module!

Thanks again! Bye!

Ps. why my post is not after the last??!!