-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathallowed_values_function.module
More file actions
109 lines (71 loc) · 4.12 KB
/
allowed_values_function.module
File metadata and controls
109 lines (71 loc) · 4.12 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/**
* Implements hook_form_alter
*
* @param unknown $form
* @param unknown $form_state
* @param unknown $form_id
*/
function allowed_values_function_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'field_ui_field_settings_form' || $form_id == 'field_ui_field_edit_form') {
if (strstr(($form['#field']['type'] ?? ''), 'list_') || strstr(($form['field']['type']['#value'] ?? ''), 'list_')) {
$form['field']['settings']['allowed_values_function'] = array(
'#type' => 'textfield',
'#title' => t('Allowed values function'),
'#default_value' => ($form['field']['settings']['allowed_values_function']['#value'] ?? ''),
'#description' => t('You may specify the name of a PHP function which returns an associative array
of keys => values. Do not add () after the function name or any other arguments or punctuation.
<br> Ex: <em>my_module_get_vals</em>.
<br>See how to implement your function (including expected arguments) in this module\'s included README.md.
<br>If you enter anything in this field, the "Allowed values list" above <strong>will be ignored</strong>.
<br>If you wish to use the "Allowed values list", simply delete the name of the function entered here and save.'),
);
$form['instance']['default_value_function'] = array(
'#type' => 'textfield',
'#title' => t('Default value function'),
'#default_value' => ($form['#instance']['default_value_function'] ?? ''),
'#description' => t('Similar to the Allowed values function field, enter the name of a function which supplies a default value for this
field. See the included README.md file for an example of what the function is expected to return.
<br>You may leave this blank to have no particular default value, or if you are unsure what to enter.'),
);
unset($form['field']['settings']['allowed_values_function_display']);
$form['#validate'][] = 'allowed_values_function_field_function_validate';
}
} // field_ui_field_settings_form
} // hook_form_alter
/**
* The primary thing we want to do here is validate that the function specified actually exists.
*
* @param unknown $form
* @param unknown $form_state
*/
function allowed_values_function_field_function_validate($form, &$form_state) {
// Clean up our function name, just in case the user entered extra characters
$fn = ($form_state['values']['field']['settings']['allowed_values_function'] ?? '');
$fn = str_replace('(', '', $fn);
$fn = str_replace(')', '', $fn);
$fn = str_replace(';', '', $fn);
$fn = trim($fn);
$dfn = ($form_state['values']['instance']['default_value_function'] ?? '');
$dfn = str_replace('(', '', $dfn);
$dfn = str_replace(')', '', $dfn);
$dfn = str_replace(';', '', $dfn);
$dfn = trim($dfn);
// Save function name back to form/form_state so it will be submitted correctly.
if ($fn) {
form_set_value($form['field']['settings']['allowed_values_function'], $fn, $form_state);
}
if ($dfn) {
form_set_value($form['instance']['default_value_function'], $dfn, $form_state);
}
// Does the function exist? If not, fail validation and tell the user.
if ($fn && !function_exists($fn)) {
form_set_error('field][settings][allowed_values_function', t('The function name you entered for "Allowed values" is invalid or does not exist. Please
check the spelling and try again.'));
}
// Does the function exist? If not, fail validation and tell the user.
if ($dfn && !function_exists($dfn)) {
form_set_error('instance][default_value_function', t('The function name you entered for "Default value" is invalid or does not exist. Please
check the spelling and try again.'));
}
} // ... validate