-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbtngExampleAddForm.php
More file actions
144 lines (128 loc) · 4 KB
/
DbtngExampleAddForm.php
File metadata and controls
144 lines (128 loc) · 4 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
<?php
namespace Drupal\dbtng_example\Form;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\dbtng_example\DbtngExampleRepository;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Form to add a database entry, with all the interesting fields.
*
* @ingroup dbtng_example
*/
class DbtngExampleAddForm implements FormInterface, ContainerInjectionInterface {
use StringTranslationTrait;
use MessengerTrait;
/**
* Our database repository service.
*
* @var \Drupal\dbtng_example\DbtngExampleRepository
*/
protected $repository;
/**
* The current user.
*
* We'll need this service in order to check if the user is logged in.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
/**
* {@inheritdoc}
*
* We'll use the ContainerInjectionInterface pattern here to inject the
* current user and also get the string_translation service.
*/
public static function create(ContainerInterface $container) {
$form = new static(
$container->get('dbtng_example.repository'),
$container->get('current_user')
);
// The StringTranslationTrait trait manages the string translation service
// for us. We can inject the service here.
$form->setStringTranslation($container->get('string_translation'));
$form->setMessenger($container->get('messenger'));
return $form;
}
/**
* Construct the new form object.
*/
public function __construct(DbtngExampleRepository $repository, AccountProxyInterface $current_user) {
$this->repository = $repository;
$this->currentUser = $current_user;
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'dbtng_add_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = [];
$form['message'] = [
'#markup' => $this->t('Add an entry to the dbtng_example table.'),
];
$form['add'] = [
'#type' => 'fieldset',
'#title' => $this->t('Add a person entry'),
];
$form['add']['name'] = [
'#type' => 'textfield',
'#title' => $this->t('Name'),
'#size' => 15,
];
$form['add']['surname'] = [
'#type' => 'textfield',
'#title' => $this->t('Surname'),
'#size' => 15,
];
$form['add']['age'] = [
'#type' => 'textfield',
'#title' => $this->t('Age'),
'#size' => 5,
'#description' => $this->t("Values greater than 127 will cause an exception. Try it - it's a great example why exception handling is needed with DTBNG."),
];
$form['add']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Add'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
// Verify that the user is logged-in.
if ($this->currentUser->isAnonymous()) {
$form_state->setError($form['add'], $this->t('You must be logged in to add values to the database.'));
}
// Confirm that age is numeric.
if (!intval($form_state->getValue('age'))) {
$form_state->setErrorByName('age', $this->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 = $this->currentUser;
// Save the submitted entry.
$entry = [
'name' => $form_state->getValue('name'),
'surname' => $form_state->getValue('surname'),
'age' => $form_state->getValue('age'),
'uid' => $account->id(),
];
$return = $this->repository->insert($entry);
if ($return) {
$this->messenger()->addMessage($this->t('Created entry @entry', ['@entry' => print_r($entry, TRUE)]));
}
}
}