DBTNGExampleUpdateForm.php
4.44 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
<?php
/**
* @file
* Contains \Drupal\dbtng_example\DBTNGExampleUpdateForm
*/
namespace Drupal\dbtng_example;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Sample UI to update a record.
*/
class DBTNGExampleUpdateForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormID() {
return 'dbtng_update_form';
}
/**
* Sample UI to update a record.
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// Wrap the form in a div.
$form = array(
'#prefix' => '<div id="updateform">',
'#suffix' => '</div>',
);
// Add some explanatory text to the form.
$form['message'] = array(
'#markup' => $this->t('Demonstrates a database update operation.'),
);
// Query for items to display.
$entries = DBTNGExampleStorage::load();
// Tell the user if there is nothing to display.
if (empty($entries)) {
$form['no_values'] = array(
'#value' => t('No entries exist in the table dbtng_example table.'),
);
return $form;
}
$keyed_entries = array();
foreach ($entries as $entry) {
$options[$entry->pid] = t('@pid: @name @surname (@age)', array(
'@pid' => $entry->pid,
'@name' => $entry->name,
'@surname' => $entry->surname,
'@age' => $entry->age,
)
);
$keyed_entries[$entry->pid] = $entry;
}
// Grab the pid.
$pid = $form_state->getValue('pid');
// Use the pid to set the default entry for updating.
$default_entry = !empty($pid) ? $keyed_entries[$pid] : $entries[0];
// Save the entries into the $form_state. We do this so the AJAX callback
// doesn't need to repeat the query.
$form_state->setValue('entries', $keyed_entries);
$form['pid'] = array(
'#type' => 'select',
'#options' => $options,
'#title' => t('Choose entry to update'),
'#default_value' => $default_entry->pid,
'#ajax' => array(
'wrapper' => 'updateform',
'callback' => array($this, 'updateCallback'),
),
);
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Updated first name'),
'#size' => 15,
'#default_value' => $default_entry->name,
);
$form['surname'] = array(
'#type' => 'textfield',
'#title' => t('Updated last name'),
'#size' => 15,
'#default_value' => $default_entry->surname,
);
$form['age'] = array(
'#type' => 'textfield',
'#title' => t('Updated age'),
'#size' => 4,
'#default_value' => $default_entry->age,
'#description' => t('Values greater than 127 will cause an exception'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Update'),
);
return $form;
}
/**
* AJAX callback handler for the pid select.
*
* When the pid changes, populates the defaults from the database in the form.
*/
public function updateCallback(array $form, FormStateInterface $form_state) {
// Gather the DB results from $form_state.
$entries = $form_state->getValue('entries');
// Use the specific entry for this $form_state.
$entry = $entries[$form_state->getValue('pid')];
// Setting the #value of items is the only way I was able to figure out
// to get replaced defaults on these items. #default_value will not do it
// and shouldn't.
foreach (array('name', 'surname', 'age') as $item) {
$form[$item]['#value'] = $entry->$item;
}
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
// Confirm that age is numeric.
if (!intval($form_state->getValue('age'))) {
$form_state->setErrorByName('age', t('Age needs to be a number'));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Gather the current user so the new record has ownership.
$account = \Drupal::currentUser();
// Save the submitted entry.
$entry = array(
'pid' => $form_state->getValue('pid'),
'name' => $form_state->getValue('name'),
'surname' => $form_state->getValue('surname'),
'age' => $form_state->getValue('age'),
'uid' => $account->id(),
);
$count = DBTNGExampleStorage::update($entry);
drupal_set_message(t('Updated entry @entry (@count row updated)', array(
'@count' => $count,
'@entry' => print_r($entry, TRUE),
)
));
}
}