-
Notifications
You must be signed in to change notification settings - Fork 0
/
flexiform.api.php
379 lines (342 loc) · 11.2 KB
/
flexiform.api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
/**
* @file
* API documentation for Flexiform.
*/
/**
* Inform flexiform of a group of forms.
*
* Flexiforms are assigned to a group to allow additional logic to be performed
* by other modules. For example, a module may define a group which it then
* adds additional fields to that are relevant for that form group.
*
* @return
* An array whose keys are the value for the group and whose values are an
* an array with the following:
* - label: The human-readable name of the group.
* - locked: Set to TRUE to prevent forms being created in this group through
* the UI.
*/
function hook_flexiform_group_info() {
return array(
'application' => array(
'label' => t('Application'),
// We want site builders to use the UI for this group.
'locked' => FALSE,
),
);
}
/**
* Alter a flexiform as it gets built.
*
* @param array $form
* The form array that has been built by the flexiform builder.
* @param array $form_state
* The form_state of the form.
* @param Flexiform $flexiform
* The flexiform object.
*
* @see FlexiformBuilder::invoke()
* @see FlexiformBuilderFlexiform::form()
*/
function hook_flexiform_build_alter(&$form, &$form_state, $flexiform) {
}
/**
* Alter a flexiform as it gets built by a particular builder.
*
* @param array $form
* The form array that has been built by the flexiform builder.
* @param array $form_state
* The form_state of the form.
* @param Flexiform $flexiform
* The flexiform object.
*
* @see FlexiformBuilder::invoke()
* @see FlexiformBuilderFlexiform::form()
* @see flexiform_get_builder_info()
*/
function hook_flexiform_build_FLEXIFORM_BUILDER_alter(&$form, &$form_state, $flexiform) {
}
/**
* Act on the validation of a flexiform.
*
* @param array $form
* The form array that has been built by the flexiform builder.
* @param array $form_state
* The form_state of the form.
* @param Flexiform $flexiform
* The flexiform object.
*
* @see FlexiformBuilder::invoke()
* @see FlexiformBuilderFlexiform::formValidate()
*/
function hook_flexiform_build_validate_alter(&$form, &$form_state, $flexiform) {
}
/**
* Act on the validation of a flexiform built by a particular builder.
*
* @param array $form
* The form array that has been built by the flexiform builder.
* @param array $form_state
* The form_state of the form.
* @param Flexiform $flexiform
* The flexiform object.
*
* @see FlexiformBuilder::invoke()
* @see FlexiformBuilderFlexiform::formValidate()
*/
function hook_flexiform_build_FLEXIFORM_BUILDER_validate_alter(&$form, &$form_state, $flexiform) {
}
/**
* Act on the submission of a flexiform.
*
* @param array $form
* The form array that has been built by the flexiform builder.
* @param array $form_state
* The form_state of the form.
* @param Flexiform $flexiform
* The flexiform object.
*
* @see FlexiformBuilder::invoke()
* @see FlexiformBuilderFlexiform::formSubmit()
*/
function hook_flexiform_build_submit_alter(&$form, &$form_state, $flexiform) {
}
/**
* Act on the submission of a flexiform built by a particular builder.
*
* @param array $form
* The form array that has been built by the flexiform builder.
* @param array $form_state
* The form_state of the form.
* @param Flexiform $flexiform
* The flexiform object.
*
* @see FlexiformBuilder::invoke()
* @see FlexiformBuilderFlexiform::formSubmit()
*/
function hook_flexiform_build_FLEXIFORM_BUILDER_submit_alter(&$form, &$form_state, $flexiform) {
}
/**
* Register builder classes with the Flexiform system.
*
* Builders are used to turn the configuration stored in the flexiform entity
* into a usable FAPI form.
*
* @return
* An array whose keys are unique builder machine-names and whose values are
* arrays of important information about the builders that must contain the
* following keys:
* - class: The name of the class used to build the form.
* - label: The human-readable name of the builder.
* - description: A description that will be used as help text on the
* flexiform config form.
* - entity_types: An array of entity types that this builder can build forms
* for. Defaults to all entity types if left blank.
*/
function hook_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;
}
/**
* Alter flexiform builder info.
*
* @param $builder_info
* The builder info array, keyed by builder machine-name.
*
* @see hook_flexiform_builder_info()
*/
function hook_flexiform_builder_info_alter(&$builder_info) {
}
/**
* Define elements for use in flexiforms.
*
* Elements should be defined in a multi-dimensional array keyed by entity
* type, bundle and name. The whole definition for an element will be passed to
* the element constructor so it is possible to store other settings in this
* array as necessary.
*
* @return
* A multidimensional array of elements keyed by entity_type, bundle and an
* element_name that must be unique for that entity-bundle. Each element
* definition should be an array with the following keys:
* - label: The human readable label for the element, this can usually be
* overridden once the element is in a form.
* - class: The Element class to use for the rendering of the element.
* - group: (optional) What group should the element appear in when being
* selected to be added to the form. Defaults to 'Other'.
*/
function hook_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 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) {
$elements[$entity_type][$bundle]['field:' . $field_name] = array(
'label' => $instance['label'],
'class' => 'FlexiformElementField',
'type' => 'field',
'group' => 'Field',
'field_name' => $field_name,
);
}
}
}
return $elements;
}
/**
* Alter flexiform element info.
*
* @param $element_info
* The element info array, keyed by element name.
*
* @see hook_flexiform_element_info()
*/
function hook_flexiform_element_info_alter(&$element_info) {
}
/**
* Register entity getters/setters with the flexiform system.
*
* Entity getters are used by flexiform to load entities into the flexiform.
*
* @return
* An array of getter definitions keyed by a unique getter name. Each
* definition should have the following keys:
* - label (required): A human readable name for the getter
* - description: A Description of the getter
* - entity_types (required): An array of entity types this getter can return
* - file: Which file is the getter in
* - params: An array of parameters for the getter keyed by variable name
* with the following keys:
* - entity_type: What type of entity to expect
* - getter callback: the name of the function to call for the getter
* (defaults to flexiform_entity_getter_GETTER_NAME)
* - setter callback: the name of the function to call to save the entity
*/
function hook_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()),
'file' => 'includes/flexiform.flexiform.inc',
);
// User Getters
$getters['user_current_user'] = array(
'label' => 'Current User',
'description' => 'Load the current user into the Form',
'entity_types' => array('user'),
'file' => 'user.flexiform.inc',
);
// 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',
);
}
return $getters;
}
/**
* Alter flexiform entity getter info.
*
* @param $entity_getter_info
* The entity getter info array, keyed by entity getter name.
*
* @see hook_flexiform_entity_getter_info()
*/
function hook_flexiform_entity_getter_info_alter(&$entity_getter_info) {
}
/**
* Prepare the base entity of a flexiform.
*
* @param $base_entity
* The base entity of the form, provided by the FlexiformDisplay.
* @param Flexiform $flexiform
* The flexiform that is about to be built.
* @param FlexiformDisplayBase $display
* The flexiform display handler that has provided the base entity.
*
* @see FlexiformDisplayBase::build()
*/
function hook_flexiform_prepare_base_entity($base_entity, Flexiform $flexiform, FlexiformDisplayBase $display) {
global $user;
// Default an entity reference field to the current user id.
// This allows the form to use the entityreference getter to load this
// user into the form.
if ($flexiform->form == 'node_create_form') {
$base_entity->field_author[LANGUAGE_NONE][0] = array(
'target_id' => $user->uid,
);
}
}
/**
* Alter the form wrapper for a Flexiform.
*
* @param callable $wrapper
* The form wrapper callback.
* @param FlexiformDisplayInterface $display
* The display being built.
* @param array $context
* The context in which the form is being built.
*/
function hook_flexiform_wrapper_alter($wrapper, FlexiformDisplayInterface $display, $context) {
// Swap the wrapper to our custom one for a particular flexiform.
if ($display->getFlexiform()->form == 'my_form') {
$wrapper = 'my_wrapper';
}
}
/**
* Alter the AJAX commands before they are sent on the submission of a form.
*
* @param array $commands
* The ajax commands that are about to be sent.
* @param array $context
* And array of helpful contextual information, including:
* - flexiform: The flexiform configuration entity,
* - form: The built form array.
* - form_state: The form state of the submitted element.
*/
function hook_flexiform_ajax_submit_commands_alter(&$commands, $context) {
// No Example.
}