-
Notifications
You must be signed in to change notification settings - Fork 4
/
boilerplate.forms.inc
77 lines (69 loc) · 2.01 KB
/
boilerplate.forms.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
<?php
/**
* @file
* Custom forms used in the module.
*
* Created by: Topsitemakers
* http://www.topsitemakers.com/
*/
/**
* Sample form.
*/
function boilerplate_sample_form($form, &$form_state, $arg1, $arg2) {
$form['sample'] = array(
'#type' => 'fieldset',
'#title' => t('Personal information'),
'#description' => t('Passed arguments: %arg1 and %arg2.', array(
'%arg1' => $arg1,
'%arg2' => $arg2,
)),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['sample']['sample_textfield'] = array(
'#title' => t('Text field'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['sample']['sample_radios'] = array(
'#title' => t('Radios'),
'#type' => 'radios',
'#options' => array(
1 => t('Option 1'),
2 => t('Option 2'),
3 => t('Option 3'),
4 => t('Option 4'),
),
// Demonstration of our custom AJAX commands.
'#ajax' => array(
'callback' => 'boilerplate_sample_form_radios_ajax',
),
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Submit the form'));
return $form;
}
/**
* Sample form AJAX callback.
*/
function boilerplate_sample_form_radios_ajax($form, $form_state) {
$commands = array();
$commands[] = boilerplate_ajax_command_add_value_to_window('sample_radios', $form_state['values']['sample_radios']);
$commands[] = boilerplate_ajax_command_log_to_console(t('Radio buttons changed. Timestamp: @time', array('@time' => REQUEST_TIME)));
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Sample form validation.
*/
function boilerplate_sample_form_validate($form, &$form_state) {
if ($form_state['values']['sample_textfield'] == 'error') {
form_set_error('sample_textfield', t('Custom form validation.'));
}
}
/**
* Sample form submit processing.
*/
function boilerplate_sample_form_submit($form, &$form_state) {
drupal_set_message(t('Form submitted successfully.'));
drupal_goto('<front>');
}