-
Notifications
You must be signed in to change notification settings - Fork 6
/
field_group.module
1160 lines (991 loc) · 36.2 KB
/
field_group.module
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Fieldgroup module.
*
* For an overview of all php and javascript hooks, see field_group.api.php.
*
*/
require_once 'field_group.groups.inc';
/**
* Implements hook_config_info().
*/
function field_group_config_info() {
$prefixes['field_group.field_group'] = array(
'name_key' => array('entity_type', 'bundle', 'mode', 'group_name'),
'label_callback' => 'field_group_config_label',
'group' => t('Field groups'),
);
return $prefixes;
}
/**
* Given a bundle config file, display a unique label.
*/
function field_group_config_label($config, $config_name) {
list($entity_type_name, $bundle, $mode, $group_name) = explode('.', str_replace('field_group.field_group.', '', $config_name));
$entity_type = entity_get_info($entity_type_name);
$entity_label = $entity_type['label'];
$bundle_label = isset($entity_type['bundles'][$bundle]['label']) ? $entity_type['bundles'][$bundle]['label'] : $bundle;
return $entity_label . ' - ' . $bundle_label . ' - Mode: ' . $mode . ' - ' . $group_name;
}
/**
* Implements hook_menu().
*/
function field_group_menu() {
$items = array();
// Ensure the following is not executed until field_bundles is working and
// tables are updated. Needed to avoid errors on initial installation.
if (defined('MAINTENANCE_MODE')) {
return $items;
}
// Create tabs for all possible bundles.
foreach (entity_get_info() as $entity_type => $entity_info) {
if (isset($entity_info['fieldable']) && $entity_info['fieldable']) {
foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
if (isset($bundle_info['admin'])) {
// Extract path information from the bundle.
$path = $bundle_info['admin']['path'];
// Different bundles can appear on the same path (e.g. %node_type and
// %comment_node_type). To allow field_group_menu_load() to extract the
// actual bundle object from the translated menu router path
// arguments, we need to identify the argument position of the bundle
// name string ('bundle argument') and pass that position to the menu
// loader. The position needs to be casted into a string; otherwise it
// would be replaced with the bundle name string.
if (isset($bundle_info['admin']['bundle argument'])) {
$bundle_arg = $bundle_info['admin']['bundle argument'];
$bundle_pos = (string) $bundle_arg;
}
else {
$bundle_arg = $bundle_name;
$bundle_pos = '0';
}
// This is the position of the %field_group_menu placeholder in the
// items below.
$group_position = count(explode('/', $path)) + 1;
// Extract access information, providing defaults.
$access = array_intersect_key($bundle_info['admin'], backdrop_map_assoc(array('access callback', 'access arguments')));
$access += array(
'access callback' => 'user_access',
'access arguments' => array('administer site configuration'),
);
$items["$path/groups/%field_group_menu/delete"] = array(
'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
'title' => 'Delete',
'page callback' => 'backdrop_get_form',
'page arguments' => array('field_group_delete_form', $group_position),
'type' => MENU_CALLBACK,
'file' => 'field_group.field_ui.inc',
) + $access;
$items["$path/groups/%field_group_menu/enable"] = array(
'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
'title' => 'Enable',
'page callback' => 'backdrop_get_form',
'page arguments' => array('field_group_enable_form', $group_position),
'type' => MENU_CALLBACK,
'file' => 'field_group.field_ui.inc',
) + $access;
}
}
}
}
return $items;
}
/**
* Implements hook_permission().
*/
function field_group_permission() {
return array(
'administer fieldgroups' => array(
'title' => t('Administer fieldgroups'),
'description' => t('Display the administration for fieldgroups.'),
),
);
}
/**
* Menu Wildcard loader function to load group definitions.
*
* @param string $group_name
* The name of the group, as contained in the path.
* @param string $entity_type
* The name of the entity.
* @param string $bundle_name
* The name of the bundle, as contained in the path.
* @param int $bundle_pos
* The position of $bundle_name in $map.
* @param array $map
* The translated menu router path argument map.
*
* @return object
* Group definition.
*/
function field_group_menu_load($group_name, $entity_type, $bundle_name, $bundle_pos, $map) {
if ($bundle_pos > 0) {
$bundle = $map[$bundle_pos];
$bundle_name = field_extract_bundle($entity_type, $bundle);
}
$args = func_get_args();
$args_pop = array_pop($args);
$mode = array_pop($args_pop);
$group = field_group_load_field_group($group_name, $entity_type, $bundle_name, $mode);
return empty($group) ? FALSE : $group;
}
/**
* Loads a group definition.
*
* @param string $group_name
* The name of the group.
* @param string $entity_type
* The name of the entity.
* @param string $bundle_name
* The name of the bundle.
* @param string $mode
* The view mode to load.
*
* @return object
* Group definition.
*/
function field_group_load_field_group($group_name, $entity_type, $bundle_name, $mode) {
$config = config('field_group.field_group.' . $entity_type . '.' . $bundle_name . '.' . $mode . '.' . $group_name);
$object = (object) $config->get();
return $object;
}
/**
* Implements hook_theme_registry_alter().
*/
function field_group_theme_registry_alter(&$theme_registry) {
// Inject field_group_build_entity_groups in all entity theming functions.
$entity_info = entity_get_info();
$entities = array();
foreach ($entity_info as $entity => $info) {
if (isset($entity_info[$entity]['fieldable']) && $entity_info[$entity]['fieldable']) {
// User uses user_profile for theming.
if ($entity == 'user') $entity = 'user_profile';
$entities[] = $entity;
}
}
// Support for File Entity.
if (isset($theme_registry['file_entity'])) {
$entities[] = 'file_entity';
}
// Support for Entity API.
if (isset($theme_registry['entity'])) {
$entities[] = 'entity';
}
foreach ($entities as $entity) {
if (isset($theme_registry[$entity])) {
$theme_registry[$entity]['preprocess functions'][] = 'field_group_build_entity_groups';
// DS support, make sure it comes after field_group.
if ($key = array_search('ds_entity_variables', $theme_registry[$entity]['preprocess functions'])) {
unset($theme_registry[$entity]['preprocess functions'][$key]);
$theme_registry[$entity]['preprocess functions'][] = 'ds_entity_variables';
}
}
}
}
/**
* Implements hook_field_attach_delete_bundle().
*/
function field_group_field_attach_delete_bundle($entity_type, $bundle) {
$list = field_group_read_groups(array('bundle' => $bundle, 'entity_type' => $entity_type));
// Delete the entity's entry from field_group of all entities.
if (isset($list[$entity_type], $list[$entity_type][$bundle])) {
foreach ($list[$entity_type][$bundle] as $group_mode => $groups) {
foreach ($groups as $group) {
$config = config('field_group.field_group.' . $entity_type . '.' . $bundle . '.' . $group_mode . '.' . $group->group_name);
$config->delete();
}
}
}
}
/**
* Implements hook_field_attach_form().
*/
function field_group_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
$form['#attached']['css'][] = backdrop_get_path('module', 'field_group') . '/css/field_group.field_ui.css';
field_group_attach_groups($form, 'form', $form_state);
$form['#pre_render'][] = 'field_group_form_pre_render';
}
/**
* Implements hook_form_FORM_ID_alter().
* Using hook_form_field_ui_field_overview_form_alter.
*/
function field_group_form_field_ui_field_overview_form_alter(&$form, &$form_state) {
form_load_include($form_state, 'inc', 'field_group', 'field_group.field_ui');
field_group_field_ui_overview_form_alter($form, $form_state);
}
/**
* Implements hook_form_FORM_ID_alter().
* Using hook_form_field_ui_display_form_alter.
*/
function field_group_form_field_ui_display_form_alter(&$form, &$form_state) {
form_load_include($form_state, 'inc', 'field_group', 'field_group.field_ui');
field_group_field_ui_overview_form_alter($form, $form_state, TRUE);
}
/**
* Implements hook_field_attach_view_alter().
*/
function field_group_field_attach_view_alter(&$element, $context) {
// Check whether the view mode uses custom display settings or the 'default' mode.
$actual_mode = 'default';
if (isset($element['#entity_type']) && isset($element['#bundle'])) {
$view_mode_settings = field_view_mode_settings($element['#entity_type'], $element['#bundle']);
$view_mode = $context['view_mode'];
$actual_mode = (!empty($view_mode_settings[$view_mode]['custom_settings']) ? $view_mode : 'default');
field_group_attach_groups($element, $actual_mode);
}
}
/**
* Implements hook_field_group_format_settings().
* If the group has no format settings, default ones will be added.
*/
function field_group_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' => -4,
);
}
if (isset($formatter['instance_settings']['required_fields']) && $mode == 'form') {
$form['instance_settings']['required_fields'] = array(
'#type' => 'checkbox',
'#title' => t('Mark group as required if it contains 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,
);
}
if (isset($formatter['instance_settings']['id'])) {
$form['instance_settings']['id'] = array(
'#title' => t('ID'),
'#type' => 'textfield',
'#default_value' => isset($group->format_settings['instance_settings']['id']) ? $group->format_settings['instance_settings']['id'] : (isset($formatter['instance_settings']['id']) ? $formatter['instance_settings']['id'] : ''),
'#weight' => 10,
'#element_validate' => array('field_group_validate_id'),
);
}
if (isset($formatter['instance_settings']['classes'])) {
$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' => 11,
'#element_validate' => array('field_group_validate_css_class'),
);
}
if (isset($formatter['instance_settings']['description'])) {
$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,
);
}
return $form;
}
/**
* Helper function to prepare basic variables needed for most formatters.
*
* Called in field_group_field_group_pre_render(), but can also be called in
* other implementations of hook_field_group_pre_render().
*
* @param object $group
* The group object passed by reference.
*/
function field_group_pre_render_prepare(&$group) {
$classes = _field_group_get_html_classes($group);
$group->classes = implode(' ', $classes->required);
$group->description = !empty($group->format_settings['instance_settings']['description']) ? filter_xss_admin(t($group->format_settings['instance_settings']['description'])) : '';
}
/**
* Implements hook_field_group_pre_render().
*/
function field_group_field_group_pre_render(&$element, &$group, &$form) {
field_group_pre_render_prepare($group);
$view_mode = isset($form['#view_mode']) ? $form['#view_mode'] : 'form';
// Add all field_group format types to the js settings.
$form['#attached']['js'][] = array(
'data' => array('field_group' => array($group->format_type => $view_mode)),
'type' => 'setting',
);
if (isset($group->format_settings['instance_settings']['id']) && !empty($group->format_settings['instance_settings']['id'])) {
$element['#id'] = backdrop_html_id($group->format_settings['instance_settings']['id']);
}
$element['#weight'] = $group->weight;
// Call the pre render function for the format type.
$function = "field_group_pre_render_" . str_replace("-", "_", $group->format_type);
if (function_exists($function)) {
$function($element, $group, $form);
}
}
/**
* Remove empty groups on forms.
*
* @param string $parent_name
* The name of the element.
* @param array $element
* The element to check the empty state.
* @param array $groups
* Array of group objects.
*/
function field_group_remove_empty_form_groups($name, &$element, $groups, &$form_groups, $entity) {
$children = element_children($element);
$hasChildren = FALSE;
if (count($children)) {
foreach ($children as $childname) {
if (in_array($childname, $groups, TRUE)) {
field_group_remove_empty_form_groups($childname, $element[$childname], $groups, $form_groups, $entity);
}
$hasChildren = $hasChildren ? TRUE : _field_group_is_empty_element($element, $entity, $childname, $groups);
}
}
if (!$hasChildren) {
// Remove empty elements from the #groups.
if (empty($element) && isset($form_groups[$name]) && !is_array($form_groups[$name])) {
foreach ($form_groups as $group_name => $group) {
if (isset($group->children)) {
$group_children = array_flip($group->children);
if (isset($group_children[$name])) {
unset($form_groups[$group_name]->children[$group_children[$name]]);
}
}
}
}
$element['#access'] = FALSE;
}
}
/**
* Determine if an element has non-empty children.
*
* @param array $element
* The element to check.
* @param string $entity
* The entity type name.
* @param string $childname
* The name of the child element.
* @param array $groups
* Array of group objects.
*
* @return bool
* TRUE if element has non-empty children, FALSE otherwise.
*/
function _field_group_is_empty_element($element, $entity, $childname, $groups) {
$exceptions = array('user__account', 'comment__author');
$exception = $entity . '__' . $childname;
if (in_array($exception, $exceptions)) {
return TRUE;
}
if (isset($element[$childname]['#type'])
|| isset($element[$childname]['#markup'])
|| isset($element[$childname]['#prefix'])
|| isset($element[$childname]['#suffix'])
) {
return TRUE;
}
// Prevent a double recursive loop (groups are already recursive looped in field_group_remove_empty_form_groups.
if (in_array($childname, $groups)) {
return FALSE;
}
$children = element_children($element[$childname]);
foreach ($children as $child) {
if (_field_group_is_empty_element($element[$childname], $entity, $child, $groups)) {
return TRUE;
}
}
return FALSE;
}
/**
* Remove empty groups on entity display.
*
* @param array $element
* The element to check the empty state.
* @param array $groups
* Array of group objects.
*
* @return bool
* TRUE if group is empty, FALSE otherwise.
*/
function field_group_remove_empty_display_groups(&$element, $groups) {
$empty_child = TRUE;
$empty_group = TRUE;
// Loop through the children for current element.
foreach (element_children($element) as $name) {
// Descend if the child is a group.
if (in_array($name, $groups)) {
$empty_child = field_group_remove_empty_display_groups($element[$name], $groups);
if (!$empty_child) {
$empty_group = FALSE;
}
}
// Child is a field, the element is not empty and access is set to true (or empty).
elseif (!empty($element[$name]) && (!isset($element[$name]['#access']) || $element[$name]['#access'])) {
$empty_group = FALSE;
}
}
// Reset an empty group.
if ($empty_group) {
$element = NULL;
}
return $empty_group;
}
/**
* Implements hook_field_group_format_summary().
*/
function field_group_field_group_format_summary($group) {
$group_form = module_invoke_all('field_group_format_settings', $group);
backdrop_alter('field_group_format_settings', $group_form, $group);
$output = '';
if (isset($group->format_settings['formatter'])) {
$output .= '<strong>' . $group->format_type . '</strong> ' . $group->format_settings['formatter'] . '';
}
if (isset($group->format_settings['instance_settings'])) {
$last = end($group->format_settings['instance_settings']);
$output .= '<br />';
foreach ($group->format_settings['instance_settings'] as $key => $value) {
if (empty($value)) {
continue;
}
$output .= '<strong>' . $key . '</strong> ';
if (isset($group_form['instance_settings'], $group_form['instance_settings'][$key]['#options'])) {
if (is_array($value)) {
$value = implode(', ', array_filter($value));
}
else {
$value = $group_form['instance_settings'][$key]['#options'][$value];
}
}
// Shorten the string.
if (backdrop_strlen($value) > 38) {
$value = truncate_utf8($value, 50, TRUE, TRUE);
}
// If still numeric, handle it as yes or no.
elseif (is_numeric($value)) {
$value = $value == '1' ? t('yes') : t('no');
}
$output .= check_plain($value);
$output .= $last == $value ? ' ' : '<br />';
}
}
return $output;
}
/**
* Implements hook_field_extra_fields().
*/
function field_group_field_extra_fields() {
$extra = array();
$extra['user']['user'] = array('form' => array());
// User picture field to integrate with user module.
if (config_get('system.core', 'user_pictures')) {
$extra['user']['user']['form']['picture'] = array(
'label' => t('Picture'),
'description' => t('User picture'),
'weight' => 5,
);
}
// Field to itegrate with contact module.
if (module_exists('contact')) {
$extra['user']['user']['form']['contact'] = array(
'label' => t('Contact'),
'description' => t('Contact user element'),
'weight' => 5,
);
}
// Field to integrate with the locale module.
if (module_exists('locale')) {
$extra['user']['user']['form']['locale'] = array(
'label' => t('Language settings'),
'description' => t('Language settings for the user account.'),
'weight' => 5,
);
}
return $extra;
}
/**
* Implements hook_field_attach_rename_bundle().
*/
function field_group_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
$prefix = 'field_group.field_group.' . $entity_type;
$field_group_config_names = config_get_names_with_prefix("$prefix.$bundle_old.");
foreach ($field_group_config_names as $config_name) {
$mode_field_group = str_replace("$prefix.$bundle_old.", '', $config_name);
$field_group_old = config("$prefix.$bundle_old.$mode_field_group");
$field_group_new = config("$prefix.$bundle_new.$mode_field_group");
$data = $field_group_old->get();
$data['bundle'] = $bundle_new;
$field_group_new->setData($data);
$field_group_new->save();
$field_group_old->delete();
}
// Clear the cache.
field_cache_clear();
entity_info_cache_clear();
}
/**
* Get all groups that match.
*
* @param string $entity_type
* The name of the entity type.
* @param string $bundle
* The name of the bundle.
* @param string $view_mode
* The view mode.
* @param bool $reset.
* Whether to reset the cache or not.
*
* @return array
* Array of group objects.
*/
function field_group_info_groups($entity_type = NULL, $bundle = NULL, $view_mode = NULL, $reset = FALSE) {
static $groups = FALSE;
if (!$groups || $reset) {
$groups = field_group_read_groups();
}
if (!isset($entity_type)) {
return $groups;
}
elseif (!isset($bundle) && isset($groups[$entity_type])) {
return $groups[$entity_type];
}
elseif (!isset($view_mode) && isset($groups[$entity_type][$bundle])) {
return $groups[$entity_type][$bundle];
}
elseif (isset($groups[$entity_type][$bundle][$view_mode])) {
return $groups[$entity_type][$bundle][$view_mode];
}
return array();
}
/**
* Read all groups.
*
* @param array $conditions
* Parameters for the query, as elements of the $conditions array.
* 'entity_type' The name of the entity type.
* 'bundle' The name of the bundle.
* 'mode' The view mode.
*
* @return array
* Array of group objects.
*/
function field_group_read_groups($conditions = array()) {
$groups = array();
$records = array();
$config_names = config_get_names_with_prefix('field_group.field_group');
foreach ($config_names as $config_name) {
$config = config($config_name);
$field = (object)$config->get();
$records[$config_name] = $field;
// If any conditions don't pass then skip that record
if (!empty($conditions)) {
foreach ($conditions as $key => $value) {
if ($field->$key != $value) {
unset($records[$config_name]);
}
}
}
}
foreach ($records as $group) {
$groups[$group->entity_type][$group->bundle][$group->mode][$group->group_name] = $group;
}
backdrop_alter('field_group_info', $groups);
return $groups;
}
/**
* Checks if a field_group exists in required context.
*
* @param string $group_name
* The name of the group.
* @param string $entity_type
* The name of the entity.
* @param string $bundle
* The bundle for the entity.
* @param string $mode
* The view mode context the group will be rendered.
*
* @return bool
* TRUE if group exists, FALSE otherwise.
*/
function field_group_exists($group_name, $entity_type, $bundle, $mode) {
$groups = field_group_read_groups();
return !empty($groups[$entity_type][$bundle][$mode][$group_name]);
}
/**
* Delete a field group.
*
* @param object $group
* A group definition.
*/
function field_group_group_delete($group) {
$config = config('field_group.field_group.' . $group->entity_type . '.' . $group->bundle . '.' . $group->mode . '.' . $group->group_name);
$config->delete();
cache_clear_all('field_groups', 'cache_field');
module_invoke_all('field_group_delete_field_group', $group);
}
/**
* Saves a group definition.
*
* @param object $group
* A group definition.
*/
function field_group_group_save(&$group, $new = TRUE) {
// Prepare the record.
$object = $group;
if ($new == FALSE) {
// Existing record.
module_invoke_all('field_group_update_field_group', $object);
}
else {
// New record.
module_invoke_all('field_group_create_field_group', $object);
}
$config = config('field_group.field_group.' . $object->entity_type . '.' . $object->bundle . '.' . $object->mode . '.' . $object->group_name);
$config->setData((array)$object);
$config->save();
}
/**
* Retrieve all format possibilities for the fieldgroups.
*
* @return array|NULL
* Array of formatters. Attempts to retrieve from static cache if set.
*/
function field_group_formatter_info() {
$cache = &backdrop_static(__FUNCTION__, array());
if (empty($cache)) {
if ($cached = cache_get('field_group_formatter_info', 'cache_field')) {
$formatters = $cached->data;
}
else {
$formatters = array();
$formatters += module_invoke_all('field_group_formatter_info');
$hidden_region = array(
'label' => '<' . t('Hidden') . '>',
'description' => '',
'format_types' => array(),
'instance_settings' => array(),
'default_formatter' => '',
);
//$formatters['form']['hidden'] = $hidden_region;
$formatters['display']['hidden'] = $hidden_region;
cache_set('field_group_formatter_info', $formatters, 'cache_field');
}
$cache = $formatters;
}
return $cache;
}
/**
* Attach groups to the (form) build.
*
* @param array $element
* The part of the form.
* @param string $view_mode
* The mode for the build.
*/
function field_group_attach_groups(&$element, $view_mode) {
$entity_type = $element['#entity_type'];
$bundle = $element['#bundle'];
$element['#groups'] = field_group_info_groups($entity_type, $bundle, $view_mode);
$element['#fieldgroups'] = $element['#groups'];
// Create a lookup array.
$group_children = array();
foreach ($element['#groups'] as $group_name => $group) {
if (!empty($group->children)) {
foreach ($group->children as $child) {
$group_children[$child] = $group_name;
}
}
}
$element['#group_children'] = $group_children;
}
/**
* Pre render callback for rendering groups.
*
* @param $element Form that is being rendered.
*
* @return array
* Array with re-arranged fields in forms.
*
* @see field_group_field_attach_form
*/
function field_group_form_pre_render(&$element) {
return field_group_build_entity_groups($element, 'form');
}
/**
* Preprocess/ Pre-render callback.
*
* @param array $vars
* Preprocess vars or form element
* @param string $type
* The type of object being rendered
*
* @return array
* Array with re-arranged fields in forms.
*
* @see field_group_form_pre_render()
* @see field_group_theme_registry_alter
* @see field_group_fields_nest()
*/
function field_group_build_entity_groups(&$vars, $type) {
if ($type == 'form') {
$element = &$vars;
$nest_vars = NULL;
}
else {
$element = &$vars['elements'];
$nest_vars = &$vars;
}
// No groups on the entity.
if (empty($element['#fieldgroups'])) {
return $element;
}
// Nest the fields in the corresponding field groups.
field_group_fields_nest($element, $nest_vars);
// Someone is doing a node view, in a node view. Reset content.
// @todo Check if this breaks something else.
if (isset($element['#node']->content) && count($element['#node']->content) > 0) {
$element['#node']->content = array();
}
$display = isset($element['#view_mode']);
$groups = array_keys($element['#groups']);
// Dish the fieldgroups with no fields for non-forms.
if ($display) {
field_group_remove_empty_display_groups($element, $groups);
}
else {
// Fix the problem on forms with additional settings.
field_group_remove_empty_form_groups('form', $element, $groups, $element['#groups'], $element['#entity_type']);
}
// Add the default field_group javascript.
$element['#attached']['js'][] = backdrop_get_path('module', 'field_group') . '/js/field_group.js';
// Allow others to alter the pre_rendered build.
backdrop_alter('field_group_build_pre_render', $element);
// Return the element on forms.
if ($type == 'form') {
return $element;
}
// No groups on the entity. Prerender removed empty field groups.
if (empty($element['#fieldgroups'])) {
return $element;
}
// Put groups inside content if we are rendering an entity_view.
foreach ($element['#fieldgroups'] as $group) {
if (!empty($element[$group->group_name]) && $type != 'user_profile') {
$vars['content'][$group->group_name] = $element[$group->group_name];
}
elseif (!empty($element[$group->group_name])) {
$vars['user_profile'][$group->group_name] = $element[$group->group_name];
}
}
// New css / js can be attached.
backdrop_process_attached($element);
}
/**
* Recursive function to nest fields in the field groups.
*
* Will take out all the elements in the form and
* place them in the correct container element, a fieldgroup.
* The current group element in the loop is passed recursively so we can
* stash fields and groups in it while we go deeper in the array.
*
* @param array $element
* The current element to analyse for grouping.
* @param array $vars
* Rendering vars from the entity being viewed.
*/
function field_group_fields_nest(&$element, &$vars = NULL) {
// Create all groups and keep a flat list of references to these groups.
$group_references = array();
foreach ($element['#fieldgroups'] as $group_name => $group) {
// check for any erroneous groups from other modules
if (is_string($group_name)) {
// Construct own weight, as some fields (for example preprocess fields) don't have weight set.
$element[$group_name] = array();
$group_references[$group_name] = &$element[$group_name];
}
}
// Loop through all form children looking for those that are supposed to be
// in groups, and insert placeholder element for the new group field in the
// correct location within the form structure.
$element_clone = array();
foreach (element_children($element) as $child_name) {
$element_clone[$child_name] = $element[$child_name];
// If this element is in a group, create the placeholder element.
if (isset($element['#group_children'][$child_name])) {
$element_clone[$element['#group_children'][$child_name]] = array();
}
}
$element = array_merge($element_clone, $element);
// Move all children to their parents. Use the flat list of references for
// direct access as we don't know where in the root_element hierarchy the
// parent currently is situated.
foreach ($element['#group_children'] as $child_name => $parent_name) {
// Entity being viewed
if ($vars) {
// If not a group, check vars['content'] for empty field.
if (!isset($element['#fieldgroups'][$child_name]) && isset($vars['content'][$child_name])) {
$group_references[$parent_name][$child_name] = $vars['content'][$child_name];
unset($vars['content'][$child_name]);
}
elseif (!isset($element['#fieldgroups'][$child_name]) && isset($vars['user_profile'][$child_name])) {
$group_references[$parent_name][$child_name] = $vars['user_profile'][$child_name];
unset($vars['user_profile'][$child_name]);
}
// If this is a group, we have to use a reference to keep the reference
// list intact (but if it is a field we don't mind).
else {
$group_references[$parent_name][$child_name] = &$element[$child_name];
unset($element[$child_name]);
}
}
// Form being viewed
else {
// Block denied fields (#access) before they are put in groups.
// Fields (not groups) that don't have children (like field_permissions) are removed
// in field_group_field_group_build_pre_render_alter.
if (isset($element[$child_name]) && (!isset($element[$child_name]['#access']) || $element[$child_name]['#access'])) {