field_permissions.admin.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. (function ($) {
  2. Drupal.behaviors.fieldPermissionsSettings = {
  3. attach: function (context) {
  4. // For user fields, we want the "Create own value for field X" permission
  5. // row to only be displayed when it's meaningful (i.e., when the "Display
  6. // on user registration form" checkbox is checked).
  7. var $user_register_form_checkbox, $required_field_checkbox, $create_permission_row;
  8. $user_register_form_checkbox = $('.form-item-instance-settings-user-register-form .form-checkbox', context);
  9. if ($user_register_form_checkbox.length) {
  10. // The "Required field" checkbox can cause the user registration checkbox
  11. // to change, so we need it also.
  12. $required_field_checkbox = $('.form-item-instance-required .form-checkbox', context);
  13. if ($required_field_checkbox.length) {
  14. // Get the permissions table row corresponding to the "Create own value
  15. // for field X" permission. The theme_user_admin_permissions() function
  16. // does not give us a good way to directly detect which row contains
  17. // the create permissions, so we have rely on the fact that it will be
  18. // the first row.
  19. $create_permission_row = $('table#permissions tbody tr', context).filter(':first');
  20. new Drupal.fieldPermissions.HideCreatePermission($user_register_form_checkbox, $required_field_checkbox, $create_permission_row);
  21. }
  22. }
  23. }
  24. };
  25. Drupal.fieldPermissions = {};
  26. /**
  27. * Constructor for the HideCreatePermission object.
  28. *
  29. * This object hides and shows the "Create own value for field X" permission
  30. * for user fields when it is appropriate to do so, depending on the state of
  31. * the "Display on user registration form" and "Required field" checkboxes.
  32. */
  33. Drupal.fieldPermissions.HideCreatePermission = function ($user_register_form_checkbox, $required_field_checkbox, $create_permission_row) {
  34. this.$user_register_form_checkbox = $user_register_form_checkbox;
  35. this.$create_permission_row = $create_permission_row;
  36. // Start off by making sure the create permission row has the correct
  37. // visibility.
  38. this.setCreatePermissionVisibility();
  39. // Set the row's visibility again whenever the user registration checkbox
  40. // changes, or when the required field checkbox (which controls it) changes.
  41. $user_register_form_checkbox.bind('change', $.proxy(this.setCreatePermissionVisibility, this));
  42. $required_field_checkbox.bind('change', $.proxy(this.setCreatePermissionVisibility, this));
  43. };
  44. /**
  45. * Set the correct visibility of the "Create own value for field X" permission.
  46. */
  47. Drupal.fieldPermissions.HideCreatePermission.prototype.setCreatePermissionVisibility = function () {
  48. // Granting permissions for "Create own value for field X" only makes sense
  49. // when the field is configured to appear on the user registration form, so
  50. // only show the row in the permissions table then.
  51. if (this.$user_register_form_checkbox.is(':checked')) {
  52. this.$create_permission_row.show();
  53. }
  54. else {
  55. this.$create_permission_row.hide();
  56. }
  57. };
  58. })(jQuery);