Restrict webform components by role

Public

Get raw version
php
  1. /**
  2.  * Alter the list of available Webform components.
  3.  */
  4. function hook_webform_component_info_alter(&$components){
  5. global $user;
  6.  
  7. if (!in_array('administrator', $user->roles)) {
  8. unset($components['file']);
  9. // unset($components['THE_COMPONENT']);
  10. }
  11. }

Comments

Felix Eve's picture

Really you should be checking for a permission, not a role, so this code would be better:

  1. function hook_webform_component_info_alter(&$components) {
  2. if(!user_access('full webform access')) {
  3. unset($components['email'], $components['hidden']);
  4. }
  5. }

Obviously you will also need to create a permission called "full webform access" for this to work.

The problem with checking for a specific role is it's very dependent on having exactly those roles, and that those roles never change names. Checking for permission is more versatile.