views:

121

answers:

2

Hi

I have a node with many CCK fields. I want to hide a field from anonymous user. I found there are roughly two approaches from http://www.lullabot.com/articles/modifying-forms-5-and-6. First, I tried theme_theme() with the code below

function ssaa_theme1(&$existing, $type, $theme, $path) {
    return array(
        'volunteer_node_form' => array(
            'arguments' => array('form' => null),
        ),
    );
}


function ssaa_volunteer_node_form($form) {
    $out = '';
    if (user_is_anonymous()) {
        unset($form['field_active']);
    }
    $out .= drupal_render($form);
    return $out;
}

This simple code worked well as I expected but produced strange result too. Save/Preview buttons are appeared on the top of the form and I couldn't move them to the bottom. At first I suspected drupal_render() function but it didn't do anything about the order. It produced same result when ssaa_volunteer_node_form() function was empty.

So I tried second option which uses hook_form_alter() and succeeded. But I still want to figure out why first approach didn't work properly. I think it's more easy and light way to do what I want to do.

Any ideas?

A: 

I think you can just use the Content Permissions module that comes bundled with CCK to set field-level permissions. Once you enable that module, all of your fields will appear in the Permissions area and you can deny permission to anonymous users there. I think that would be a much easier way to accomplish this.

theunraveler
I tried Content Permissions module before, but I gave up checking hundreds of checkboxes though. Thank you for the answer.
+1  A: 

If you only want to deny this one field to users, it makes sense to form_alter it. Otherwise, I also suggest you use Content Permissions. It will give you the power to toggle such settings for all your fields without needing a text editor.

CCK Field input forms do not accept hook_form_alter() very well. They are inserted into the node edit form late in the cycle, so to speak.

This example demonstrates how disable CCK form elements, it can be adapted to suit your needs.

As you have a very specific need, you might want to look at the new Field Permissions module. It is still in development (and I have not tried it), but it allows you to opt-in individual fields to Content Permissions. This prevents your Permissions screen from getting cluttered and from all new fields defaulting to inaccessible.

Grayside
Wow I wasn't aware of that Field Permissions module. It sounds like some of the benefits like "Edit own field permission" seem pretty worth it.
theunraveler
With Field Permissions module you suggested, it accomplished perfectly. That CCK field's default value also works. I really need the default value working. I've tried Content Permissions module but there're so many checkboxes to be checked (There're nearly 20 content types and 8 roles in the system) Thanks a lot!

related questions