-
Notifications
You must be signed in to change notification settings - Fork 0
/
flexiform.module
826 lines (723 loc) · 24.2 KB
/
flexiform.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
<?php
/**
* @file
* Module for the Flexiform system.
*/
/**
* Implements hook_hook_info().
*/
function flexiform_hook_info() {
$hooks['flexiform_group_info'] = array('group' => 'flexiform');
$hooks['flexiform_builder_info'] = array('group' => 'flexiform');
$hooks['flexiform_display_info'] = array('group' => 'flexiform');
$hooks['flexiform_element_info'] = array('group' => 'flexiform');
$hooks['flexiform_entity_getter_info'] = array('group' => 'flexiform');
$hooks['flexiform_ajax_submit_commands_alter'] = array('group' => 'flexiform');
$hooks['flexiform_access'] = array('group' => 'flexiform');
$hooks['flexiform_submission_access'] = array('group' => 'flexiform');
$hooks['default_flexiform'] = array('group' => 'flexiform_default');
$hooks['default_flexiform_alter'] = array('group' => 'flexiform_default');
return $hooks;
}
/**
* Implements hook_menu()
*
* We want to add a manage form fields tab and a manage form entities tab
* for each flexiform
*/
function flexiform_menu() {
$items = array();
// @todo: Consider altering storage for displays so that this can be more performant.
$forms = entity_load('flexiform');
foreach ($forms as $form) {
$displays = flexiform_display_info();
foreach ($displays as $name => $info) {
if (method_exists($info['class'], 'hook_menu')) {
if (($display = $form->getDisplay($name)) && $display->isEnabled()) {
$items += $display->hook_menu();
}
}
}
}
// Entityreference autocomplete flexiform.
if (module_exists('entityreference')) {
$items['flexiform/entityreference/autocomplete/single/%flexiform/%'] = array(
'title' => 'Flexiform Entity Reference Autocomplete',
'page callback' => 'flexiform_entityreference_autocomplete_callback',
'page arguments' => array(3,4,5),
'access callback' => 'flexiform_entityreference_autocomplete_access_callback',
'access arguments' => array(3,4,5),
'file' => 'flexiform.entityreference.inc',
'type' => MENU_CALLBACK,
);
$items['flexiform/entityreference/autocomplete/tags/%flexiform/%'] = array(
'title' => 'Flexiform Entity Reference Autocomplete',
'page callback' => 'flexiform_entityreference_autocomplete_callback',
'page arguments' => array(3,4,5),
'access callback' => 'flexiform_entityreference_autocomplete_access_callback',
'access arguments' => array(3,4,5),
'file' => 'flexiform.entityreference.inc',
'type' => MENU_CALLBACK,
);
}
return $items;
}
/**
* Implements hook_menu_alter()
*
* Allow displays to alter the menu.
*/
function flexiform_menu_alter(&$items) {
// @todo: Consider altering storage for displays so that this can be more performant.
$forms = entity_load('flexiform');
foreach ($forms as $form) {
$displays = flexiform_display_info();
foreach ($displays as $name => $info) {
if (method_exists($info['class'], 'hook_menu_alter')) {
if (($display = $form->getDisplay($name)) && $display->isEnabled()) {
$display->hook_menu_alter($items);
}
}
}
}
// Alter comment bits.
// Adjust the Field UI tabs on admin/structure/types/manage/[node-type].
// See comment_entity_info().
if (!empty($items['admin/structure/types/manage/%comment_node_type/comment/fields']['title'])) {
$items['admin/structure/types/manage/%comment_node_type/comment/fields']['title'] = 'Comment fields';
$items['admin/structure/types/manage/%comment_node_type/comment/fields']['weight'] = 4;
}
if (!empty($items['admin/structure/types/manage/%comment_node_type/comment/display']['title'])) {
$items['admin/structure/types/manage/%comment_node_type/comment/display']['title'] = 'Comment display';
$items['admin/structure/types/manage/%comment_node_type/comment/display']['weight'] = 5;
}
if (!empty($items['admin/structure/types/manage/%comment_node_type/comment/forms']['title'])) {
$items['admin/structure/types/manage/%comment_node_type/comment/forms']['title'] = 'Comment forms';
$items['admin/structure/types/manage/%comment_node_type/comment/forms']['weight'] = 6;
}
}
/**
* Implements hook_user_categories()
*
* Add any user categories being created by flexiforms.
*/
function flexiform_user_categories() {
$categories = array();
$forms = entity_load('flexiform');
foreach ($forms as $form) {
if (($display = $form->getDisplay('FlexiformDisplayUserCategory')) && $display->isEnabled()) {
$categories = array_merge($categories, $display->hook_user_categories());
}
}
return $categories;
}
/**
* Implement hook_entity_info().
*
* We define two entities here - the actual entity that will hold our domain
* specific information and an entity that holds information about the different
* types of entities. See here: http://drupal.org/node/977380 for a discussion on this
* choice.
*/
function flexiform_entity_info() {
// The entity that holds information about the entity types
$return['flexiform'] = array(
'label' => t('Flexible Form'),
'entity class' => 'Flexiform',
'controller class' => 'FlexiformController',
'base table' => 'flexiform',
'fieldable' => FALSE,
'exportable' => TRUE,
'entity keys' => array(
'id' => 'id',
'name' => 'form',
'label' => 'label',
),
'access callback' => 'flexiform_access',
'module' => 'flexiform',
// Enable the entity API's admin UI.
'admin ui' => array(
'path' => 'admin/structure/flexiforms',
'file' => 'flexiform.admin.inc',
'controller class' => 'FlexiformUIController',
),
);
return $return;
}
/**
* Implements hook_views_api().
*/
function flexiform_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'flexiform') . '/includes/views',
);
}
/**
* Impements hook_ctools_plugin_directory().
*/
function flexiform_ctools_plugin_directory($owner, $plugin_type) {
if ($owner == 'panelizer' && $plugin_type == 'entity') {
return "plugins/$plugin_type";
}
if ($owner == 'ctools' && $plugin_type == 'content_types') {
return "plugins/$plugin_type";
}
}
/**
* Implements hook_permission().
*/
function flexiform_permission() {
// We set up permisssions to manage entity types, manage all entities and the
// permissions for each individual entity
$permissions = array(
'administer flexiforms' => array(
'title' => t('Administer flexiforms'),
'description' => t('Create and delete fields for flexiforms, and set their permissions.'),
),
);
return $permissions;
}
/**
* Access callback for the entity API.
*/
function flexiform_access($op, $flexiform = NULL, $account = NULL) {
if (user_access('administer flexiforms', $account)) {
return TRUE;
}
// Allow modules to grant / deny access.
$access = module_invoke_all('flexiform_access', $op, $flexiform, $account);
// Only grant access if at least one module granted access and no one denied
// access.
if (in_array(FALSE, $access, TRUE)) {
return FALSE;
}
elseif (in_array(TRUE, $access, TRUE)) {
return TRUE;
}
return FALSE;
}
/**
* Access callback for widget type form.
*/
function flexiform_field_widget_type_form_access($flexiform, $element_namespace) {
$element = FlexiformElement::getElement($flexiform, $element_namespace);
return ($element instanceof FlexiformElementFieldAPIInterface)
&& user_access('administer flexiforms');
}
/**
* Gets an array of all flexiforms, keyed by the type name.
*
* @param $type_name
* If set, the type with the given name is returned.
* @return Flexiform[]
* Depending whether $type isset, an array of flexiforms or a single one.
*/
function flexiform_get_flexiforms($flexiform_name = NULL) {
// entity_load will get the Entity controller for our flexiform_submission entity and call the load
// function of that object - we are loading entities by name here.
$flexiforms = entity_load_multiple_by_name('flexiform', isset($flexiform_name) ? array($flexiform_name) : FALSE);
return isset($flexiform_name) ? reset($flexiforms) : $flexiforms;
}
/**
* Gets an array of all flexiforms with a given base_entity type and bundle.
*
* @param string $entity_type
* The entity type of base entity.
* @param string $bundle
* The bundle of the base entity.
*
* @return array
* An array of flexiforms.
*/
function flexiform_get_base_entity_forms($entity_type, $bundle = FALSE) {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'flexiform');
$query->propertyCondition('base_entity', $entity_type);
if ($bundle) {
$query->propertyCondition('base_entity_bundle', $bundle);
}
$entities = $query->execute();
if (empty($entities['flexiform'])) {
return array();
}
return entity_load('flexiform', array_keys($entities['flexiform']));
}
/**
* Menu argument loader; Load a flexiform by string.
*
* @param $form
* The machine-readable name of a flexiform to load.
*
* @return
* A flexiform array or FALSE if $type does not exist.
*/
function flexiform_load($form) {
return flexiform_get_flexiforms($form);
}
/**
* Saves a flexiform to the db.
*/
function flexiform_save(Flexiform $form) {
$form->save();
}
/**
* Deletes a flexiform from the db.
*/
function flexiform_delete(Flexiform $form) {
$form->delete();
}
/**
* Split a namespace into entity and field name
*/
function flexiform_formfield_load($string) {
return $string;
}
/**
* Implement hook_theme().
*/
function flexiform_theme() {
return array(
'flexiform' => array(
'render element' => 'element',
'template' => 'flexiform',
),
'flexiform_field_table' => array(
'render element' => 'elements',
),
'flexiform_entity_table' => array(
'render element' => 'elements',
),
'flexiform_ui_overview_item' => array(
'variables' => array('label' => NULL, 'entity_type' => NULL, 'url' => FALSE, 'name' => FALSE, 'tags' => FALSE),
'file' => 'flexiform.admin.inc',
),
);
}
/**
* Preprocessor for flexiforms.
*/
function template_preprocess_flexiform(&$variables) {
$variables['form'] = drupal_render_children($variables['element']);
$variables['form_html_id'] = 'flexiform--' . $variables['element']['#id'];
}
/**
* Implements hook_forms().
*/
function flexiform_forms($form_id, $args) {
$forms = array();
$parts = explode('__', $form_id);
if ($parts[0] == 'flexiform') {
$forms[$form_id] = array(
'callback' => 'flexiform',
'wrapper_callback' => 'flexiform_form_wrapper',
);
}
return $forms;
}
/**
* Flexiform Form Wrapper to ensure that the flexiform include file is always present.
*/
function flexiform_form_wrapper($form, &$form_state) {
form_load_include($form_state, 'inc', 'flexiform', 'includes/flexiform.flexiform');
return $form;
}
/**
* Implements hook_element_info().
*/
function flexiform_element_info() {
return array(
'flexiform_field_table' => array(
'#theme' => 'flexiform_field_table',
'#pre_render' => array('flexiform_field_table_pre_render'),
'#regions' => array('' => array()),
),
'flexiform_entity_table' => array(
'#theme' => 'flexiform_entity_table',
'#pre_render' => array('flexiform_entity_table_pre_render'),
'#regions' => array('' => array()),
),
);
}
/**
* Retreive a list of flexiform groups.
*/
function flexiform_get_groups() {
$groups = &drupal_static(__FUNCTION__, NULL);
if ($groups === NULL) {
$groups = module_invoke_all('flexiform_group_info');
}
return $groups;
}
/**
* Title Callback for Flexiform Submission Pages.
*/
function flexiform_page_title($flexiform, $display, $base_entity_id = NULL) {
if (!is_object($flexiform)) {
$flexiform = flexiform_load($flexiform);
}
if (!$flexiform) {
return FALSE;
}
$context = array(
'base_entity_id' => $base_entity_id,
);
return $flexiform->getDisplay($display)->title($context);
}
/**
* Access callback for flexiform pages.
*/
function flexiform_page_access($flexiform, $display, $base_entity_id = NULL) {
if (!is_object($flexiform)) {
$flexiform = flexiform_load($flexiform);
}
if (!$flexiform) {
return FALSE;
}
$context = array(
'base_entity_id' => $base_entity_id,
);
return $flexiform->getDisplay($display)->access($context);
}
/**
* Access callback for flexiform user categories.
*/
function flexiform_user_category_access($flexiform, $display, $user) {
if (!is_object($flexiform)) {
$flexiform = flexiform_load($flexiform);
}
if (!$flexiform) {
return FALSE;
}
$context = array(
'base_entity_id' => $user,
);
return user_edit_access($user) && $flexiform->getDisplay($display)->access($context);
}
/**
* Implements hook_field_create_instance().
*/
function flexiform_field_create_instance() {
// Clear the element cache.
flexiform_get_element_info('','','', TRUE);
}
//-----------------------------------------------------------------------------
// Flexiform Builder API
/**
* Get info about all available builders.
*/
function flexiform_builder_info($builder = NULL) {
$builders = &drupal_static(__FUNCTION__, array());
if (empty($builders)) {
if ($cache = cache_get('flexiform:builder_info', 'cache')) {
$builders = $cache->data;
}
else {
$builders = array();
foreach (module_implements('flexiform_builder_info') as $module) {
// Due to http://drupal.org/node/890660 we can't use module_invoke_all()
// because we need to know the provenance of each set.
$builders_module = module_invoke($module, 'flexiform_builder_info');
foreach ($builders_module as $builder_name => $builder_info) {
$builder_info += array(
'class' => $builder_name,
'label' => $builder_name,
'module' => $module,
'entity_types' => array(),
);
$builders[$builder_name] = $builder_info;
}
}
// Alter the data sets with hook_party_data_set_info_alter().
drupal_alter('flexiform_builder_info', $builders);
cache_set('flexiform:builder_info', $builders);
}
}
if (!empty($builder)) {
return $builders[$builder];
}
return $builders;
}
/**
* Get the builders available for a particular base entity type.
*/
function flexiform_entity_type_get_builders($entity_type) {
// If this isn't an entity type return false
if (!entity_get_info($entity_type)) {
return FALSE;
}
$builders = flexiform_builder_info();
$applicable_builders = array();
foreach ($builders as $builder => $info) {
if (empty($info['entity_types'])
|| in_array($entity_type, $info['entity_types'])) {
$applicable_builders[$builder] = $info;
}
}
return $applicable_builders;
}
/**
* Access callback for form-entities and form-fields tabs that are only
* available for some builders.
*
* @param Flexiform $flexiform
* The flexiform to check.
* @param string $component
* The component to check access for, normally 'elements' or 'entities'
*/
function flexiform_builder_admin_access($flexiform, $component) {
return user_access('administer flexiforms') && $flexiform->getBuilder()->supportsComponent($component);
}
//-----------------------------------------------------------------------------
// Entity Getter and Setter API
/**
* Get info about all entity getters
*/
function flexiform_entity_getter_info($getter = NULL) {
$getters = &drupal_static(__FUNCTION__);
if (!isset($getters)) {
if ($cache = cache_get('flexiform:entity_getter_info', 'cache')) {
$getters = $cache->data;
}
else {
$getters = array();
foreach (module_implements('flexiform_entity_getter_info') as $module) {
// Due to http://drupal.org/node/890660 we can't use module_invoke_all()
// because we need to know the provenance of each set.
$getters_module = module_invoke($module, 'flexiform_entity_getter_info');
foreach ($getters_module as $getter_name => $getter_info) {
$getter_info += array(
'params' => array(),
'module' => $module,
);
if (!isset($getter_info['class'])) {
$getter_info['class'] = 'FlexiformFormEntityLegacy';
$getter_info += array(
'getter callback' => 'flexiform_entity_getter_' . $getter_name,
'setter callback' => 'flexiform_entity_setter_' . $getter_name,
'file' => $module . '.flexiform.inc',
);
}
$getters[$getter_name] = $getter_info;
}
}
// Alter the data sets with hook_party_data_set_info_alter().
drupal_alter('flexiform_entity_getter_info', $getters);
cache_set('flexiform:entity_getter_info', $getters);
}
}
if (!empty($getter)) {
return $getters[$getter];
}
return $getters;
}
/**
* Get the getters for a particular entity type
*/
function flexiform_entity_type_get_getters($entity_type) {
// If this isn't an entity type return false
if (!entity_get_info($entity_type)) {
return FALSE;
}
$getters = flexiform_entity_getter_info();
$applicable_getters = array();
foreach ($getters as $getter => $info) {
if (in_array($entity_type, $info['entity_types'])) {
$applicable_getters[$getter] = $info;
}
}
return $applicable_getters;
}
//-----------------------------------------------------------------------------
// Flexiform Element API
/**
* Get information about flexiform elements.
*/
function flexiform_get_element_info($entity_type = '', $bundle = '', $name = '', $reset = FALSE) {
$elements = &drupal_static(__FUNCTION__);
if (!isset($elements) || $reset) {
if (($cache = cache_get('flexiform:element_info', 'cache')) && !$reset) {
$elements = $cache->data;
}
else {
$elements = array();
$elements = module_invoke_all('flexiform_element_info');
// Make sure the entity_type, bundle and name are set.
foreach ($elements as $entity_type_ => $elems) {
foreach ($elems as $bundle_ => $els) {
foreach ($els as $name_ => $element) {
$elements[$entity_type_][$bundle_][$name_] += array(
'entity_type' => $entity_type_,
'bundle' => $bundle_,
'name' => $name_,
'group' => 'Other',
);
}
}
}
// Alter the data sets with hook_party_data_set_info_alter().
drupal_alter('flexiform_element_info', $elements);
cache_set('flexiform:element_info', $elements);
}
}
if (!empty($entity_type)) {
if (!empty($bundle)) {
if (!empty($name)) {
return isset($elements[$entity_type][$bundle][$name]) ? $elements[$entity_type][$bundle][$name] : FALSE;
}
return isset($elements[$entity_type][$bundle]) ? $elements[$entity_type][$bundle] : FALSE;
}
return isset($elements[$entity_type]) ? $elements[$entity_type] : FALSE;
}
return $elements;
}
//-----------------------------------------------------------------------------
// Flexiform Display API
/**
* Get information about flexiform displays.
*/
function flexiform_display_info($display = FALSE) {
$displays = &drupal_static(__FUNCTION__, array());
if (empty($displays)) {
if ($cache = cache_get('flexiform:display_info', 'cache')) {
$displays = $cache->data;
}
else {
$displays = array();
foreach (module_implements('flexiform_display_info') as $module) {
// Due to http://drupal.org/node/890660 we can't use module_invoke_all()
// because we need to know the provenance of each set.
$displays_module = module_invoke($module, 'flexiform_display_info');
foreach ($displays_module as $display_name => $display_info) {
$display_info += array(
'class' => $display_name,
'label' => $display_name,
'module' => $module,
'entity_types' => array(),
);
$displays[$display_name] = $display_info;
}
}
// Alter the data sets with hook_party_data_set_info_alter().
drupal_alter('flexiform_display_info', $displays);
cache_set('flexiform:display_info', $displays);
}
}
if (!empty($display)) {
return empty($displays[$display]) ? FALSE : $displays[$display];
}
return $displays;
}
/**
* Get display plugins for a particular entity type.
*/
function flexiform_entity_type_get_displays($entity_type) {
// If this isn't an entity type return false
if (!entity_get_info($entity_type)) {
return FALSE;
}
$displays = flexiform_display_info();
$applicable_displays = array();
foreach ($displays as $display => $info) {
if (empty($info['entity_types'])
|| in_array($entity_type, $info['entity_types'])) {
$applicable_displays[$display] = $info;
}
}
return $applicable_displays;
}
/**
* Display a flexiform.
*/
function flexiform_display($flexiform, $display, $context = array()) {
return $flexiform->getDisplay($display)->build($context);
}
/**
* Implements hook_ctools_access_get.
*/
function flexiform_ctools_access_get($argument) {
$flexiform = flexiform_load($argument);
try {
$contexts = $flexiform->getAccessController()->prepareContexts();
}
catch (Exception $e) {
return array();
}
return array($flexiform->getAccessController()->getCtoolsSettings(), $contexts);
}
/**
* Implements hook_ctools_access_set().
*/
function flexiform_ctools_access_set($argument, $access) {
$flexiform = flexiform_load($argument);
$flexiform->getAccessController()->setCtoolsSettings($access);
}
/**
* Access callback for flexiform entityreference autocomplete.
*/
function flexiform_entityreference_autocomplete_access_callback($type, $flexiform, $element_namespace) {
try {
$element = FlexiformElement::getElement($flexiform, $element_namespace);
$element_field = $element->getField();
if ($element_field['type'] != 'entityreference') {
return FALSE;
}
// @todo: Check Flexiform Access.
return TRUE;
}
catch (Exception $e) {
return FALSE;
}
}
/**
* Implements hook_flexiform_build_alter().
*/
function flexiform_flexiform_build_alter(&$form, &$form_state, $flexiform) {
// Manually adding support for field_collection's
// "field_collection_field_attach_form" hook that the
// Flexiform builder doesn't invoke.
foreach($flexiform->elements as $element_namespace => $element_info) {
// If we're not dealing with a field (i.e.: potentially a property),
// then skip this element.
if (!isset($element_info['field_name'])) {
continue;
}
$field_name = $element_info['field_name'];
$field = field_info_field($field_name);
$element = &$form[$element_namespace];
$entity_info = $flexiform->entities[$element_info['entity_namespace']];
$entity_type = $entity_info['entity_type'];
if ($field['type'] == 'field_collection') {
$instance = field_info_instance($entity_type, $field_name, $entity_info['bundle']);
// Can access the field and is target widget type?
if(field_access('edit', $field, $entity_type)
&& in_array($instance['widget']['type'], array('field_collection_embed', 'field_collection_table'))) {
if (field_collection_hide_blank_items($field)) {
$element_langcode = $element['#language'];
if ($element[$element_langcode]['#max_delta'] > 0) {
$element[$element_langcode]['#max_delta']--;
}
// Remove blank form elements and force user to explicitly add a field
// collection if both 'hide_initial_item' and 'hide_blank_items' are TRUE.
if ($field['settings']['hide_initial_item']
&& $field['settings']['hide_blank_items']
&& field_collection_item_is_empty($element[$element_langcode][0]['#entity'])) {
_field_collection_process_children_attached($element[$element_langcode][0]);
unset($element[$element_langcode][0]);
unset($form_state['field']['#parents'][$field_name][$element_langcode][0]);
}
}
if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED
&& empty($form_state['programmed'])) {
$element_langcode = $element['#language'];
$element_wrapper = $element[$element_langcode]['add_more']['#ajax']['wrapper'];
for ($i = 0; $i <= $element[$element_langcode]['#max_delta']; $i++) {
if (isset($element[$element_langcode][$i]['remove_button'])) {
$element[$element_langcode][$i]['remove_button']['#ajax']['wrapper'] = $element_wrapper;
}
}
}
}
}
}
}