user_role_field.admin.inc 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. /**
  3. * Alter the field settings form.
  4. */
  5. function _user_role_field_field_settings_form_alter(&$form, $form_state, $form_id) {
  6. // Try to obtain the field name from the form itself.
  7. if ($form_id == 'field_ui_field_settings_form') {
  8. $field_name = $form['field']['field_name']['#value'];
  9. }
  10. elseif ($form_id == 'field_ui_field_edit_form') {
  11. $field_name = $form['instance']['field_name']['#value'];
  12. }
  13. else {
  14. return;
  15. }
  16. // Try to obtain information about this field.
  17. $field = field_info_field($field_name);
  18. if (empty($field)) {
  19. return;
  20. }
  21. // Enhance the field settings form with role visibility, but without
  22. // the anonymouse user.
  23. $roles = user_roles ();
  24. $form['field']['settings']['user_role_field'] = array(
  25. '#title' => t('Apply only to'),
  26. '#type' => 'checkboxes',
  27. '#checkall' => TRUE,
  28. '#options' => $roles,
  29. '#default_value' => _user_role_field_roles($field),
  30. '#description' => t('Use these options to make a field visible for a specific role. If no role is set, the field is visible to all roles. If "Display on user registration form" is ticked and some roles have been set here, make sure to set the anonymous user role as well, else the field will not be shown.'),
  31. '#weight' => -1,
  32. );
  33. // Hide the option to non-privileged users.
  34. if (!user_access('administer user field roles')) {
  35. $form['field']['settings']['user_role_field']['#type'] = 'value';
  36. $form['field']['settings']['user_role_field']['#value'] = $form['field']['settings']['user_role_field']['#default_value'];
  37. }
  38. }