Skip to content

Commit 9dc1266

Browse files
reddommirus
andcommitted
Add support for multi-column list fields (#5)
Author: Zoltan Balogh <reddomester@gmail.com> Add support for multi-column list fields Co-authored-by: Matt Mirus <matt@mattmirus.com>
1 parent b5cfd1e commit 9dc1266

File tree

5 files changed

+57
-7
lines changed

5 files changed

+57
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.1.0 / 2019-09-24
4+
5+
- Add support for multiple columns using List field type (thanks @reddo!)
6+
37
## 1.0.5 / 2019-08-28
48

59
- Update dependencies to fix vulnerabilities

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77

88
This plugin allows you to add custom data attributes to Gravity Forms fields.
99

10-
It should work with all of the Standard field types and any of the Advanced field types that only have one input field (e.g., Email but not Address).
10+
It should work with:
11+
12+
- all of the Standard field types
13+
- any of the Advanced field types that only have one input field (e.g., Email but not Address)
14+
- the List advanced field type, including with multiple columns (thanks @reddo!).
1115

1216
For fields with choices, such as checkboxes, you can assign separate values for each data attribute for each choice (the UI for this is ugly right now 🤷‍).
1317

app/frontend.php

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ function dataAttrNamesToArray($attrs)
88
return preg_split("/\r\n|\n|\r/", $attrs);
99
}
1010

11+
// test is the field has data attributes
12+
function fieldHasDataAttributes($field)
13+
{
14+
return property_exists($field, 'enableDataAttrsField') && $field->enableDataAttrsField;
15+
}
16+
1117
// Add data attributes to inputs that don't have multiple choices
1218
add_filter('gform_field_content', function ($content, $field, $value, $lead_id, $form_id) {
13-
// Bail if: in the admin, the field has choices, or the field doesn't have data attributes enabled
14-
if (is_admin() || !empty($field->choices) || !property_exists($field, 'enableDataAttrsField') || !$field->enableDataAttrsField) {
19+
// Bail if: in the admin, no data attributes, or the field has choices
20+
if (is_admin() || !fieldHasDataAttributes($field) || !empty($field->choices)) {
1521
return $content;
1622
}
1723

@@ -38,8 +44,8 @@ function dataAttrNamesToArray($attrs)
3844

3945
// Add data attributes to inputs that have multiple choices (checkboxes, drop downs, etc.)
4046
add_filter('gform_field_choice_markup_pre_render', function ($choice_markup, $choice, $field, $value) {
41-
// Bail if: in the admin or the field doesn't have data attributes enabled
42-
if (is_admin() || !property_exists($field, 'enableDataAttrsField') || !$field->enableDataAttrsField) {
47+
// Bail if: in the admin or no data attributes
48+
if (is_admin() || !fieldHasDataAttributes($field)) {
4349
return $choice_markup;
4450
}
4551

@@ -72,3 +78,39 @@ function dataAttrNamesToArray($attrs)
7278

7379
return $choice_markup;
7480
}, 10, 4);
81+
82+
// Add data attributes to multi-column list fields
83+
add_filter('gform_column_input_content', function ($input, $input_info, $field, $text) {
84+
// Bail if: in the admin, no data attributes, or the field isn't using multiple columns
85+
if (is_admin() || !fieldHasDataAttributes($field) || empty($field->choices)) {
86+
return $input;
87+
}
88+
89+
$attrs = dataAttrNamesToArray($field->dataAttrsField);
90+
91+
$attrHtml = '';
92+
93+
foreach ($attrs as $attr) {
94+
$item = null;
95+
foreach ($field->choices as $choice) {
96+
if ($text == $choice['text']) {
97+
$item = $choice;
98+
break;
99+
}
100+
}
101+
102+
// skip if not set
103+
if (!array_key_exists($attr, $item)) {
104+
continue;
105+
}
106+
107+
$value = $item[$attr];
108+
$attrHtml .= " data-{$attr}='{$value}'";
109+
}
110+
111+
if ($attrHtml) {
112+
$input = str_replace(' name=', "$attrHtml name=", $input);
113+
}
114+
115+
return $input;
116+
}, 10, 5);

gravity-forms-data-attributes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Description: Add custom data attributes to Gravity Forms fields.
66
Author: Matt Mirus
77
Author URI: https://github.com/mmirus
8-
Version: 1.0.5
8+
Version: 1.1.0
99
GitHub Plugin URI: https://github.com/mmirus/gravity-forms-data-attributes
1010
*/
1111

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gravity-forms-data-attributes",
3-
"version": "1.0.5",
3+
"version": "1.1.0",
44
"description": "Add custom data attributes to Gravity Forms fields.",
55
"private": true,
66
"repository": "git@github.com:mmirus/gravity-forms-data-attributes.git",

0 commit comments

Comments
 (0)