-
Notifications
You must be signed in to change notification settings - Fork 6
/
field_group.api.php
468 lines (425 loc) · 15.5 KB
/
field_group.api.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
<?php
/**
* @file
* Hooks provided by the Field Group module.
*
* Fieldgroup is a module that will wrap fields and other fieldgroups. Nothing more, nothing less.
* For this there are formatters we can create on forms and view modes.
*
* Some of the elements defined in fieldgroup will be ported to the elements module.
*
* DEVELOPERS NOTES
*
* - Fieldgroup uses a ''#fieldgroups' property to know what fieldgroups are to be pre_rendered and
* rendered by the field_group module. This means we need to be sure our groups are in #fieldgroups.
* #fieldgroups is later merged with the normal #groups that can be used by any other module.
* This is done to be sure fieldgroup is not taking fieldsets from profile2, commerce line items,
* commerce user profiles, ... .
* When trying to merge a programmatically created field wrapper (div, markup, fieldset, ...) into
* groups, you might consider adding it in #field_groups as well if you want the element processed
* by fieldgroup.
*/
/**
* @addtogroup hooks
* @{
*/
/**
* Javascript hooks
*
* Backdrop.FieldGroup.Effects.processHook.execute()
* See field_group.js for the examples for all implemented formatters.
*/
/**
* Implements hook_field_group_formatter_info().
*
* Define the information on formatters. The formatters are
* separated by view mode type. We have "form" for all form elements
* and "display" will be the real view modes (full, teaser, sticky, ...)
*
* structure:
* @code
* array(
* 'form' => array(
* 'fieldset' => array(
* // required, String with the name of the formatter type.
* 'label' => t('Fieldset'),
* // optional, String description of the formatter type.
* 'description' => t('This is field group that ...'),
* // required, Array of available formatter options.
* 'format_types' => array('open', 'collapsible', 'collapsed'),
* // required, String with default value of the style.
* 'default_formatter' => 'collapsible',
* // optional, Array with key => default_value pairs.
* 'instance_settings' => array('key' => 'value'),
* ),
* ),
* 'display' => array(
* 'fieldset' => array(
* // required, String with the name of the formatter type.
* 'label' => t('Fieldset'),
* // optional, String description of the formatter type.
* 'description' => t('This is field group that ...'),
* // required, Array of available formatter options.
* 'format_types' => array('open', 'collapsible', 'collapsed'),
* // required, String with default value of the style.
* 'default_formatter' => 'collapsible',
* // optional, Array with key => default_value pairs.
* 'instance_settings' => array('key' => 'value'),
* ),
* ),
* ),
* @endcode
*
* @return array
* A collection of available formatting html controls for form
* and display overview type.
*
* @see field_group_field_group_formatter_info()
*/
function hook_field_group_formatter_info() {
return array(
'form' => array(
'fieldset' => array(
'label' => t('Fieldset'),
'description' => t('This fieldgroup renders the inner content in a fieldset with the title as legend.'),
'format_types' => array('open', 'collapsible', 'collapsed'),
'instance_settings' => array('classes' => ''),
'default_formatter' => 'collapsible',
),
),
'display' => array(
'div' => array(
'label' => t('Div'),
'description' => t('This fieldgroup renders the inner content in a simple div with the title as legend.'),
'format_types' => array('open', 'collapsible', 'collapsed'),
'instance_settings' => array('effect' => 'none', 'speed' => 'fast', 'classes' => ''),
'default_formatter' => 'collapsible',
),
),
);
}
/**
* Implements hook_field_group_format_settings().
*
* Defines configuration widget for the settings on a field group
* formatter. Each formatter can have different elements and storage.
*
* @param object $group
* The Group definition.
* @return array
* The form element for the format settings.
*/
function hook_field_group_format_settings($group) {
// Add a wrapper for extra settings to use by others.
$form = array(
'instance_settings' => array(
'#tree' => TRUE,
'#weight' => 2,
),
);
$field_group_types = field_group_formatter_info();
$mode = $group->mode == 'form' ? 'form' : 'display';
$formatter = $field_group_types[$mode][$group->format_type];
// Add the required formatter type selector.
if (isset($formatter['format_types'])) {
$form['formatter'] = array(
'#title' => t('Fieldgroup settings'),
'#type' => 'select',
'#options' => backdrop_map_assoc($formatter['format_types']),
'#default_value' => isset($group->format_settings['formatter']) ? $group->format_settings['formatter'] : $formatter['default_formatter'],
'#weight' => 1,
);
}
if ($mode == 'form') {
$form['instance_settings']['required_fields'] = array(
'#type' => 'checkbox',
'#title' => t('Mark group for required fields.'),
'#default_value' => isset($group->format_settings['instance_settings']['required_fields']) ? $group->format_settings['instance_settings']['required_fields'] : (isset($formatter['instance_settings']['required_fields']) ? $formatter['instance_settings']['required_fields'] : ''),
'#weight' => 2,
);
}
$form['instance_settings']['classes'] = array(
'#title' => t('Extra CSS classes'),
'#type' => 'textfield',
'#default_value' => isset($group->format_settings['instance_settings']['classes']) ? $group->format_settings['instance_settings']['classes'] : (isset($formatter['instance_settings']['classes']) ? $formatter['instance_settings']['classes'] : ''),
'#weight' => 3,
'#element_validate' => array('field_group_validate_css_class'),
);
$form['instance_settings']['description'] = array(
'#title' => t('Description'),
'#type' => 'textarea',
'#default_value' => isset($group->format_settings['instance_settings']['description']) ? $group->format_settings['instance_settings']['description'] : (isset($formatter['instance_settings']['description']) ? $formatter['instance_settings']['description'] : ''),
'#weight' => 0,
);
// Add optional instance_settings.
switch ($group->format_type) {
case 'div':
$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,
);
break;
case 'fieldset':
$form['instance_settings']['classes'] = array(
'#title' => t('Extra CSS classes'),
'#type' => 'textfield',
'#default_value' => isset($group->format_settings['instance_settings']['classes']) ? $group->format_settings['instance_settings']['classes'] : $formatter['instance_settings']['classes'],
'#weight' => 3,
'#element_validate' => array('field_group_validate_css_class'),
);
break;
case 'tabs':
case 'htabs':
case 'accordion':
unset($form['instance_settings']['description']);
if (isset($form['instance_settings']['required_fields'])) {
unset($form['instance_settings']['required_fields']);
}
break;
case 'tab':
case 'htab':
case 'accordion-item':
default:
}
return $form;
}
/**
* Implements hook_field_group_pre_render().
*
* This function gives you the opportunity to create the given
* wrapper element that can contain the fields.
* In the example beneath, some variables are prepared and used when building the
* actual wrapper element. All elements in Backdrop FAPI can be used.
*
* Note that at this point, the field group has no notion of the fields in it.
*
* There is also an alternative way of handling this. The default implementation
* within field_group calls "field_group_pre_render_<format_type>".
* @see field_group_pre_render_fieldset.
*
* @param array $elements
* Elements by address.
* @param object $group
* The field group info.
*/
function hook_field_group_pre_render(&$element, $group, &$form) {
// You can prepare some variables to use in the logic.
$view_mode = isset($form['#view_mode']) ? $form['#view_mode'] : 'form';
$id = $form['#entity_type'] . '_' . $form['#bundle'] . '_' . $view_mode . '_' . $group->group_name;
$classes = '';
// Each formatter type can have whole different set of element properties.
switch ($group->format_type) {
// Normal or collapsible div.
case 'div':
$effect = isset($group->format_settings['instance_settings']['effect']) ? $group->format_settings['instance_settings']['effect'] : 'none';
$speed = isset($group->format_settings['instance_settings']['speed']) ? $group->format_settings['instance_settings']['speed'] : 'none';
$add = array(
'#type' => 'markup',
'#weight' => $group->weight,
'#id' => $id,
);
$classes .= " speed-$speed effect-$effect";
if ($group->format_settings['formatter'] != 'open') {
$add['#prefix'] = '<div class="field-group-format ' . $classes . '">
<span class="field-group-format-toggler">' . check_plain(t($group->label)) . '</span>
<div class="field-group-format-wrapper" style="display: none;">';
$add['#suffix'] = '</div></div>';
}
else {
$add['#prefix'] = '<div class="field-group-format ' . $group->group_name . ' ' . $classes . '">';
$add['#suffix'] = '</div>';
}
if (!empty($description)) {
$add['#prefix'] .= '<div class="description">' . $description . '</div>';
}
$element += $add;
if ($effect == 'blind') {
backdrop_add_library('system', 'effects.blind');
}
break;
break;
}
}
/**
* Implements hook_field_group_pre_render().
*
* Last resort to alter the pre_render build.
*/
function hook_field_group_pre_render_alter(&$element, $group, &$form) {
if ($group->format_type == 'htab') {
$element['#theme_wrappers'] = array('my_horizontal_tab');
}
}
/**
* Implements hook_field_group_build_pre_render_alter().
*
* Last resort where you can alter things. It is expected that when you need
* this function, you have most likely a very custom case or it is a fix that
* can be put in field_group core.
*
* @param array $elements
* Elements by address.
*/
function hook_field_group_build_pre_render_alter(&$element) {
// Prepare variables.
$display = isset($element['#view_mode']);
$groups = array_keys($element['#groups']);
// Example from field_group itself to unset empty elements.
if ($display) {
field_group_remove_empty_display_groups($element, $groups);
}
// You might include additional javascript files and stylesheets.
$element['#attached']['js'][] = backdrop_get_path('module', 'field_group') . '/js/field_group.js';
$element['#attached']['css'][] = backdrop_get_path('module', 'field_group') . '/css/field_group.css';
}
/**
* Implements hook_field_group_format_summary().
*
* Place to override or change default summary behavior. In most
* cases the implementation of field group itself will be enough.
*
* TODO It might be better to change this hook with already created summaries,
* giving the ability to alter or add it later on.
*
* @param object $group
* Group definition.
*
* @return string
* HTML of the format summary.
*/
function hook_field_group_format_summary($group) {
$output = '';
// Create additional summary or change the default setting.
return $output;
}
/**
* Alter the field group definitions provided by other modules.
*
* @param array $groups
* Reference to an array of field group definition objects.
*/
function hook_field_group_info_alter(&$groups) {
if (!empty($groups['group_issue_metadata|node|project_issue|form'])) {
$groups['group_issue_metadata|node|project_issue|form']->data['children'][] = 'taxonomy_vocabulary_9';
}
}
/**
* Implements hook_field_group_update_field_group().
*
* @param object $group
* The Field Group definition.
*/
function hook_field_group_update_field_group($group) {
// Update data depending on the group.
}
/**
* Implements hook_field_group_delete_field_group().
*
* @param object $group
* The Field Group definition.
*/
function hook_field_group_delete_field_group($group) {
// Delete extra data depending on the group.
}
/**
* Implements hook_field_group_create_field_group().
*
* @param object $group
* The Field Group definition.
*/
function hook_field_group_create_field_group($group) {
// Create extra data depending on the group.
}
/**
* Implements hook_field_group_format_settings_alter().
*
* @param array $form
* An associative array containing the structure of the form, which is passed
* by reference.
* @param object $group
* The Group definition.
*/
function hook_field_group_format_settings_alter(&$form, &$group) {
// Alter the group format settings that appear in the summary and form.
}
/**
* Implements hook_field_group_field_ui_parent_requirements_alter().
*
* @param array $parent_requirements
* An associative array keyed by group type of a group parent.
* @param array $context
* Array of the form structure and display overview.
*/
function hook_field_group_field_ui_parent_requirements_alter(&$parent_requirements, &$context) {
// Alter the parent requirements array used to display warning if a container
// has not been set up.
}
/**
* Implements hook_field_group_html_classes_alter().
*
* @param object $classes
* An object of required and optional classes.
* @param object $group
* The Group definition.
*/
function hook_field_group_html_classes_alter(&$classes, &$group) {
// Alter the required or optional classes on a field group.
}
/**
* @} End of "addtogroup hooks".
*/
/**
* @addtogroup utility functions
* @{
*/
/**
* Get the groups for a given entity type, bundle and view mode.
*
* @param String $entity_type
* The Entity type where field groups are requested.
* @param String $bundle
* The entity bundle for the field groups.
* @param String $view_mode
* The view mode scope for the field groups.
*
* @see field_group_read_groups()
*/
function field_group_info_groups($entity_type = NULL, $bundle = NULL, $view_mode = NULL, $reset = FALSE) {
// This function caches the result and delegates to field_group_read_groups.
}
/**
* Get the groups for the given parameters, uncached.
*
* @param Array $params
* The Entity type where field groups are requested.
* @param $enabled
* Return enabled or disabled groups.*
*
* @see field_group_info_groups()
*/
function field_group_read_groups($conditions = array()) {
// This function loads the requested groups
}
/**
* Hides field groups including children in a render array.
*
* @param array $element
* A render array. Can be a form, node, user, ...
* @param array $group_names
* An array of field group names that should be hidden.
*/
function field_group_hide_field_groups(&$element, $group_names) {
}
/**
* @} End of "addtogroup utility functions".
*/