-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathajf.form.dynamic.fields.inc
More file actions
162 lines (142 loc) · 5.57 KB
/
ajf.form.dynamic.fields.inc
File metadata and controls
162 lines (142 loc) · 5.57 KB
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
<?php
/**
* Code to add additional fields to a form dynamically as needed.
*
* The user clicks a button and another instance of the field(s) is added.
*
* This is based on code shared by Sam in his blog article:
* http://rapiddg.com/blog/use-ajax-support-multiple-item-values-custom-drupal-7-forms
*
* I have adapted his example to my own needs.
*/
function ajf_customer_create_form($form, &$form_state) {
// Set the page breadcrumb.
$crumbs = array(
l(t('Home'), '<front>'),
l(t('Administration'), 'admin'),
l(t('ajf'), 'admin/ajf'),
l(t('Customers'), 'admin/ajf/customers'),
);
drupal_set_breadcrumb($crumbs);
return ajf_build_form($form, $form_state, "create");
}
function ajf_customer_create_form_submit($form, &$form_state) {
$custRow = array(
'cust_name' => $form_state['values']['customer-name'],
'cust_type' => $form_state['values']['customer-type'],
'addr_line1' => $form_state['values']['addr-line1'],
'addr_line2' => $form_state['values']['addr-line2'],
'addr_line3' => $form_state['values']['addr-line3'],
'addr_line4' => $form_state['values']['addr-line4'],
'zipcode' => $form_state['values']['zipcode'],
'city' => $form_state['values']['city'],
'state' => $form_state['values']['state'],
'country' => $form_state['values']['country'],
'phone1' => $form_state['values']['phone1'],
'phone1_type' => $form_state['values']['phone1-type'],
'phone2' => $form_state['values']['phone2'],
'phone2_type' => $form_state['values']['phone2-type'],
'phone3' => $form_state['values']['phone3'],
'phone3_type' => $form_state['values']['phone3-type'],
'description' => $form_state['values']['description'],
);
//Function definition omitted in this snippet.
$custId = ajf_customer_insert($custRow);
for ($i = 1; $i <= $form_state['storage']['email_count']; $i++) {
if (isset($form_state['values']['email'][$i]) && (!empty($form_state['values']['email'][$i]['email_addr']))) {
$emailRow = array(
'cust_id' => $custId,
'email_addr' => $form_state['values']['email'][$i]['email_addr'],
'email_type' => $form_state['values']['email'][$i]['email_type'],
'default_email' => $form_state['values']['email'][$i]['default_email'],
'description' => $form_state['values']['email'][$i]['email_desc'],
);
// Function definition omitted in this snippet.
$emailId = ajf_email_insert($emailRow);
}
}
$form_state['redirect'] = 'admin/ajf/customers';
drupal_set_message("Successfully saved Customer.");
}
function ajf_build_form($form, &$form_state, $type, $custData = array()) {
// If the form state storage field for the email address count is not set, then set it to zero.
$form_state['storage']['email_count'] =
isset($form_state['storage']['email_count']) ? $form_state['storage']['email_count'] : 0;
//dpm($form_state);
// *ajf - many form fields for creating a customer are omitted here, as this is only to highlight the dynamic email fields.
//Additional instances of the email form fields can be added by the user - as many as needed.
$form['email'] = array(
'#type' => 'container',
'#tree' => TRUE,
'#prefix' => '<div id="email">',
'#suffix' => '</div>',
);
if ($form_state['storage']['email_count']) {
for ($i = 1; $i <= $form_state['storage']['email_count']; $i++) {
$form['email'][$i] = array(
'#type' => 'fieldset',
'#tree' => TRUE,
);
$form['email'][$i]['email_addr'] = array(
'#title' => t('Email Address'),
'#type' => 'textfield',
'#description' => 'Enter an email address for this customer.',
);
$form['email'][$i]['email_type'] = array(
'#type' => 'select',
'#title' => t('Email Type'),
'#options' => array('Work' => 'Work', 'Personal' => 'Personal'),
'#default_value' => 'Work',
);
$form['email'][$i]['default_email'] = array(
'#title' => t('Make Default'),
'#type' => 'radios',
'#default_value' => 'N',
'#options' => array('Y' => 'Yes', 'N' => 'No'),
'#description' => t('Choose Yes if you want this email to be chosen by default when sending invoices.'),
);
$form['email'][$i]['email_desc'] = array(
'#type' => 'textarea',
'#title' => t('Email Description'),
'#maxlength' => 255,
'#wysiwyg' => false,
);
}
}
$form['add_email'] = array(
'#type' => 'button',
'#name' => 'add_email',
'#value' => t('Add an Email Address'),
'#href' => '',
'#ajax' => array(
'callback' => 'ajf_ajax_add_email',
'wrapper' => 'email',
),
);
$form['actions'] = array('#type' => 'actions'); // wrapper for a group of buttons
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
if ($type === "edit") {
$form['actions']['cancel'] = array(
'#markup' => l(t('Cancel'), 'admin/ajf/customers' . $custData['cust_id'] . '/view'),
);
} else {
$form['actions']['cancel'] = array(
'#markup' => l(t('Cancel'), 'admin/ajf/customers'),
);
}
// when the form is rendered, check if we added an email. If so, increment the counter.
if (isset($form_state['triggering_element'])) {
if ($form_state['triggering_element']['#name'] == 'add_email') {
$form_state['storage']['email_count']++;
}
}else{
$form_state['storage']['email_count']++; // the first time the user clicks the email button, 'triggering_element' isn't set, so we need this.
}
return $form;
}
function ajf_ajax_add_email($form, $form_state) {
return $form['email'];
}