|
| 1 | +<?php |
| 2 | +/* |
| 3 | +Plugin Name: Gravity Forms Data Attributes |
| 4 | +Plugin URI: https://github.com/mmirus/gravity-forms-data-attributes |
| 5 | +Description: Add custom data attributes to your form inputs |
| 6 | +Author: Matt Mirus |
| 7 | +Author URI: https://github.com/mmirus |
| 8 | +Version: 1.0.0 |
| 9 | +GitHub Plugin URI: https://github.com/mmirus/gravity-forms-data-attributes |
| 10 | + */ |
| 11 | + |
| 12 | +add_action('gform_field_standard_settings', function ($position, $form_id) { |
| 13 | + if ($position === 1350) : |
| 14 | + ?> |
| 15 | + <li class="enable_data_attrs_setting field_setting"> |
| 16 | + <input type="checkbox" id="field_enable_data_attrs_value" onclick="ToggleDataAttrs();" /> |
| 17 | + <label for="field_enable_data_attrs_value" class="inline"> |
| 18 | + <?php esc_html_e('Enable Data Attributes', 'gravityforms'); ?> |
| 19 | + </label> |
| 20 | + |
| 21 | + <div id="gform_data_attrs" style="display:none;"> |
| 22 | + <br> |
| 23 | + <label for="field_data_attrs" class="section_label"> |
| 24 | + <?php esc_html_e('Data Attribute Names', 'gravityforms'); ?> |
| 25 | + <?php gform_tooltip('form_field_data_attrs') ?> |
| 26 | + </label> |
| 27 | + <textarea id="field_data_attrs" class="fieldwidth-3 fieldheight-2" oninput="SetFieldProperty('dataAttrsField', this.value);"></textarea> |
| 28 | + <em style="display:block;">You must save the form after changing this setting.</em> |
| 29 | + |
| 30 | + <div id="gform_data_attr_inputs"></div> |
| 31 | + </div> |
| 32 | + </li> |
| 33 | + <?php |
| 34 | + endif; |
| 35 | +}, 10, 2); |
| 36 | + |
| 37 | +add_action('gform_editor_js', function () { |
| 38 | + ?> |
| 39 | + <script type='text/javascript'> |
| 40 | + // show/hide data attributes textarea |
| 41 | + function ToggleDataAttrs(isInit){ |
| 42 | + var speed = isInit ? "" : "slow"; |
| 43 | + |
| 44 | + if(jQuery("#field_enable_data_attrs_value").is(":checked")){ |
| 45 | + jQuery("#gform_data_attrs").show(speed); |
| 46 | + |
| 47 | + SetFieldProperty('enableDataAttrsField', true); |
| 48 | + } |
| 49 | + else{ |
| 50 | + jQuery("#gform_data_attrs").hide(speed); |
| 51 | + SetFieldProperty('enableDataAttrsField', false); |
| 52 | + SetFieldProperty('dataAttrsField', ''); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // make custom settings availalbe to all field types |
| 57 | + for (i in fieldSettings) { |
| 58 | + fieldSettings[i] += ', .enable_data_attrs_setting, .data_attrs_setting'; |
| 59 | + } |
| 60 | + |
| 61 | + // initialize our custom settings on the field settings load event |
| 62 | + jQuery(document).on('gform_load_field_settings', function(event, field, form){ |
| 63 | + jQuery('#field_enable_data_attrs_value').attr('checked', field.enableDataAttrsField == true); |
| 64 | + ToggleDataAttrs(true); |
| 65 | + jQuery('#field_data_attrs').val(field.dataAttrsField); |
| 66 | + |
| 67 | + if (['checkbox', 'radio'].includes(field.type)) return; |
| 68 | + |
| 69 | + var dataAttrsInputContainer = jQuery('#gform_data_attr_inputs'); |
| 70 | + |
| 71 | + var dataAttrs = field.dataAttrsField |
| 72 | + |
| 73 | + if (!dataAttrs) return ''; |
| 74 | + |
| 75 | + dataAttrs = dataAttrs.split("\n").map(function(name) { |
| 76 | + return { |
| 77 | + name: name, |
| 78 | + value: field[name] || '' |
| 79 | + }; |
| 80 | + }); |
| 81 | + |
| 82 | + var inputs = dataAttrs.map(function(dataAttr) { |
| 83 | + return "<br><label class='section_label'>" + dataAttr.name + "</label><input type='text' id='" + dataAttr.name + "' value='" + dataAttr.value + "' class='field-" + dataAttr.name + " field-data-attr' data-attr-name='" + dataAttr.name + "' /><br>"; |
| 84 | + }).join(''); |
| 85 | + |
| 86 | + dataAttrsInputContainer.append(inputs); |
| 87 | + }); |
| 88 | + |
| 89 | + // save data attribute values (general) |
| 90 | + jQuery('#gform_data_attr_inputs').on('input propertychange', '.field-data-attr', function () { |
| 91 | + var $this = jQuery(this); |
| 92 | + |
| 93 | + field = GetSelectedField(); |
| 94 | + field[$this.data('attrName')] = $this.val(); |
| 95 | + }); |
| 96 | + |
| 97 | + // save data attribute values ()checkbox and radio fields) |
| 98 | + jQuery('.choices_setting').on('input propertychange', '.field-choice-data-attr', function () { |
| 99 | + var $this = jQuery(this); |
| 100 | + var i = $this.closest('li.field-choice-row').data('index'); |
| 101 | + |
| 102 | + field = GetSelectedField(); |
| 103 | + field.choices[i][$this.data('attrName')] = $this.val(); |
| 104 | + }); |
| 105 | + |
| 106 | + // add data attribute fields to checkbox / radio choices |
| 107 | + gform.addFilter('gform_append_field_choice_option', function (str, field, i) { |
| 108 | + var inputType = GetInputType(field); |
| 109 | + var custom = field.choices[i].custom ? field.choices[i].custom : ''; |
| 110 | + |
| 111 | + var dataAttrs = field.dataAttrsField |
| 112 | + |
| 113 | + if (!dataAttrs) return ''; |
| 114 | + |
| 115 | + dataAttrs = dataAttrs.split("\n").map(function(name) { |
| 116 | + return { |
| 117 | + name: name, |
| 118 | + value: field.choices[i][name] || '' |
| 119 | + }; |
| 120 | + }); |
| 121 | + |
| 122 | + // TODO remove inline styles from below? |
| 123 | + var inputs = dataAttrs.map(function(dataAttr) { |
| 124 | + var id = inputType + "_choice_" + dataAttr.name + "_" + i; |
| 125 | + return "<label style='width:155px;margin:10px 0 0;'>" + dataAttr.name + " <input type='text' id='" + id + "' value='" + dataAttr.value + "' class='field-choice-input field-choice-" + dataAttr.name + " field-choice-data-attr' data-attr-name='" + dataAttr.name + "' /></label>"; |
| 126 | + }).join(''); |
| 127 | + |
| 128 | + return "<div style='display:flex; flex-wrap:wrap; margin-left:35px;'>" + inputs + "</div>"; |
| 129 | + }); |
| 130 | + </script> |
| 131 | +<?php |
| 132 | + |
| 133 | +}); |
| 134 | + |
| 135 | +add_filter('gform_tooltips', function ($tooltips) { |
| 136 | + $tooltips['form_field_data_attrs'] = "<h6>Data Attribute Names</h6><p>Enter the names of the data attributes you wish to enable, one per line.</p><p>You must save the form after changing this setting.</p>"; |
| 137 | + return $tooltips; |
| 138 | +}); |
0 commit comments