-
Notifications
You must be signed in to change notification settings - Fork 0
/
field_group_multiple.field_ui.inc
281 lines (216 loc) · 10.4 KB
/
field_group_multiple.field_ui.inc
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
/**
* @file
* field_group_multiple.field_ui.inc
*/
function field_group_multiple_field_ui_overview_form_alter(&$form, &$form_state, $display_overview = FALSE) {
$form['#validate'][] = "field_group_multiple_field_overview_validate";
$form['#submit'][] = "field_group_multiple_field_overview_submit";
}
function field_group_multiple_field_overview_validate($form, &$form_state) {
_field_group_multiple_field_ui_overview_operation($form, $form_state, 'validate');
}
function field_group_multiple_field_overview_submit($form, &$form_state) {
_field_group_multiple_field_ui_overview_operation($form, $form_state, 'submit');
}
function _field_group_multiple_field_ui_overview_operation($form, &$form_state, $op) {
if (!isset($form['fields'])) return;
$map = array();
foreach (element_children($form['fields'], TRUE) as $field_name) {
if (isset($form['fields'][$field_name]['#row_type']) && $form['fields'][$field_name]['#row_type'] == 'field') {
if (isset($form['fields'][$field_name]['parent_wrapper']['parent']['#value']) && !empty($form['fields'][$field_name]['parent_wrapper']['parent']['#value'])) {
$map[$field_name] = $form['fields'][$field_name]['parent_wrapper']['parent']['#value'];
}
}
}
if (isset($form_state['values']['fields'])) {
foreach ($form_state['values']['fields'] as $field_name => $data) {
if (isset($data['format']['type']) && _is_field_group_multiple($data['format']['type'])) {
$function = '_' . $data['format']['type'] . '_format_' . $op;
if (function_exists($function)) {
$group = new stdClass();
$group->group_name = $field_name;
$group->format_type = $data['format']['type'];
$unsorted = array_keys($map, $field_name);
$sorted = array();
foreach ($unsorted as $unsorted_field) {
$sorted[$unsorted_field] = array('#weight' => $form_state['values']['fields'][$unsorted_field]['weight']);
}
$group->children = element_children($sorted, TRUE);
$function($form, $form_state, $group);
}
}
}
}
}
function _field_group_multiple_get_fields_infos($field_names, $key) {
$cardinalities = array();
foreach ($field_names as $field_name) {
$instance = field_info_field($field_name);
$cardinalities[$field_name] = $instance[$key];
}
return $cardinalities;
}
function _field_group_multiple_allowed_parent_formatter($view_mode) {
// TODO currently only two formatter allowed, check the other
$allowed_formatter = array("div", "fieldset");
$view_mode = $view_mode == 'form' ? $view_mode : 'display';
$formatter = field_group_formatter_info();
$formatter = $formatter[$view_mode];
$parent_formatter = array("" => t("None"));
foreach ($formatter as $key => $value) {
if (in_array($key, $allowed_formatter)) {
$parent_formatter[$key] = $value['label'];
}
}
return $parent_formatter;
}
/**
* Default settings
* TODO attach validate and submit functions directly to the element
*/
function _field_group_multiple_default_format_settings($group) {
$form = array();
$default_parent_type = isset($group->format_settings['instance_settings']['parent_format_type']) ? $group->format_settings['instance_settings']['parent_format_type'] : "";
$formatter_options = _field_group_multiple_allowed_parent_formatter($group->mode);
$form['instance_settings']['parent_format_type'] = array(
'#title' => t("Parent format type"),
'#type' => 'select',
'#options' => $formatter_options,
'#default_value' => $default_parent_type,
'#attributes' => array('class' => array('field-group-parent-type')),
);
if (!empty($default_parent_type)) {
$field_group_types = field_group_formatter_info();
$mode = $group->mode == 'form' ? 'form' : 'display';
$formatter = $field_group_types[$mode][$default_parent_type];
$form['instance_settings']['show_label'] = array(
'#title' => t('Show label'),
'#type' => 'select',
'#options' => array(0 => t('No'), 1 => t('Yes')),
'#default_value' => isset($group->format_settings['instance_settings']['show_label']) ? $group->format_settings['instance_settings']['show_label'] : $formatter['instance_settings']['show_label'],
'#weight' => 2,
);
$form['instance_settings']['label_element'] = array(
'#title' => t('Label element'),
'#type' => 'select',
'#options' => array('h2' => t('Header 2'), 'h3' => t('Header 3')),
'#default_value' => isset($group->format_settings['instance_settings']['label_element']) ? $group->format_settings['instance_settings']['label_element'] : $formatter['instance_settings']['label_element'],
'#weight' => 2,
);
$form['instance_settings']['effect'] = array(
'#title' => t('Effect'),
'#type' => 'select',
'#options' => array('none' => t('None'), 'blind' => t('Blind')),
'#default_value' => isset($group->format_settings['instance_settings']['effect']) ? $group->format_settings['instance_settings']['effect'] : $formatter['instance_settings']['effect'],
'#weight' => 2,
);
$form['instance_settings']['speed'] = array(
'#title' => t('Speed'),
'#type' => 'select',
'#options' => array('none' => t('None'), 'slow' => t('Slow'), 'fast' => t('Fast')),
'#default_value' => isset($group->format_settings['instance_settings']['speed']) ? $group->format_settings['instance_settings']['speed'] : $formatter['instance_settings']['speed'],
'#weight' => 3,
);
}
return $form;
}
/**
* Default summary
*/
function _field_group_multiple_default_format_summary($group) {
$cardinalities = _field_group_multiple_get_fields_infos($group->children, 'cardinality');
$min_cardinality = _field_group_multiple_get_max_cardinality($cardinalities);
$info = '<em>This value depends on the shortest selected "number of values" of the grouped fields.</em>';
if ($min_cardinality == FIELD_CARDINALITY_UNLIMITED) {
return '<div class="description">' . t('The maximal amount of items that can be added is unlimited.<br/>' . $info, array('!count' => $min_cardinality)) . '</div>';
}
else {
return '<div class="description">' . t('The maximal amount of items that can be added is !count.<br/>' . $info, array('!count' => $min_cardinality)) . '</div>';
}
}
function _field_group_multiple_default_format_validate($form, &$form_state, $group) {
$cardinalities = _field_group_multiple_get_fields_infos($group->children, 'cardinality');
if (!empty($cardinalities)) {
if (min($cardinalities) != max($cardinalities)) {
form_set_error("fields[" . $group->group_name . "][format][type]",
t('Field cardinalities in group !group_name must be equal.', array('!group_name' => $group->group_name)));
}
}
}
function _field_group_multiple_default_format_submit($form, &$form_state, $group) {
}
/**
* Table settings
*/
function _field_group_multiple_table_format_settings($group) {
$form = _field_group_multiple_default_format_settings($group);
$form['instance_settings']['row_titles'] = array(
'#title' => t("Row Titles"),
'#description' => t("If you want col 1 to render as row titles enter them as csv's, the first value is the col header, use -none- as the first to omit header e.g -none-,Red,Green,Blue or Colors,Red,Green,Blue ."),
'#type' => 'textarea',
'#default_value' => isset($group->format_settings['instance_settings']['row_titles']) ? $group->format_settings['instance_settings']['row_titles'] : '',
);
return $form;
}
function _field_group_multiple_table_format_summary($group) {
return _field_group_multiple_default_format_summary($group);
}
function _field_group_multiple_table_format_validate($form, &$form_state, $group) {
_field_group_multiple_default_format_validate($form, $form_state, $group);
}
function _field_group_multiple_table_format_submit($form, &$form_state, $group) {
_field_group_multiple_default_format_submit($form, $form_state, $group);
}
function _field_group_multiple_container_format_settings($group) {
$form = _field_group_multiple_default_format_settings($group);
return $form;
}
function _field_group_multiple_container_format_summary($group) {
return _field_group_multiple_default_format_summary($group);
}
function _field_group_multiple_container_format_validate($form, &$form_state, $group) {
_field_group_multiple_default_format_validate($form, $form_state, $group);
}
function _field_group_multiple_container_format_submit($form, &$form_state, $group) {
}
function _field_group_multiple_label_value_format_settings($group) {
$form = _field_group_multiple_default_format_settings($group);
$label_field = isset($group->format_settings['instance_settings']['label_value_field']) ? $group->format_settings['instance_settings']['label_value_field'] : "";
$fields = array();
foreach ($group->children as $field_name) {
$field = field_info_field($field_name);
if ($field['type'] == 'text') {
$fields[$field_name] = $field_name;
}
}
$form['instance_settings']['label_value_field'] = array(
'#title' => t("Field with the label"),
'#type' => 'select',
'#options' => $fields,
'#default_value' => $label_field,
'#attributes' => array('class' => array('field-group-label-field')),
);
return $form;
}
function _field_group_multiple_label_value_format_summary($group) {
return _field_group_multiple_default_format_summary($group);
}
function _field_group_multiple_label_value_format_validate($form, &$form_state, $group) {
// TODO check if this works
_field_group_multiple_default_format_validate($form, $form_state, $group);
if (isset($form_state['values']['fields'][$group->group_name]['format_settings'])) {
$_form = $form_state['values']['fields'][$group->group_name]['format_settings'];
$label_field = isset($_form['settings']['instance_settings']['label_value_field']) ? $_form['settings']['instance_settings']['label_value_field'] : NULL;
if (is_null($label_field) || empty($label_field)) {
form_set_error("fields[" . $group->group_name . "]", t('Label field must be selected in group "!group_name"', array('!group_name' => $group->group_name)));
}
else {
}
}
if (count($group->children) != 2) {
form_set_error("fields[" . $group->group_name . "]", t('Only two fields can be attached to "!group_name" because the group-type is of label-value.', array('!group_name' => $group->group_name)));
}
}
function _field_group_multiple_label_value_format_submit($form, &$form_state, $group) {
}