-
Notifications
You must be signed in to change notification settings - Fork 57
/
unique-repeater-sub-field.php
64 lines (55 loc) · 1.76 KB
/
unique-repeater-sub-field.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
This filter can be used to ensure the unuqueness of a repeater sub field value
*/
// example add_filter, can use any valid repeater sub field key
add_filter('acf/validate_value/key=field_59dd1ad221414', 'unique_repeater_subfield', 20, 4);
function unique_repeater_subfield($valid, $value, $field, $input) {
if (!$valid) {
return $valid;
}
// get list of array indexes from $input
// [ <= this fixes my IDE, it has problems with unmatched brackets in the regex
preg_match_all('/\[([^\]]+)\]/', $input, $matches);
if (!count($matches[1])) {
// this should actually never happen
return $valid;
}
// only need the index list captured
$matches = $matches[1];
// walk the acf input to find the repeater and current row
$array = $_POST['acf'];
$repeater_key = false;
$repeater_value = false;
$row_key = false;
$row_value = false;
$field_key = false;
$field_value = false;
for ($i=0; $i<count($matches); $i++) {
if (isset($array[$matches[$i]])) {
$repeater_key = $row_key;
$repeater_value = $row_value;
$row_key = $field_key;
$row_value = $field_value;
$field_key = $matches[$i];
$field_value = $array[$matches[$i]];
if ($field_key == $field['key']) {
// found the level we need to be at
break;
}
// get next level of acf input to check
$array = $array[$matches[$i]];
} // end isset
} // end foreach index
if (!$repeater_key) {
// this should not happen
return $valid;
}
// look for duplicate values in the repeater
foreach ($repeater_value as $index => $row) {
if ($index != $row_key && strtolower($row[$field_key]) == strtolower($value)) {
$valid = 'this value is not unique';
break;
}
}
return $valid;
} // end public function unique_repeater_subfield