Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Drupal - Conditionally hiding CCK fieldgroups

Parent Feed: 

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; } } } ?>
Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web