Drupal - Conditionally hiding CCK fieldgroups

Conditional Fields is a great Drupal module for conditionally hiding CCK fields.

Unfortunately, it can't hide CCK fieldgroups. It's also not ideal if you're concerned about security— it simply hides fields; it doesn't deny access. A recent project of mine required that I conditionally deny access to field groups, so I decided to implement a programmatic solution.

In this case, I needed to deny access to specific fieldgroups in content type 'company' based on:

  • The role of the acting user
  • The value of the field_company_type CCK field

The downside to this approach is that you must save and return to the node edit page before seeing a change in fieldgroup visibility.

Here's my solution: roles)) && $user->uid != 1){ $company_type = $form['#node']->field_company_type[0]['value']; $hidden_groups = array(); //determine which fields should be hidden for which company types //visible to only manufacturer if ($company_type != 'manufacturer') { $hidden_groups[] = 'group_manufacturing'; $hidden_groups[] = 'group_reactions'; $hidden_groups[] = 'group_equipment'; } //visible to only manufacturer and distrubutors if ($company_type != 'manufacturer' && $company_type != 'distributor_supplier'){ $hidden_groups[] = 'group_capabilities'; $hidden_groups[] = 'group_certifications'; $hidden_groups[] = 'group_products'; } //visible to only manufacturers, distrubutors, and service providers if ($company_type != 'manufacturer' && $company_type != 'distributor_supplier' && $company_type != 'industry_service_provider'){ //$hidden_groups[] = 'group_industries'; $hidden_groups[] = 'group_industries'; $hidden_groups[] = 'group_consultants'; } //deny access to hidden fieldgroups foreach ($hidden_groups as $group){ $form[$group]['#access'] = FALSE; } } } ?>

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.