-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfield_group.field_ui.inc
1027 lines (921 loc) · 38 KB
/
field_group.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
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
* Field_group.field_ui.inc is a file that contains most functions
* needed on the Fields UI Manage forms (display and fields).
*/
/**
* Helper function to get the form parameters to use while
* building the fields and display overview form.
*
* @param array $form
* An associative array containing the structure of the form.
* @param bool $display_overview
* Set to TRUE to get display overview form parameters.
*
* @return object
* Group parameters.
*/
function field_group_field_ui_form_params($form, $display_overview) {
$params = new stdClass();
$params->entity_type = $form['#entity_type'];
$params->bundle = $form['#bundle'];
$params->admin_path = _field_ui_bundle_admin_path($params->entity_type, $params->bundle);
$params->display_overview = $display_overview;
if ($display_overview) {
$params->region_callback = 'field_group_display_overview_row_region';
$params->mode = $form['#view_mode'];
}
else {
$params->region_callback = 'field_group_field_overview_row_region';
$params->mode = 'form';
}
$params->groups = field_group_info_groups($params->entity_type, $params->bundle, $params->mode, TRUE);
// Gather parenting data.
$params->parents = array();
foreach ($params->groups as $name => $group) {
foreach ($group->children as $child) {
$params->parents[$child] = $name;
}
}
return $params;
}
/**
* Alter the fields overview and display overview screen.
*
* @param array $form
* An associative array containing the structure of the form, which is passed
* by reference.
* @param array $form_state
* An associative array containing the current state of the form, which is
* passed by reference.
* @param bool $display_overview
* Set to TRUE if altering the display overview.
*/
function field_group_field_ui_overview_form_alter(&$form, &$form_state, $display_overview = FALSE) {
// Only start altering the form if we need to.
if ($display_overview && empty($form['#fields']) && empty($form['#extra'])) {
return;
}
$params = field_group_field_ui_form_params($form, $display_overview);
// Add some things to be able to preserve synced usage of field_ui.
if (!$display_overview) {
// This key is used to store the current updated field.
$form_state += array(
'formatter_settings_edit' => NULL,
);
// Add AJAX wrapper.
$form['fields']['#prefix'] = '<div id="field-display-overview-wrapper">';
$form['fields']['#suffix'] = '</div>';
}
$form['#groups'] = array_keys($params->groups);
$table = &$form['fields'];
// Add a region for 'add_new' rows, but only when fields are
// available and thus regions.
if (isset($table['#regions'])) {
$table['#regions'] += array(
'add_new' => array('title' => ' '),
);
}
// Extend available parenting options.
foreach ($params->groups as $name => $group) {
$table['#parent_options'][$name] = $group->label;
}
$table['#parent_options']['_add_new_group'] = t('Add new group');
// Update existing rows accordingly to the parents.
foreach (element_children($table) as $name) {
$table[$name]['parent_wrapper']['parent']['#options'] = $table['#parent_options'];
// Inherit the value of the parent when default value is empty.
if (empty($table[$name]['parent_wrapper']['parent']['#default_value'])) {
$table[$name]['parent_wrapper']['parent']['#default_value'] = isset($params->parents[$name]) ? $params->parents[$name] : '';
}
}
$formatter_options = field_group_field_formatter_options($display_overview ? 'display' : 'form');
$refresh_rows = isset($form_state['values']['refresh_rows']) ? $form_state['values']['refresh_rows'] : (isset($form_state['input']['refresh_rows']) ? $form_state['input']['refresh_rows'] : NULL);
// Create the group rows and check actions.
foreach (array_keys($params->groups) as $name) {
// Play around with form_state so we only need to hold things
// between requests, until the save button was hit.
if (isset($form_state['field_group'][$name])) {
$group = & $form_state['field_group'][$name];
}
else {
$group = & $params->groups[$name];
}
// Check the currently selected formatter, and merge persisted values for
// formatter settings for the group.
// This needs to be done first, so all fields are updated before creating form elements.
if (isset($refresh_rows) && $refresh_rows == $name) {
$settings = isset($form_state['values']['fields'][$name]) ? $form_state['values']['fields'][$name] : (isset($form_state['input']['fields'][$name]) ? $form_state['input']['fields'][$name] : NULL);
if (array_key_exists('settings_edit', $settings)) {
//$group->format_type = $form_state['field_group'][$name]->format_type;
$group = $form_state['field_group'][$name];
}
field_group_formatter_row_update($group, $settings);
}
// Save the group when the configuration is submitted.
if (!empty($form_state['values'][$name . '_formatter_settings_update'])) {
field_group_formatter_settings_update($group, $form_state['values']['fields'][$name]);
}
// After all updates are finished, let the form_state know.
$form_state['field_group'][$name] = $group;
$settings = field_group_format_settings_form($group);
$id = strtr($name, '_', '-');
$js_rows_data[$id] = array('type' => 'group', 'name' => $name);
// A group cannot be selected as its own parent.
$parent_options = $table['#parent_options'];
unset($parent_options[$name]);
$table[$name] = array(
'#attributes' => array('class' => array('draggable', 'field-group'), 'id' => $id),
'#row_type' => 'group',
'#region_callback' => $params->region_callback,
'#js_settings' => array('rowHandler' => 'group'),
'human_name' => array(
'#markup' => check_plain(t($group->label)),
'#prefix' => '<span class="group-label">',
'#suffix' => '</span>',
),
'weight' => array(
'#type' => 'textfield',
'#default_value' => $group->weight,
'#size' => 3,
'#attributes' => array('class' => array('field-weight')),
),
'parent_wrapper' => array(
'parent' => array(
'#type' => 'select',
'#options' => $parent_options,
'#empty_value' => '',
'#default_value' => isset($params->parents[$name]) ? $params->parents[$name] : '',
'#attributes' => array('class' => array('field-parent')),
'#parents' => array('fields', $name, 'parent'),
),
'hidden_name' => array(
'#type' => 'hidden',
'#default_value' => $name,
'#attributes' => array('class' => array('field-name')),
),
),
);
$table[$name] += array(
'group_name' => array(
'#markup' => check_plain($name),
),
'format' => array(
'type' => array(
'#type' => 'select',
'#options' => $formatter_options,
'#default_value' => $group->format_type,
'#attributes' => array('class' => array('field-group-type')),
),
),
);
$base_button = array(
'#submit' => array('field_ui_display_overview_multistep_submit'),
'#ajax' => array(
'callback' => 'field_ui_display_overview_multistep_js',
'wrapper' => 'field-display-overview-wrapper',
'effect' => 'fade',
),
'#field_name' => $name,
);
if ($form_state['formatter_settings_edit'] == $name) {
$table[$name]['format']['#cell_attributes'] = array('colspan' => $display_overview ? 3 : 2);
$table[$name]['format']['format_settings'] = array(
'#type' => 'container',
'#attributes' => array('class' => array('field-formatter-settings-edit-form')),
'#parents' => array('fields', $name, 'format_settings'),
'#weight' => -5,
'label' => array(
'#markup' => t('Field group format:') . ' <span class="formatter-name">' . $group->format_type . '</span>',
),
// Create a settings form where hooks can pick in.
'settings' => $settings,
'actions' => array(
'#type' => 'actions',
'save_settings' => $base_button + array(
'#type' => 'submit',
'#name' => $name . '_formatter_settings_update',
'#value' => t('Update'),
'#op' => 'update',
),
'cancel_settings' => $base_button + array(
'#type' => 'submit',
'#name' => $name . '_formatter_settings_cancel',
'#value' => t('Cancel'),
'#op' => 'cancel',
// Do not check errors for the 'Cancel' button.
'#limit_validation_errors' => array(),
),
),
);
$table[$name]['#attributes']['class'][] = 'field-formatter-settings-editing';
$table[$name]['format']['type']['#attributes']['class'] = array('element-invisible');
}
else {
// After saving, the settings are updated here as well. First we create
// the element for the table cell.
$table[$name]['settings_summary'] = array('#markup' => '');
if (!empty($group->format_settings)) {
$table[$name]['settings_summary'] = field_group_format_settings_summary($name, $group);
}
// Add the configure button.
$table[$name]['settings_edit'] = array(
'#type' => 'container',
'#attributes' => array('class' => array('field-formatter-settings-edit-wrapper')),
);
$table[$name]['settings_edit']['edit'] = $base_button + array(
'#type' => 'submit',
'#name' => $name . '_group_settings_edit',
'#value' => t('Configure'),
'#attributes' => array('class' => array('field-formatter-settings-edit button-secondary'), 'alt' => t('Configure')),
'#op' => 'edit',
// Do not check errors for the 'Configure' button.
'#limit_validation_errors' => array(),
);
if ($display_overview) {
$table[$name]['settings_edit']['delete'] = array(
'#markup' => l(t('Delete'), $params->admin_path . '/groups/' . $name . '/delete/' . $params->mode),
);
}
}
if (!$display_overview) {
$table[$name]['settings_edit']['delete'] = array(
'#markup' => l(t('Delete'), $params->admin_path . '/groups/' . $name . '/delete/form'),
);
}
}
// Additional row: add new group.
$parent_options = $table['#parent_options'];
unset($parent_options['_add_new_group']);
$table['_add_new_group'] = field_group_add_row('_add_new_group', $parent_options, $params);
$table['_add_new_group'] += array(
'format' => array(
'type' => array(
'#type' => 'select',
'#options' => $formatter_options,
'#default_value' => 'fieldset',
'#prefix' => '<div class="add-new-placeholder"> </div>',
),
'#cell_attributes' => array('colspan' => 2),
),
);
if (!$display_overview) {
// See field_ui.admin.inc for more details on refresh rows.
$form['refresh_rows'] = array('#type' => 'hidden');
$form['refresh'] = array(
'#type' => 'submit',
'#value' => t('Refresh'),
'#op' => 'refresh_table',
'#submit' => array('field_ui_display_overview_multistep_submit'),
'#ajax' => array(
'callback' => 'field_ui_display_overview_multistep_js',
'wrapper' => 'field-display-overview-wrapper',
'effect' => 'fade',
// The button stays hidden, so we hide the AJAX spinner too. Ad-hoc
// spinners will be added manually by the client-side script.
'progress' => 'none',
),
);
}
$form['#attached']['css'][] = backdrop_get_path('module', 'field_group') . '/css/field_group.field_ui.css';
$form['#attached']['js'][] = backdrop_get_path('module', 'field_group') . '/js/field_group.field_ui.js';
$form['#validate'][] = 'field_group_field_overview_validate';
$form['#submit'][] = 'field_group_field_overview_submit';
// Create the settings for fieldgroup as vertical tabs.
field_group_field_ui_create_vertical_tabs($form, $form_state, $params);
// Show a warning if the user has not set up required containers
if ($form['#groups']) {
$parent_requirements = array();
$context = array(
'form' => $form,
'display_overview' => $display_overview,
);
backdrop_alter('field_group_field_ui_parent_requirements', $parent_requirements, $context);
foreach ($form['#groups'] as $group_name) {
$group_check = field_group_load_field_group($group_name, $params->entity_type, $params->bundle, $params->mode);
if (isset($parent_requirements[$group_check->format_type])) {
if (!$group_check->parent_name || field_group_load_field_group($group_check->parent_name, $params->entity_type, $params->bundle, $params->mode)->format_type != $parent_requirements[$group_check->format_type]['parent']) {
backdrop_set_message(t($parent_requirements[$group_check->format_type]['message']), 'warning', FALSE);
}
}
}
}
}
/**
* Return an array of field_group_formatter options.
*
* @param string $type
* Name of field group type.
*
* @return array
* Array of field group formatter options.
*/
function field_group_field_formatter_options($type) {
$options = &backdrop_static(__FUNCTION__);
if (!isset($options)) {
$options = array();
$field_group_types = field_group_formatter_info();
foreach ($field_group_types[$type] as $name => $field_group_type) {
$options[$name] = $field_group_type['label'];
}
}
return $options;
}
/**
* Helper function to add a row in the overview forms.
*
* @param string $name
* Machine name.
* @param array $parent_options
* An array of field group parents.
* @param object $params
* Group parameters.
*/
function field_group_add_row($name, $parent_options, $params) {
return array(
'#attributes' => array('class' => array('draggable', 'field-group', 'add-new')),
'#row_type' => 'add_new_group',
'#js_settings' => array('rowHandler' => 'group'),
'#region_callback' => $params->region_callback,
'label' => array(
'#title_display' => 'invisible',
'#title' => t('Label for new group'),
'#type' => 'textfield',
'#size' => 15,
'#description' => t('Label'),
'#prefix' => '<div class="label-input"><div class="add-new-placeholder">' . t('Add new group') . '</div>',
'#suffix' => '</div>',
),
'weight' => array(
'#type' => 'textfield',
'#default_value' => field_info_max_weight($params->entity_type, $params->bundle, $params->mode) + 3,
'#size' => 3,
'#title_display' => 'invisible',
'#title' => t('Weight for new group'),
'#attributes' => array('class' => array('field-weight')),
'#prefix' => '<div class="add-new-placeholder"> </div>',
),
'parent_wrapper' => array(
'parent' => array(
'#title_display' => 'invisible',
'#title' => t('Parent for new group'),
'#type' => 'select',
'#options' => $parent_options,
'#empty_value' => '',
'#attributes' => array('class' => array('field-parent')),
'#prefix' => '<div class="add-new-placeholder"> </div>',
'#parents' => array('fields', $name, 'parent'),
),
'hidden_name' => array(
'#type' => 'hidden',
'#default_value' => $name,
'#attributes' => array('class' => array('field-name')),
),
),
'group_name' => array(
'#type' => 'machine_name',
'#title' => t('New field name'),
'#title_display' => 'invisible',
// This field should stay LTR even for RTL languages.
'#field_prefix' => '<span dir="ltr">group_',
'#field_suffix' => '</span>‎',
'#size' => 15,
'#description' => t('A unique machine-readable name containing letters, numbers, and underscores.'),
// 32 characters minus the 'field_' prefix.
'#maxlength' => 26,
'#prefix' => '<div class="add-new-placeholder"> </div>',
'#machine_name' => array(
'source' => array('fields', $name, 'label'),
'exists' => '_field_group_group_name_exists',
'standalone' => TRUE,
'label' => '',
),
'#required' => FALSE,
'#cell_attributes' => array('colspan' => $params->display_overview ? 1 : 2),
),
);
}
/**
* Creates a form for field_group formatters.
*
* @param object $group
* The Field Group object, which is passed by reference.
*
* @return array
* An associative array containing the structure of the form.
*/
function field_group_format_settings_form(&$group) {
$form = array();
$form['label'] = array(
'#type' => 'textfield',
'#title' => t('Field group label'),
'#default_value' => $group->label,
'#weight' => -5,
'#element_validate' => array('field_group_format_settings_label_validate'),
);
$addition = module_invoke_all('field_group_format_settings', $group);
backdrop_alter('field_group_format_settings', $addition, $group);
$form += $addition;
// Give fieldgroup implementations the chance to alter the description.
if (!empty($addition['label']['#description'])) {
$form['label']['#description'] = $addition['label']['#description'];
}
$form['#validate'] = array('field_group_format_settings_form_validate');
return $form;
}
/**
* Validate the label. Label is required for fieldsets that are collapsible.
*
* @param array $element
*
* @param array $form_state
* An associative array containing the current state of the form.
*/
function field_group_format_settings_label_validate($element, &$form_state) {
$group = $form_state['values']['fields'][$element['#parents'][1]];
$settings = $group['format_settings']['settings'];
$name = $form_state['formatter_settings_edit'];
$form_state['values']['fields'][$name]['settings_edit_form']['settings'] = $settings;
if ($group['format']['type'] == 'fieldset' && ($settings['formatter'] == 'collapsed' || $settings['formatter'] == 'collapsible') && empty($settings['label'])) {
form_error($element, t('The label is required when formatter is collapsible or collapsed'));
}
if ($group['format']['type'] == 'details' && empty($settings['label'])) {
form_error($element, t('The label is required'));
}
}
/**
* Update the row so that the group variables are updated.
* The rendering of the elements needs the updated defaults.
*
* @param object $group
* Group definition, passed by reference.
* @param array $settings
* Configuration settings.
*/
function field_group_formatter_row_update(&$group, $settings) {
// If the row has changed formatter type, update the group object.
if (!empty($settings['format']['type']) && $settings['format']['type'] != $group->format_type) {
$group->format_type = $settings['format']['type'];
field_group_formatter_settings_update($group, $settings);
}
}
/**
* Update handler for field_group configuration settings.
*
* @param object $group
* The group definition object.
* @param array $settings
* Configuration settings
*/
function field_group_formatter_settings_update(&$group, $settings) {
// For format changes we load the defaults.
if (empty($settings['format_settings']['settings'])) {
$mode = $group->mode == 'form' ? 'form' : 'display';
$group->format_settings = _field_group_get_default_formatter_settings($group->format_type, $mode);
}
else {
$group->format_type = $settings['format']['type'];
$group->label = $settings['format_settings']['settings']['label'];
$group->format_settings = $settings['format_settings']['settings'];
}
}
/**
* Creates a summary for the field format configuration summary.
*
* @param string $group_name
* The name of the group.
* @param object $group
* The group definition.
*
* @return array
* Array of summary ready to be rendered.
*/
function field_group_format_settings_summary($group_name, $group) {
$summary = implode('<br />', module_invoke_all('field_group_format_summary', $group));
return array(
'#markup' => '<div class="field-formatter-summary">' . $summary . '</div>',
'#cell_attributes' => array('class' => array('field-formatter-summary-cell')),
);
}
/**
* Returns the region to which a row in the 'Manage fields' screen belongs.
*
* @param array $row
* A field or field_group row.
*
* @return string
* The current region.
*/
function field_group_field_overview_row_region($row) {
switch ($row['#row_type']) {
case 'group':
return 'main';
case 'add_new_group':
// If no input in 'label', assume the row has not been dragged out of the
// 'add new' section.
if (empty($row['label']['#value'])) {
return 'add_new';
}
return 'main';
}
}
/**
* Returns the region to which a row in the 'Manage display' screen belongs.
*
* @param array $row
* A field or field_group row.
*
* @return string
* The current region.
*/
function field_group_display_overview_row_region($row) {
switch ($row['#row_type']) {
case 'group':
return ($row['format']['type']['#value'] == 'hidden' ? 'hidden' : 'visible');
case 'add_new_group':
// If no input in 'label', assume the row has not been dragged out of the
// 'add new' section.
if (empty($row['label']['#value'])) {
return 'add_new';
}
return ($row['format']['type']['#value'] == 'hidden' ? 'hidden' : 'visible');
}
}
/**
* Render API callback: Checks if a group machine name is taken.
*
* @param string $value
* The machine name, not prefixed with 'group_'.
*
* @return bool
* Always FALSE.
*
* @todo
* This callback can only check the name globally without context.
* Field group names, however, can be the same in different view modes and
* content types. See field_group_field_overview_validate() for current
* approach to validating the group name.
*/
function _field_group_group_name_exists($value) {
return FALSE;
}
/**
* Validate handler for the overview screens.
*
* @param array $form
* An associative array containing the structure of the form.
* @param array $form_state
* The state of the form.
*/
function field_group_field_overview_validate($form, &$form_state) {
$form_values = $form_state['values']['fields'];
$entity_type = $form['#entity_type'];
$bundle = $form['#bundle'];
$mode = (isset($form['#view_mode']) ? $form['#view_mode'] : 'form');
$group = $form_values['_add_new_group'];
// Validate if any information was provided in the 'add new group' row.
if (array_filter(array($group['label'], $group['group_name']))) {
// Missing group name.
if (!$group['group_name']) {
form_set_error('fields][_add_new_group][group_name', t('Add new group: you need to provide a group name.'));
}
// Group name validation.
else {
$group_name = $group['group_name'];
// Add the 'group_' prefix.
if (backdrop_substr($group_name, 0, 6) != 'group_') {
$group_name = 'group_' . $group_name;
form_set_value($form['fields']['_add_new_group']['group_name'], $group_name, $form_state);
}
// Invalid group name.
if (!preg_match('!^group_[a-z0-9_]+$!', $group_name)) {
form_set_error('fields][_add_new_group][group_name', t('Add new group: the group name %group_name is invalid. The name must include only lowercase unaccentuated letters, numbers, and underscores.', array('%group_name' => $group_name)));
}
if (backdrop_strlen($group_name) > 32) {
form_set_error('fields][_add_new_group][group_name', t("Add new group: the group name %group_name is too long. The name is limited to 32 characters, including the 'group_' prefix.", array('%group_name' => $group_name)));
}
// Group name already exists.
if (field_group_exists($group_name, $entity_type, $bundle, $mode)) {
form_set_error('fields][_add_new_group][group_name', t('Add new group: the group name %group_name already exists.', array('%group_name' => $group_name)));
}
}
}
}
/**
* Submit handler for the overview screens.
*
* @param array $form
* The complete form.
* @param array $form_state
* The state of the form.
*/
function field_group_field_overview_submit($form, &$form_state) {
$form_values = $form_state['values']['fields'];
$entity_type = $form['#entity_type'];
$bundle = $form['#bundle'];
$mode = (isset($form['#view_mode']) ? $form['#view_mode'] : 'form');
// Collect children.
$children = array_fill_keys($form['#groups'], array());
foreach ($form_values as $name => $value) {
if (!empty($value['parent'])) {
// Substitute newly added fields, in case they were dragged
// directly in a group.
if ($name == '_add_new_field' && isset($form_state['fields_added']['_add_new_field'])) {
$name = $form_state['fields_added']['_add_new_field'];
}
elseif ($name == '_add_existing_field' && isset($form_state['fields_added']['_add_existing_field'])) {
$name = $form_state['fields_added']['_add_existing_field'];
}
$children[$value['parent']][$name] = $name;
}
}
// Create new group.
if (!empty($form_values['_add_new_group']['group_name'])) {
$values = $form_values['_add_new_group'];
$field_group_types = field_group_formatter_info();
$formatter = $field_group_types[($mode == 'form' ? 'form' : 'display')][$values['format']['type']];
$new_group = (object) array(
'group_name' => $values['group_name'],
'entity_type' => $entity_type,
'bundle' => $bundle,
'mode' => $mode,
'children' => isset($children['_add_new_group']) ? array_keys($children['_add_new_group']) : array(),
'parent_name' => $values['parent'],
'weight' => $values['weight'],
'label' => $values['label'],
'format_type' => $values['format']['type'],
);
$new_group->format_settings = array('formatter' => isset($formatter['default_formatter']) ? $formatter['default_formatter'] : '');
if (isset($formatter['instance_settings'])) {
$new_group->format_settings['instance_settings'] = $formatter['instance_settings'];
}
$classes = _field_group_get_html_classes($new_group);
$new_group->format_settings['instance_settings']['classes'] = implode(' ', $classes->optional);
field_group_group_save($new_group, TRUE);
// Store new group information for any additional submit handlers.
$form_state['groups_added']['_add_new_group'] = $new_group->group_name;
backdrop_set_message(t('New group %label successfully created.', array('%label' => $new_group->label)));
// Replace the newly created group in the $children array, in case it was
// dragged directly in an existing field.
foreach (array_keys($children) as $parent) {
if (isset($children[$parent]['_add_new_group'])) {
unset($children[$parent]['_add_new_group']);
$children[$parent][$new_group->group_name] = $new_group->group_name;
}
}
}
// Update existing groups.
$groups = field_group_info_groups($entity_type, $bundle, $mode, TRUE);
foreach ($form['#groups'] as $group_name) {
$group = $groups[$group_name];
$group->label = $form_state['field_group'][$group_name]->label;
$group->children = array_keys($children[$group_name]);
$group->parent_name = $form_values[$group_name]['parent'];
$group->weight = $form_values[$group_name]['weight'];
$old_format_type = $group->format_type;
$group->format_type = isset($form_values[$group_name]['format']['type']) ? $form_values[$group_name]['format']['type'] : 'visible';
if (isset($form_state['field_group'][$group_name]->format_settings)) {
$group->format_settings = $form_state['field_group'][$group_name]->format_settings;
}
// If the format type is changed, make sure we have all required format settings.
if ($group->format_type != $old_format_type) {
$mode = $group->mode == 'form' ? 'form' : 'display';
$default_formatter_settings = _field_group_get_default_formatter_settings($group->format_type, $mode);
$group->format_settings += $default_formatter_settings;
$group->format_settings['instance_settings'] += $default_formatter_settings['instance_settings'];
}
field_group_group_save($group, FALSE);
}
cache_clear_all('field_groups', 'cache_field');
}
/**
* Validate the entered css class from the submitted format settings.
*
* @param array $element
* The validated element.
* @param array $form_state
* The state of the form.
*/
function field_group_validate_css_class($element, &$form_state) {
if (!empty($form_state['values']['fields'][$form_state['formatter_settings_edit']]['format_settings']['settings']['instance_settings']['classes']) && !preg_match('!^[A-Za-z0-9-_ ]+$!', $form_state['values']['fields'][$form_state['formatter_settings_edit']]['format_settings']['settings']['instance_settings']['classes'])) {
form_error($element, t('The css class must include only letters, numbers, underscores and dashes.'));
}
}
/**
* Validate the entered id attribute from the submitted format settings.
*
* @param array $element
* The validated element.
* @param array $form_state
* The state of the form.
*/
function field_group_validate_id($element, &$form_state) {
if (!empty($form_state['values']['fields'][$form_state['formatter_settings_edit']]['format_settings']['settings']['instance_settings']['id']) && !preg_match('!^[A-Za-z0-9-_]+$!', $form_state['values']['fields'][$form_state['formatter_settings_edit']]['format_settings']['settings']['instance_settings']['id'])) {
form_error($element, t('The id must include only letters, numbers, underscores and dashes.'));
}
}
/**
* Implements hook_field_info_max_weight().
*/
function field_group_field_info_max_weight($entity_type, $bundle, $context) {
$weights = array();
foreach (field_group_info_groups($entity_type, $bundle, $context) as $group) {
$weights[] = $group->weight;
}
return $weights ? max($weights) : NULL;
}
/**
* Menu callback; present a form for removing a group.
*
* @param array $form
* An associative array containing the structure of the form.
* @param array $form_state
* An associative array containing the current state of the form, which is
* passed by reference.
* @param object $group
* A group definition.
* @param string $view_mode
* The display mode.
*
* @return array
* A form render array.
*/
function field_group_delete_form($form, &$form_state, $group, $view_mode = 'form') {
$form['#group'] = $group;
$admin_path = _field_ui_bundle_admin_path($group->entity_type, $group->bundle);
if ($view_mode == 'form') {
$admin_path .= '/fields';
}
else {
$admin_path .= '/display/' . $view_mode;
}
$form['#redirect'] = array($admin_path);
$output = confirm_form($form,
t('Are you sure you want to delete the group %group?', array('%group' => t($group->label))),
$admin_path,
t('This action cannot be undone.'),
t('Delete'), t('Cancel'),
'confirm'
);
return $output;
}
/**
* Remove group from bundle.
*
* @param array $form
* An associative array containing the structure of the form.
* @param array $form_state
* An associative array containing the current state of the form, which is
* passed by reference.
*
* @todo we'll have to reset all view mode settings.
*/
function field_group_delete_form_submit($form, &$form_state) {
$group = $form['#group'];
$bundle = $group->bundle;
$entity_type = $group->entity_type;
$group->mode = $form_state['build_info']['args'][1];
$bundles = field_info_bundles();
$bundle_label = $bundles[$entity_type][$bundle]['label'];
field_group_group_delete($group);
backdrop_set_message(t('The group %group has been deleted from the %type content type.', array('%group' => t($group->label), '%type' => $bundle_label)));
// Redirect.
$form_state['redirect'] = $form['#redirect'];
}
/**
* Create vertical tabs.
*
* @param array $form
* An associative array containing the structure of the form, which is passed
* by reference.
* @param array $form_state
* An associative array containing the current state of the form.
* @param object $params
* Group parameters.
*/
function field_group_field_ui_create_vertical_tabs(&$form, &$form_state, $params) {
$form_state['field_group_params'] = $params;
$entity_info = entity_get_info($params->entity_type);
$view_modes = array();
if ($params->mode != 'default') {
$view_modes['default'] = t('Default');
}
if ($params->mode != 'form') {
$view_modes['0'] = t('Form');
}
foreach ($entity_info['view modes'] as $view_mode => $data) {
if ($data['custom settings'] && $params->mode != $view_mode) {
$view_modes[$view_mode] = $data['label'];
}
}
// Add additional settings vertical tab.
if (!isset($form['additional_settings'])) {
$form['additional_settings'] = array(
'#type' => 'vertical_tabs',
'#theme_wrappers' => array('vertical_tabs'),
'#prefix' => '<div>',
'#suffix' => '</div>',
'#tree' => TRUE,
);
$form['#attached']['js'][] = 'core/misc/form.js';
$form['#attached']['js'][] = 'core/misc/collapse.js';
}
// Add extra guidelines for webmaster.
$form['additional_settings']['field_group'] = array(
'#type' => 'fieldset',
'#title' => t('Fieldgroups'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#parents' => array('additional_settings'),
);
$form['additional_settings']['field_group']['fieldgroup_clone'] = array(
'#title' => t('Clone Fieldgroups'),
'#description' => t('Select source view mode or form.'),
'#type' => 'select',
'#options' => $view_modes,
'#default_value' => 'none'
);
$form['additional_settings']['field_group']['fieldgroup_submit'] = array(
'#type' => 'submit',
'#value' => t('Clone'),
'#validate' => array('field_group_field_ui_clone_field_groups_validate'),
'#submit' => array('field_group_field_ui_clone_field_groups')
);
$form['additional_settings']['field_group']['help_title'] = array(
'#type' => 'item',
'#title' => t('Configuring fieldgroups'),
);
$form['additional_settings']['field_group']['help'] = array(
'#type' => 'help',
'#markup' => t('<p>Fields can be dragged into groups with unlimited nesting. Each Fieldgroup format comes with a configuration form, specific for that format type.</p><p>Some formats come in pairs. These types have an HTML wrapper to nest its fieldgroup children. For example, place accordion items into the accordion; vertical tabs into the vertical tab group; and horizontal tabs into the horizontal tab group. There is one exception to this rule: you can use a vertical tab without a wrapper when the additional settings tabs are available, such as with content type forms.</p>'),
);
}
/**
* Validate handler to validate saving existing fieldgroups from one view mode or form to another.
*
* @param array $form
* An associative array containing the structure of the form.
* @param array $form_state
* An associative array containing the current state of the form, which is
* passed by reference.
*/
function field_group_field_ui_clone_field_groups_validate($form, &$form_state) {
$source_mode = $form_state['#source_mode'] = $form_state['values']['additional_settings']['fieldgroup_clone'] == '0' ? 'form' : $form_state['values']['additional_settings']['fieldgroup_clone'];
$groups_to_clone = $form_state['#groups_to_clone'] = field_group_read_groups(array('bundle' => $form_state['field_group_params']->bundle, 'entity_type' => $form_state['field_group_params']->entity_type, 'mode' => $source_mode));
$form_state['#source_groups'] = array();
// @todo this is probably not necessary since field_group_read_groups already filters by these.
if (!empty($groups_to_clone) && isset($groups_to_clone[$form_state['field_group_params']->entity_type], $groups_to_clone[$form_state['field_group_params']->entity_type][$form_state['field_group_params']->bundle], $groups_to_clone[$form_state['field_group_params']->entity_type][$form_state['field_group_params']->bundle][$source_mode])) {
$form_state['#source_groups'] = $groups_to_clone[$form_state['field_group_params']->entity_type][$form_state['field_group_params']->bundle][$source_mode];
}
// Check for types are not known in current mode.
if ($form_state['field_group_params']->mode != 'form') {
$non_existing_types = array('multipage', 'multipage-group');
}
if (!empty($form_state['#source_groups'])) {
$field_instances = field_info_instances($form_state['field_group_params']->entity_type, $form_state['field_group_params']->bundle);
$visible_fields = array();
foreach ($field_instances as $field_name => $field_instance) {
if ($field_instance['display'][$form_state['field_group_params']->mode]['type'] != 'hidden') {
$visible_fields[] = $field_name;
}
}
$form_state['#visible_fields'] = $visible_fields;
}
foreach ($form_state['#source_groups'] as $key => $group) {
if (in_array($group->format_type, $non_existing_types)) {
unset($form_state['#source_groups'][$key]);
backdrop_set_message(t('Skipping @group because this type does not exist in current mode', array('@group' => $group->label)), 'warning');
}
}
if (empty($form_state['#source_groups'])) {
// Report error found with selection.
form_set_error('additional_settings][fieldgroup_clone', t('No field groups were found in selected view mode.'));
return;
}
}
/**
* Submit handler to save existing fieldgroups from one view mode or form to another.
*
* @param array $form
* An associative array containing the structure of the form.