-
Notifications
You must be signed in to change notification settings - Fork 0
/
flexiform.flexiform.inc
575 lines (511 loc) · 18 KB
/
flexiform.flexiform.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
<?php
/**
* @file
* All the flexiform hooks.
*/
/**
* Implements hook_flexiform_group_info().
*/
function flexiform_flexiform_group_info() {
return array(
'general' => array(
'label' => t('General'),
),
'test' => array(
'label' => t('Test'),
),
);
}
/**
* Implements hook_flexiform_builder_info().
*/
function flexiform_flexiform_builder_info() {
$builders = array();
$builders['FlexiformBuilderFlexiform'] = array(
'class' => 'FlexiformBuilderFlexiform',
'label' => t('Flexiform Form Builder'),
'description' => t('The flexiform custom form builder. Use the configuration pages to add fields and entities to the form.'),
);
$fieldable_entities = array();
foreach (entity_get_info() as $entity_type => $info) {
if ($info['fieldable']) {
$fieldable_entities[] = $entity_type;
}
}
$builders['FlexiformBuilderEntityForm'] = array(
'class' => 'FlexiformBuilderEntityForm',
'label' => t('Entity Field Form'),
'description' => t('Render the standard entity field form.'),
'entity_types' => $fieldable_entities,
);
return $builders;
}
/**
* Implements hook_flexiform_entity_getter_info().
*/
function flexiform_flexiform_entity_getter_info() {
$getters = array();
// Base entity getter.
$getters['base_entity'] = array(
'label' => 'Base Entity',
'description' => 'The Base Entity for this Flexiform',
'entity_types' => array_keys(entity_get_info()),
'class' => 'FlexiformFormEntityBaseEntity',
);
// Load entity getter.
$getters['load_entity'] = array(
'label' => 'Load Entity from ID',
'description' => 'Load the entity based on id.',
'entity_types' => array_keys(entity_get_info()),
'class' => 'FlexiformFormEntityLoad',
);
// New entity getter.
$getters['new_entity'] = array(
'label' => 'Create a New Entity',
'description' => 'Create a new entity.',
'entity_types' => array_keys(entity_get_info()),
'class' => 'FlexiformFormEntityNew',
);
// User Getters
$getters['user_current_user'] = array(
'label' => 'Current User',
'description' => 'Load the current user into the Form',
'entity_types' => array('user'),
'class' => 'FlexiformFormEntityCurrentUser',
);
// Profile2 Getters
if (module_exists('profile2')) {
$getters['profile2_profile_from_user'] = array(
'label' => 'Profile2 from User',
'description' => 'Load a Profile 2 Basede on a User',
'params' => array(
'user' => array(
'entity_type' => 'user',
),
),
'entity_types' => array('profile2'),
'file' => 'profile2.flexiform.inc',
);
}
// Party Getters.
if (module_exists('party')) {
// Getters for data sets.
foreach (party_get_data_set_info() as $data_set_name => $info) {
$getters[$data_set_name . '_from_party'] = array(
'label' => t('@label data set', array('@label' => $info['label'])),
'description' => t('Load a data set from a party.'),
'data_set_name' => $data_set_name,
'params' => array(
'party' => array(
'entity_type' => 'party',
),
),
'entity_types' => array($info['entity type']),
'class' => 'FlexiformFormEntityPartyDataSet',
);
}
// Party from user Getters
$getters['party_from_user'] = array(
'label' => 'Party from User',
'description' => 'Load a Party Based on a User',
'params' => array(
'user' => array(
'entity_type' => 'user',
),
),
'entity_types' => array('party'),
'class' => 'FlexiformFormEntityPartyFromUser',
);
}
// Build entity reference getters.
if (module_exists('entityreference')) {
$fields = db_select('field_config', 'fc')
->condition('type', 'entityreference')
->fields('fc', array('field_name'))
->execute()
->fetchCol();
foreach ($fields as $field_name) {
// Get all the info we need.
$field = field_info_field($field_name);
$target_entity_type = $field['settings']['target_type'];
$target_entity_info = entity_get_info($target_entity_type);
// Build a getter for each entity_type this field is present on.
foreach ($field['bundles'] as $base_entity_type => $bundles) {
$base_entity_info = entity_get_info($base_entity_type);
// Prepare replacements for the label.
$label_replacements = array(
'@target' => $target_entity_info['label'],
'@base' => $base_entity_info['label'],
'@field' => $field_name,
);
// Build getter info.
$getters["entityreference__{$field_name}__{$base_entity_type}"] = array(
'label' => t('@target from @base via @field', $label_replacements),
'description' => t('Load an entity via an entityreference field'),
'field_name' => $field_name,
'params' => array(
'base' => array(
'entity_type' => $base_entity_type,
),
),
'entity_types' => array($target_entity_type),
'class' => 'FlexiformFormEntityEntityReference',
);
}
}
}
// Build field collection getters.
if (module_exists('field_collection')) {
$fields = db_select('field_config', 'fc')
->condition('type', 'field_collection')
->fields('fc', array('field_name'))
->execute()
->fetchCol();
foreach ($fields as $field_name) {
$field = field_info_field($field_name);
// Build a getter for each entity_type this field is present on.
foreach ($field['bundles'] as $base_entity_type => $bundles) {
$base_entity_info = entity_get_info($base_entity_type);
// Prepare replacements for the label.
$label_replacements = array(
'@base' => $base_entity_info['label'],
'@field' => $field_name,
);
$getters["field_collection__{$field_name}__{$base_entity_type}"] = array(
'label' => t('Field Collection Item: @field from @base', $label_replacements),
'description' => t('Load the field collection item'),
'field_name' => $field_name,
'params' => array(
'base' => array(
'entity_type' => $base_entity_type,
),
),
'entity_types' => array('field_collection_item'),
'class' => 'FlexiformFormEntityFieldCollection',
);
}
}
}
// Build reply module getters.
if (module_exists('reply')) {
$fields = db_select('field_config', 'fc')
->condition('type', 'reply')
->fields('fc', array('field_name'))
->execute()
->fetchCol();
foreach ($fields as $field_name) {
$field = field_info_field($field_name);
foreach ($field['bundles'] as $base_entity_type => $bundles) {
$base_entity_info = entity_get_info($base_entity_type);
// Prepare replacements for the label.
$label_replacements = array(
'@base' => $base_entity_info['label'],
'@field' => $field_name,
);
$getters["reply_new__{$field_name}__{$base_entity_type}"] = array(
'label' => t('New Reply: @field from @base', $label_replacements),
'description' => t('Get a new reply on the entity.'),
'field_name' => $field_name,
'params' => array(
'base' => array(
'entity_type' => $base_entity_type,
),
),
'entity_types' => array('reply'),
'class' => 'FlexifromFormEntityNewReply',
);
}
}
}
if (module_exists('commerce_line_item')) {
$getters['commerce_line_item_new'] = array(
'label' => t('New Line Item for Order'),
'description' => t('Get a new line item for the order.'),
'params' => array(
'base' => array(
'entity_type' => 'commerce_order',
),
),
'entity_types' => array('commerce_line_item'),
'class' => 'FlexiformFormEntityNewCommerceLineItem',
);
}
if (module_exists('commerce_order')) {
$getters['commerce_order_user'] = array(
'label' => t('User from Order'),
'description' => t('Get a user from the order.'),
'params' => array(
'base' => array(
'entity_type' => 'commerce_order',
),
),
'entity_types' => array('user'),
'class' => 'FlexiformFormEntityCommerceOrderUser',
);
}
return $getters;
}
/**
* Implements hook_flexiform_display_info().
*/
function flexiform_flexiform_display_info() {
$displays = array();
$displays['FlexiformDisplayCreatePage'] = array(
'label' => t('Add page'),
'description' => t('A page to add a new submission of the form. Will create a new base entity.'),
);
$displays['FlexiformDisplayEditPage'] = array(
'label' => t('Edit page'),
'description' => t('A page to an edit form. Will load an existing base entity.'),
);
if (module_exists('ctools')) {
$displays['FlexiformDisplayAddPane'] = array(
'label' => t('Add Pane (Ctools Content)'),
'description' => t('Expose the form in a Ctools Content Plugin.'),
);
$displays['FlexiformDisplayEditPane'] = array(
'label' => t('Edit Pane (Ctools Content)'),
'description' => t('Expose the form in a Ctools Content Plugin.'),
);
$displays['FlexiformDisplayCreateModal'] = array(
'label' => t('Add Modal Form'),
'description' => t('Expose the form in a Ctools modal.'),
);
$displays['FlexiformDisplayEditModal'] = array(
'label' => t('Edit Modal Form'),
'description' => t('Expose the form in a Ctools modal.'),
);
}
if (module_exists('user')) {
$displays['FlexiformDisplayUserCategory'] = array(
'label' => t('User category'),
'description' => t('Expose this form as a tab under user edit.'),
);
}
if (module_exists('commerce_checkout')) {
$displays['FlexiformDisplayCheckoutPane'] = array(
'label' => t('Checkout Pane'),
'description' => t('Expose this form as a checkout pane. (Commerce Order must be the base entity)'),
);
}
return $displays;
}
/**
* Implements hook_flexiform_element_info().
*/
function flexiform_flexiform_element_info() {
$elements = array();
// Get the element for all nested flexiforms.
$forms = db_select('flexiform', 'f')
->fields('f', array('label', 'form', 'base_entity', 'base_entity_bundle'))
->execute()
->fetchAllAssoc('form');
foreach ($forms as $form) {
$elements[$form->base_entity][$form->base_entity_bundle]['flexiform:' . $form->form] = array(
'label' => $form->label,
'class' => 'FlexiformElementFlexiform',
'type' => 'form',
'group' => 'Flexiform',
'form' => $form->form,
);
}
// Get all entity properties
foreach (entity_get_property_info() as $entity_type => $info) {
$entity_properties = empty($info['properties']) ? array() : $info['properties'];
if (isset($info['bundles'])) {
foreach ($info['bundles'] as $bundle => $binfo) {
$properties = empty($binfo['properties']) ? array() : $binfo['properties'];
$properties += $entity_properties;
foreach ($properties as $name => $property) {
// If it is a field api field. Use the field api logic later.
if (!empty($property['field'])) {
continue;
}
else if (!empty($property['setter callback'])) {
$class = 'FlexiformElementEntityProperty';
if (!empty($property['type'])) {
$type = $property['type'];
if ($list = entity_property_list_extract_type($type)) {
$type = $list;
}
$class = 'FlexiformElementEntityProperty_' . $type;
}
$elements[$entity_type][$bundle]['property:' . $name] = array(
'label' => $property['label'],
'class' => class_exists($class) ? $class : 'FlexiformElementEntityProperty',
'type' => 'property',
'group' => 'Property',
'property' => $name,
);
}
}
// Add field_api_fields.
foreach (field_info_instances($entity_type, $bundle) as $field_name => $instance) {
$field = field_info_field($field_name);
$field_class = 'FlexiformElementField_' . $field['type'];
$elements[$entity_type][$bundle]['field:' . $field_name] = array(
'label' => $instance['label'],
'class' => class_exists($field_class) ? $field_class : 'FlexiformElementField',
'type' => 'field',
'group' => 'Field',
'field_name' => $field_name,
);
}
}
}
else {
foreach ($entity_properties as $name => $property) {
if (!empty($property['setter callback'])) {
$class = 'FlexiformElementEntityProperty';
if (!empty($property['type'])) {
$class = 'FlexiformElementEntityProperty_' . $property['type'];
}
$elements[$entity_type][$entity_type]['property:' . $name] = array(
'label' => $property['label'],
'class' => class_exists($class) ? $class : 'FlexiformElementEntityProperty',
'type' => 'property',
'group' => 'Property',
'property' => $name,
);
}
}
}
}
// Get all the field elements.
/*$fields_info = field_info_instances();
foreach ($fields_info as $entity_type => $entity_fields) {
foreach ($entity_fields as $bundle => $bundle_fields) {
foreach ($bundle_fields as $field_name => $instance) {
$field = field_info_field($instance['field_name']);
$field_class = 'FlexiformElementField_' . $field['type'];
$elements[$entity_type][$bundle]['field:' . $field_name] = array(
'label' => $instance['label'],
'class' => class_exists($field_class) ? $field_class : 'FlexiformElementField',
'type' => 'field',
'group' => 'Field',
'field_name' => $field_name,
);
}
}
}*/
// Get node elements
foreach (node_type_get_types() as $bundle => $info) {
if ($info->has_title) {
$elements['node'][$bundle]['node:title'] = array(
'label' => $info->title_label,
'class' => 'FlexiformElementNodeTitle',
'type' => 'title',
'group' => 'Node',
);
}
$elements['node'][$bundle]['node:author'] = array(
'label' => t('Authored by'),
'class' => 'FlexiformElementNodeAuthor',
'type' => 'author',
'group' => 'Node',
);
$elements['node'][$bundle]['node:status'] = array(
'label' => t('Published'),
'class' => 'FlexiformElementNodeStatus',
'type' => 'status',
'group' => 'Node',
);
$elements['node'][$bundle]['node:promote'] = array(
'label' => t('Promoted to front page'),
'class' => 'FlexiformElementNodePromote',
'type' => 'promote',
'group' => 'Node',
);
$elements['node'][$bundle]['node:sticky'] = array(
'label' => t('Sticky at top of lists'),
'class' => 'FlexiformElementNodeSticky',
'type' => 'sticky',
'group' => 'Node',
);
}
$elements['user']['user']['user:name'] = array(
'label' => t('Name'),
'class' => 'FlexiformElementUserName',
'type' => 'name',
'group' => 'User',
);
$elements['user']['user']['user:pass'] = array(
'label' => t('Password'),
'class' => 'FlexiformElementUserPass',
'type' => 'pass',
'group' => 'User',
);
$elements['user']['user']['user:mail'] = array(
'label' => t('E-mail'),
'class' => 'FlexiformElementUserMail',
'type' => 'mail',
'group' => 'User',
);
$elements['user']['user']['user:roles'] = array(
'label' => t('Roles'),
'class' => 'FlexiformElementUserRoles',
'type' => 'roles',
'group' => 'User',
);
// Include ctools content stuff.
$panes = $entity_panes = array();
if (module_exists('ctools')) {
ctools_include('content');
// Trying to make all ctools content types available is to expensive to fit
// into a reasonable load time so limit the exposed content types. Extra
// types can be exposed by changing the value of this variable.
$exposed_types = variable_get('flexiform_exposed_ctools_content', array('views_panes', 'entity_view'));
foreach ($exposed_types as $name) {
$type = ctools_get_content_type($name);
$sub_types = ctools_content_get_subtypes($type);
if (empty($sub_types)) {
continue;
}
foreach ($sub_types as $sub => $info) {
if (!empty($info['required context']) && (!is_array($info['required context']) || count($info['required context']) === 1)) {
$context = $info['required context'];
if (is_array($context)) {
$context = reset($context);
}
if (!$context) {
continue;
}
$entity_type = $context->keywords;
if (substr($entity_type, 0, 7) == 'entity:') {
$entity_type = substr($entity_type, 7);
}
$entity_panes[$entity_type]["{$name}:{$sub}"] = array(
'type' => $name,
'subtype' => $sub,
'title' => $info['title'],
'category' => (is_array($info['category'])) ? reset($info['category']) : $info['category'],
);
}
}
}
}
// Add custom form elements to every entity type.
foreach (entity_get_info() as $entity_type => $info) {
foreach ($info['bundles'] as $bundle => $info) {
$elements[$entity_type][$bundle]['custom:html'] = array(
'label' => t('Custom HTML'),
'class' => 'FlexiformElementCustomHtml',
'type' => 'custom_html',
'group' => 'Custom',
);
if (!empty($entity_panes[$entity_type])) {
foreach ($entity_panes[$entity_type] as $name => $pinfo) {
$elements[$entity_type][$bundle]['ctools:'.$name] = array(
'label' => $pinfo['title'],
'class' => 'FlexiformElementCtoolsContent',
'type' => 'ctools_content',
'group' => $pinfo['category'],
'content_type' => $pinfo['type'],
'sub_type' => $pinfo['subtype'],
);
}
}
}
}
return $elements;
}