tac_create.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * Disable disallowed terms in taxonomy fields, and re-enable on submit.
  3. *
  4. * We do this in jQuery because FAPI does not yet support it:
  5. * @see
  6. * http://drupal.org/node/284917
  7. * @see
  8. * http://drupal.org/node/342316
  9. *
  10. * @todo
  11. * Use clearer coding standards.
  12. * @see
  13. * http://jsdemystified.drupalgardens.com/
  14. */
  15. Drupal.behaviors.tac_create = {};
  16. Drupal.behaviors.tac_create.attach = function(context, settings) {
  17. var $ = jQuery;
  18. var $fields = $(Drupal.settings.taxonomy_access);
  19. // For each controlled field, disable disallowed terms.
  20. $.each($fields, function(i, field) {
  21. var fieldname = "." + field.field;
  22. // Disable disallowed term and its label, if any.
  23. $.each(field.disallowed_tids, function(j, tid) {
  24. // Children of the widget element with the specified tid as a value.
  25. // Can be either <option> or <input>.
  26. // .tac_fieldname [value='1']
  27. selector = fieldname + " [value='" + tid + "']";
  28. $(selector).attr('disabled','disabled');
  29. // Label sibling adjacent the child element.
  30. // .tac_fieldname [value='1'] + label
  31. label_selector = fieldname + " [value='" + tid + "']" + " + label";
  32. $(label_selector).attr('class','option disabled');
  33. });
  34. });
  35. // Re-enable and re-select disallowed defaults on submit.
  36. $("form").submit(function() {
  37. // For each controlled field, re-enable disallowed terms.
  38. $.each($fields, function(i, field) {
  39. var fieldname = "." + field.field;
  40. // Enable and select disallowed defaults.
  41. $.each(field.disallowed_defaults, function(j, tid) {
  42. // Children of the widget element with the specified tid as a value.
  43. // Can be either <option> or <input>.
  44. // .tac_fieldname [value='1']
  45. selector = fieldname + " [value='" + tid + "']";
  46. $(selector).attr('disabled','');
  47. $(selector).attr('selected','selected');
  48. });
  49. });
  50. });
  51. }