-
Notifications
You must be signed in to change notification settings - Fork 13
/
webform.api.php
1568 lines (1454 loc) · 50.5 KB
/
webform.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
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
* Sample hooks demonstrating usage in Webform.
*/
/**
* @defgroup webform_hooks Webform Module Hooks
* @{
* Webform's hooks enable other modules to intercept events within Webform, such
* as the completion of a submission or adding validation. Webform's hooks also
* allow other modules to provide additional components for use within forms.
*/
/**
* Define callbacks that can be used as select list options.
*
* When users create a select component, they may select a pre-built list of
* certain options. Webform core provides a few of these lists such as the
* United States, countries of the world, and days of the week. This hook
* provides additional lists that may be utilized.
*
* @see webform_options_example()
* @see hook_webform_select_options_info_alter()
*
* @return array
* An array of callbacks that can be used for select list options. This array
* should be keyed by the "name" of the pre-defined list. The values should
* be an array with the following additional keys:
* - title: The translated title for this list.
* - options callback: The name of a function implementing
* callback_webform_options() that will return the list.
* - options arguments: Any additional arguments to send to the callback.
* - file: Optional. The file containing the options callback, relative to
* the module root.
*/
function hook_webform_select_options_info() {
$items = array();
$items['days'] = array(
'title' => t('Days of the week'),
'options callback' => 'webform_options_days',
'file' => 'includes/webform.options.inc',
);
return $items;
}
/**
* Alter the list of select list options provided by Webform and other modules.
*
* @see hook_webform_select_options_info()
*/
function hook_webform_select_options_info_alter(&$items) {
// Remove the days of the week options.
unset($items['days']);
}
/**
* Define a list of options that Webform may use in a select component.
*
* Callback for hook_webform_select_options_info().
*
* @param $component
* The Webform component array for the select component being displayed.
* @param $flat
* Boolean value indicating whether the returned list needs to be a flat array
* of key => value pairs. Select components support up to one level of
* nesting, but when results are displayed, the list needs to be returned
* without the nesting.
* @param $arguments
* The "options arguments" specified in hook_webform_select_options_info().
*
* @return array
* An array of key => value pairs suitable for a select list's #options
* FormAPI property.
*/
function callback_webform_options($component, $flat, $arguments) {
$options = array(
'one' => t('Pre-built option one'),
'two' => t('Pre-built option two'),
'three' => t('Pre-built option three'),
);
return $options;
}
/**
* Respond to the loading of Webform submissions.
*
* @param $submissions
* An array of Webform submissions that are being loaded, keyed by the
* submission ID. Modifications to the submissions are done by reference.
*/
function hook_webform_submission_load(&$submissions) {
foreach ($submissions as $sid => $submission) {
$submissions[$sid]->new_property = 'foo';
}
}
/**
* Respond to the creation of a new submission from form values.
*
* This hook is called when a user has completed a submission to initialize the
* submission object. After this object has its values populated, it will be
* saved by webform_submission_insert(). Note that this hook is only called for
* new submissions, not for submissions being edited. If responding to the
* saving of all submissions, it's recommended to use
* hook_webform_submission_presave().
*
* @param $submission
* The submission object that has been created.
* @param $node
* The Webform node for which this submission is being saved.
* @param $account
* The user account that is creating the submission.
* @param $form_state
* The contents of form state that is the basis for this submission.
*
* @see webform_submission_create()
*/
function hook_webform_submission_create_alter(&$submission, &$node, &$account, &$form_state) {
$submission->new_property = TRUE;
}
/**
* Modify a Webform submission, prior to saving it in the database.
*
* @param $node
* The Webform node on which this submission was made.
* @param $submission
* The Webform submission that is about to be saved to the database.
*/
function hook_webform_submission_presave($node, &$submission) {
// Update some component's value before it is saved.
$component_id = 4;
$submission->data[$component_id][0] = 'foo';
}
/**
* Respond to a Webform submission being inserted.
*
* Note that this hook is called after a submission has already been saved to
* the database. If needing to modify the submission prior to insertion, use
* hook_webform_submission_presave().
*
* @param $node
* The Webform node on which this submission was made.
* @param $submission
* The Webform submission that was just inserted into the database.
*/
function hook_webform_submission_insert($node, $submission) {
// Insert a record into a 3rd-party module table when a submission is added.
db_insert('mymodule_table')
->fields(array(
'nid' => $node->nid,
'sid' => $submission->sid,
'foo' => 'foo_data',
))
->execute();
}
/**
* Respond to a Webform submission being updated.
*
* Note that this hook is called after a submission has already been saved to
* the database. If needing to modify the submission prior to updating, use
* hook_webform_submission_presave().
*
* @param $node
* The Webform node on which this submission was made.
* @param $submission
* The Webform submission that was just updated in the database.
*/
function hook_webform_submission_update($node, $submission) {
// Update a record in a 3rd-party module table when a submission is updated.
db_update('mymodule_table')
->fields(array(
'foo' => 'foo_data',
))
->condition('nid', $node->nid)
->condition('sid', $submission->sid)
->execute();
}
/**
* Respond to a Webform submission being deleted.
*
* @param $node
* The Webform node on which this submission was made.
* @param $submission
* The Webform submission that was just deleted from the database.
*/
function hook_webform_submission_delete($node, $submission) {
// Delete a record from a 3rd-party module table when a submission is deleted.
db_delete('mymodule_table')
->condition('nid', $node->nid)
->condition('sid', $submission->sid)
->execute();
}
/**
* Provide a list of actions that can be executed on a submission.
*
* Some actions are displayed in the list of submissions such as edit, view, and
* delete. All other actions are displayed only when viewing the submission.
* These additional actions may be specified in this hook. Examples included
* directly in the Webform module include PDF, print, and resend e-mails. Other
* modules may extend this list by using this hook.
*
* @param $node
* The Webform node on which this submission was made.
* @param $submission
* The Webform submission on which the actions may be performed.
*
* @return array
* List of action.
*/
function hook_webform_submission_actions($node, $submission) {
$actions = array();
if (webform_results_access($node)) {
$actions['myaction'] = array(
'title' => t('Do my action'),
'href' => 'node/' . $node->nid . '/submission/' . $submission->sid . '/myaction',
'query' => backdrop_get_destination(),
);
}
return $actions;
}
/**
* Modify the draft to be presented for editing.
*
* When drafts are enabled for the webform, by default, a pre-existing draft is
* presented when the webform is displayed to that user. To allow multiple
* drafts, implement this alter function to set the $sid to NULL, or use your
* application's business logic to determine whether a new draft or which of
* the pre-existing drafts should be presented.
*
* @param int $sid
* The id of the most recent submission to be presented for editing. Change
* to a different draft's sid or set to NULL for a new draft.
* @param array $context
* Array of context with indices 'nid' and 'uid'.
*/
function hook_webform_draft_alter(&$sid, array $context) {
if ($_GET['newdraft']) {
$sid = NULL;
}
}
/**
* Alter the display of a Webform submission.
*
* This function applies to both e-mails sent by Webform and normal display of
* submissions when viewing through the administrative interface.
*
* @param $renderable
* The Webform submission in a renderable array, similar to FormAPI's
* structure. This variable must be passed in by-reference. Important
* properties of this array include #node, #submission, #email, and #format,
* which can be used to find the context of the submission that is being
* rendered.
*/
function hook_webform_submission_render_alter(&$renderable) {
// Remove page breaks from sent e-mails.
if (isset($renderable['#email'])) {
foreach (element_children($renderable) as $key) {
if ($renderable[$key]['#component']['type'] == 'pagebreak') {
unset($renderable[$key]);
}
}
}
}
/**
* Modify a loaded Webform component.
*
* IMPORTANT: This hook does not actually exist because components are loaded
* in bulk as part of webform_node_load(). Use hook_node_load() to modify loaded
* components when the node is loaded. This example is provided merely to point
* to hook_node_load().
*
* @see hook_nodeapi()
* @see webform_node_load()
*/
function hook_webform_component_load() {
// This hook does not exist. Instead use hook_node_load().
}
/**
* Modify a Webform component before it is saved to the database.
*
* Note that most of the time this hook is not necessary, because Webform will
* automatically add data to the component based on the component form. Using
* hook_form_alter() will be sufficient in most cases.
*
* @param $component
* The Webform component being saved.
*
* @see hook_form_alter()
* @see webform_component_edit_form()
*/
function hook_webform_component_presave(&$component) {
$component['extra']['new_option'] = 'foo';
}
/**
* Respond to a Webform component being inserted into the database.
*/
function hook_webform_component_insert($component) {
// Insert a record into a 3rd-party module table when a component is inserted.
db_insert('mymodule_table')
->fields(array(
'nid' => $component['nid'],
'cid' => $component['cid'],
'foo' => 'foo_data',
))
->execute();
}
/**
* Respond to a Webform component being updated in the database.
*/
function hook_webform_component_update($component) {
// Update a record in a 3rd-party module table when a component is updated.
db_update('mymodule_table')
->fields(array(
'foo' => 'foo_data',
))
->condition('nid', $component['nid'])
->condition('cid', $component['cid'])
->execute();
}
/**
* Respond to a Webform component being deleted.
*/
function hook_webform_component_delete($component) {
// Delete a record in a 3rd-party module table when a component is deleted.
db_delete('mymodule_table')
->condition('nid', $component['nid'])
->condition('cid', $component['cid'])
->execute();
}
/**
* Alter the entire analysis before rendering to the page on the Analysis tab.
*
* This alter hook allows modification of the entire analysis of a node's
* Webform results. The resulting analysis is displayed on the Results ->
* Analysis tab on the Webform.
*
* @param array $analysis
* A Backdrop renderable array, passed by reference, containing the entire
* contents of the analysis page. This typically will contain the following
* two major keys:
* - form: The form for configuring the shown analysis.
* - components: The list of analyses for each analysis-enabled component
* for the node. Each keyed by its component ID.
*/
function hook_webform_analysis_alter(array &$analysis) {
$node = $analysis['#node'];
// Add an additional piece of information to every component's analysis:
foreach (element_children($analysis['components']) as $cid) {
$component = $node->components[$cid];
$analysis['components'][$cid]['chart'] = array(
'#markup' => t('Chart for the @name component', array('@name' => $component['name'])),
);
}
}
/**
* Alter data when displaying an analysis on that component.
*
* This hook modifies the data from an individual component's analysis results.
* It can be used to add additional analysis, or to modify the existing results.
* If needing to alter the entire set of analyses rather than an individual
* component, hook_webform_analysis_alter() may be used instead.
*
* @param array $data
* An array containing the result of a components analysis hook, passed by
* reference. This is passed directly from a component's
* _webform_analysis_component() function. See that hook for more information
* on this value.
* @param object $node
* The node object that contains the component being analyzed.
* @param array $component
* The Webform component array whose analysis results are being displayed.
*
* @see _webform_analysis_component()
* @see hook_webform_analysis_alter()
*/
function hook_webform_analysis_component_data_alter(array &$data, $node, array $component) {
if ($component['type'] === 'textfield') {
// Do not display rows that contain a zero value.
foreach ($data as $row_number => $row_data) {
if ($row_data[1] === 0) {
unset($data[$row_number]);
}
}
}
}
/**
* Alter a Webform submission's header when exported.
*/
function hook_webform_csv_header_alter(&$header, $component) {
// Use the machine name for component headers, but only for the webform
// with node 5 and components that are text fields.
if ($component['nid'] == 5 && $component['type'] == 'textfield') {
$header[2] = $component['form_key'];
}
}
/**
* Alter a Webform submission's data when exported.
*/
function hook_webform_csv_data_alter(&$data, $component, $submission) {
// If a value of a field was left blank, use the value from another
// field.
if ($component['cid'] == 1 && empty($data)) {
$data = $submission->data[2]['value'][0];
}
}
/**
* Define components to Webform.
*
* @return array
* An array of components, keyed by machine name. Required properties are
* "label" and "description". The "features" array defines which capabilities
* the component has, such as being displayed in e-mails or csv downloads.
* A component like "markup" for example would not show in these locations.
* The possible features of a component include:
*
* - csv
* - email
* - email_address
* - email_name
* - required
* - conditional
* - spam_analysis
* - group
* - private
*
* Note that most of these features do not indicate the default state, but
* determine if the component can have this property at all. Setting
* "required" to TRUE does not mean that a component's fields will always be
* required, but instead give the option to the administrator to choose the
* requiredness. See the example implementation for details on how these
* features may be set.
*
* An optional "file" may be specified to be loaded when the component is
* needed. A set of callbacks will be established based on the name of the
* component. All components follow the pattern:
*
* _webform_[callback]_[component]
*
* Where [component] is the name of the key of the component and [callback] is
* any of the following:
*
* - defaults
* - edit
* - render
* - display
* - submit
* - delete
* - help
* - theme
* - analysis
* - table
* - csv_headers
* - csv_data
*
* See the sample component implementation for details on each one of these
* callbacks.
*
* @see webform_components()
*/
function hook_webform_component_info() {
$components = array();
$components['textfield'] = array(
'label' => t('Textfield'),
'description' => t('Basic textfield type.'),
'features' => array(
// This component includes an analysis callback. Defaults to TRUE.
'analysis' => TRUE,
// Add content to CSV downloads. Defaults to TRUE.
'csv' => TRUE,
// This component supports default values. Defaults to TRUE.
'default_value' => FALSE,
// This component supports a description field. Defaults to TRUE.
'description' => FALSE,
// Show this component in e-mailed submissions. Defaults to TRUE.
'email' => TRUE,
// Allow this component to be used as an e-mail FROM or TO address.
// Defaults to FALSE.
'email_address' => FALSE,
// Allow this component to be used as an e-mail SUBJECT or FROM name.
// Defaults to FALSE.
'email_name' => TRUE,
// This component may be toggled as required or not. Defaults to TRUE.
'required' => TRUE,
// Store data in database. Defaults to TRUE. When FALSE, submission data
// will never be saved. This is for components like fieldset, markup, and
// pagebreak which do not collect data.
'stores_data' => TRUE,
// This component supports a title attribute. Defaults to TRUE.
'title' => FALSE,
// This component has a title that can be toggled as displayed or not.
'title_display' => TRUE,
// This component has a title that can be displayed inline.
'title_inline' => TRUE,
// If this component can be used as a conditional SOURCE. All components
// may always be displayed conditionally, regardless of this setting.
// Defaults to TRUE.
'conditional' => TRUE,
// If this component allows other components to be grouped within it
// (like a fieldset or tabs). Defaults to FALSE.
'group' => FALSE,
// If this component can be used for SPAM analysis.
'spam_analysis' => FALSE,
// If this component saves a file that can be used as an e-mail
// attachment. Defaults to FALSE.
'attachment' => FALSE,
// If this component reflects a time range and should use labels such as
// "Before" and "After" when exposed as filters in Views module.
'views_range' => FALSE,
// Set this to FALSE if this component cannot be used as a private
// component. If this is not FALSE, in your implementation of
// _webform_defaults_COMPONENT(), set ['extra']['private'] property to
// TRUE or FALSE.
'private' => FALSE,
),
// Specify the conditional behaviour of this component.
// Examples are 'string', 'date', 'time', 'numeric', 'select'.
// Defaults to 'string'.
'conditional_type' => 'string',
'file' => 'components/textfield.inc',
);
return $components;
}
/**
* Alter the list of available Webform components.
*
* @param $components
* A list of existing components as defined by hook_webform_component_info().
*
* @see hook_webform_component_info()
*/
function hook_webform_component_info_alter(&$components) {
// Completely remove a component.
unset($components['grid']);
// Change the name of a component.
$components['textarea']['label'] = t('Text box');
}
/**
* Alter the list of Webform component default values.
*
* @param $defaults
* A list of component defaults as defined by _webform_defaults_COMPONENT().
* @param $type
* The component type whose defaults are being provided.
*
* @see _webform_defaults_component()
*/
function hook_webform_component_defaults_alter(&$defaults, $type) {
// Alter a default for all component types.
$defaults['required'] = 1;
// Add a default for a new field added via hook_form_alter() or
// hook_form_FORM_ID_alter() for all component types.
$defaults['extra']['added_field'] = t('Added default value');
// Add or alter defaults for specific component types:
switch ($type) {
case 'select':
$defaults['extra']['optrand'] = 1;
break;
case 'textfield':
case 'textarea':
$defaults['extra']['another_added_field'] = t('Another added default value');
}
}
/**
* Alter access to a Webform submission.
*
* @param $node
* The Webform node on which this submission was made.
* @param $submission
* The Webform submission.
* @param $op
* The operation to be performed on the submission. Possible values are:
* - "view"
* - "edit"
* - "delete"
* - "list"
* @param $account
* A user account object.
*
* @return bool
* TRUE if the current user has access to submission,
* or FALSE otherwise.
*/
function hook_webform_submission_access($node, $submission, $op = 'view', $account = NULL) {
switch ($op) {
case 'view':
return TRUE;
case 'edit':
return FALSE;
case 'delete':
return TRUE;
case 'list':
return TRUE;
}
}
/**
* Determine if a user has access to see the results of a webform.
*
* Note in addition to the view access to the results granted here, the $account
* must also have view access to the Webform node in order to see results.
* Access via this hook is in addition (adds permission) to the standard
* webform access.
*
* @param $node
* The Webform node to check access on.
* @param $account
* The user account to check access on.
*
* @return bool
* TRUE or FALSE if the user can access the webform results.
*
* @see webform_results_access()
*/
function hook_webform_results_access($node, $account) {
// Let editors view results of unpublished webforms.
if ($node->status == 0 && in_array('editor', $account->roles)) {
return TRUE;
}
else {
return FALSE;
}
}
/**
* Determine if a user has access to clear the results of a webform.
*
* Access via this hook is in addition (adds permission) to the standard
* webform access (delete all webform submissions).
*
* @param object $node
* The Webform node to check access on.
* @param object $account
* The user account to check access on.
*
* @return bool
* TRUE or FALSE if the user can access the webform results.
*
* @see webform_results_clear_access()
*/
function hook_webform_results_clear_access($node, $account) {
return user_access('my additional access', $account);
}
/**
* Overrides the node_access and user_access permissions.
*
* Overrides the node_access and user_access permission to access and edit
* webform components, e-mails, conditions, and form settings.
*
* Return NULL to defer to other modules. If all implementations defer, then
* access to the node's EDIT tab plus 'edit webform components' permission
* determines access. To grant access, return TRUE; to deny access, return
* FALSE. If more than one implementation return TRUE/FALSE, all must be TRUE
* to grant access.
*
* In this way, access to the EDIT tab of the node may be decoupled from
* access to the WEBFORM tab. When returning TRUE, consider all aspects of
* access as this will be the only test. For example, 'return TRUE;' would grant
* annonymous access to creating webform components, which seldom be desired.
*
* @param object $node
* The Webform node to check access on.
* @param object $account
* The user account to check access on.
*
* @return bool|null
* TRUE or FALSE if the user can access the webform results, or NULL if
* access should be deferred to other implementations of this hook or
* node_access('update') plus user_access('edit webform components').
*
* @see webform_node_update_access()
*/
function hook_webform_update_access($node, $account) {
// Allow anyone who can see webform_editable_by_user nodes and who has
// 'my webform component edit access' permission to see, edit, and delete the
// webform components, e-mails, conditionals, and form settings.
if ($node->type == 'webform_editable_by_user') {
return node_access('view', $node, $account) && user_access('my webform component edit access', $account);
}
}
/**
* Return an array of files associated with the component.
*
* The output of this function will be used to attach files to e-mail messages.
*
* @param $component
* A Webform component array.
* @param $value
* An array of information containing the submission result, directly
* correlating to the webform_submitted_data database schema.
*
* @return array
* An array of files, each file is an array with following keys:
* - filepath: The relative path to the file.
* - filename: The name of the file including the extension.
* - filemime: The mimetype of the file.
* This will result in an array looking something like this:
*
* @code
* array[0] => array(
* 'filepath' => '/sites/default/files/attachment.txt',
* 'filename' => 'attachment.txt',
* 'filemime' => 'text/plain',
* );
* @endcode
*/
function _webform_attachments_component($component, $value) {
$files = array();
$files[] = (array) file_load($value[0]);
return $files;
}
/**
* Alter default settings for a newly created webform node.
*
* @param array $defaults
* Default settings for a newly created webform node as defined by
* webform_node_defaults().
*
* @see webform_node_defaults()
*/
function hook_webform_node_defaults_alter(array &$defaults) {
$defaults['allow_draft'] = '1';
}
/**
* Add additional fields to submission data downloads.
*
* @return array
* Keys and titles for default submission information.
*
* @see hook_webform_results_download_submission_information_data()
* @see hook_webform_results_download_submission_information_info_alter()
*/
function hook_webform_results_download_submission_information_info() {
return array(
'field_key_1' => t('Field Title 1'),
'field_key_2' => t('Field Title 2'),
);
}
/**
* Alter fields in submission data downloads.
*
* @param array $submission_information
* Keys and titles for default submission information.
*
* @see hook_webform_results_download_submission_information_info()
*/
function hook_webform_results_download_submission_information_info_alter(array &$submission_information) {
// Unset a property to remove it from submission data downloads.
if (isset($submission_information['webform_ip_address'])) {
unset($submission_information['webform_ip_address']);
}
}
/**
* Return values for submission data download fields.
*
* @param $token
* The name of the token being replaced.
* @param $submission
* The data for an individual submission from webform_get_submissions().
* @param array $options
* A list of options that define the output format. These are generally passed
* through from the GUI interface.
* @param $serial_start
* The starting position for the Serial column in the output.
* @param $row_count
* The number of the row being generated.
*
* @return string
* Value for requested submission information field.
*
* @see hook_webform_results_download_submission_information_info()
*/
function hook_webform_results_download_submission_information_data($token, $submission, array $options, $serial_start, $row_count) {
switch ($token) {
case 'field_key_1':
return 'Field Value 1';
case 'field_key_2':
return 'Field Value 2';
}
}
/**
* Alter the query that will produce the list of submission IDs to be
* downloaded.
*
* @param object $query
* The query object that is being built up to provide the list of submission
* IDs.
*
* @see webform_download_sids_query()
*/
function hook_webform_download_sids_query_alter(&$query) {
global $user;
// Check if component value matches a node ID and author of that node.
$query->join('webform_submitted_data', 'wsd', 'ws.sid = wsd.sid');
$query->condition('wsd.cid', 2);
$query->join('node', 'n', 'wsd.data = n.nid');
$query->condition('n.uid', $user->uid);
}
/**
* @}
*/
/**
* @defgroup webform_component Sample Webform Component
* @{
* In each of these examples, the word "component" should be replaced with the,
* name of the component type (such as textfield or select). These are not
* actual hooks, but instead samples of how Webform integrates with its own
* built-in components.
*/
/**
* Specify the default properties of a component.
*
* @return array
* An array defining the default structure of a component.
*/
function _webform_defaults_component() {
return array(
'name' => '',
'form_key' => NULL,
'required' => 0,
'pid' => 0,
'weight' => 0,
'extra' => array(
'options' => '',
'questions' => '',
'optrand' => 0,
'qrand' => 0,
'description' => '',
'description_above' => FALSE,
'private' => FALSE,
'analysis' => TRUE,
),
);
}
/**
* Generate the form for editing a component.
*
* Create a set of form elements to be displayed on the form for editing this
* component. Use care naming the form items, as this correlates directly to the
* database schema. The component "Name" and "Description" fields are added to
* every component type and are not necessary to specify here (although they
* may be overridden if desired).
*
* @param array $component
* A Webform component array.
* @param array $form
* The form array.
* @param array $form_state
* The form state array.
*
* @return array
* Return $form with whatever changes are desired.
*/
function _webform_edit_component(array $component, array $form, array $form_state) {
// Disabling the description if not wanted.
$form['description']['#access'] = FALSE;
// Most options are stored in the "extra" array, which stores any settings
// unique to a particular component type.
$form['extra']['options'] = array(
'#type' => 'textarea',
'#title' => t('Options'),
'#default_value' => $component['extra']['options'],
'#description' => t('Key-value pairs may be entered separated by pipes. i.e. safe_key|Some readable option') . ' ' . theme('webform_token_help'),
'#cols' => 60,
'#rows' => 5,
'#weight' => -3,
'#required' => TRUE,
);
return $form;
}
/**
* Render a Webform component to be part of a form.
*
* @param $component
* A Webform component array.
* @param $value
* If editing an existing submission or resuming a draft, this will contain
* an array of values to be shown instead of the default in the component
* configuration. This value will always be an array, keyed numerically for
* each value saved in this field.
* @param $filter
* Whether or not to filter the contents of descriptions and values when
* rendering the component. Values need to be unfiltered to be editable by
* Form Builder.
* @param $submission
* The submission from which this component is being rendered. Usually not
* needed. Used by _webform_render_date() to validate using the submission's
* completion date.
*
* @return array
* $form_item
*
* @see _webform_client_form_add_component()
*/
function _webform_render_component($component, $value = NULL, $filter = TRUE, $submission = NULL) {
$form_item = array(
'#type' => 'textfield',
'#title' => $filter ? webform_filter_xss($component['name']) : $component['name'],
'#required' => $component['required'],
'#weight' => $component['weight'],
'#description' => $filter ? webform_filter_descriptions($component['extra']['description']) : $component['extra']['description'],
'#default_value' => $filter ? webform_replace_tokens($component['value']) : $component['value'],
'#theme_wrappers' => array('webform_element'),
);
if (isset($value)) {
$form_item['#default_value'] = $value[0];
}
return $form_item;
}
/**
* Allow modules to modify a webform component that will be rendered in a form.
*
* @param array $element
* The display element as returned by _webform_render_component().
* @param array $component
* A Webform component array.
*
* @see _webform_render_component()
*/
function hook_webform_component_render_alter(array &$element, array &$component) {
if ($component['cid'] == 10) {
$element['#title'] = 'My custom title';
$element['#default_value'] = 42;
}
}
/**