Skip to content

Commit b5d3b31

Browse files
Mile23paul-m
Mile23
authored andcommitted
Issue #2987316 by Mile23: Always inject services in controllers and forms
1 parent 4f36484 commit b5d3b31

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

ajax_example/src/Form/Wizard.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Drupal\Core\Form\FormStateInterface;
77
use Drupal\Core\Url;
88
use Drupal\Core\Link;
9+
use Symfony\Component\DependencyInjection\ContainerInterface;
910

1011
/**
1112
* AJAX example wizard.
@@ -19,6 +20,18 @@ public function getFormId() {
1920
return 'ajax_example_wizard';
2021
}
2122

23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public static function create(ContainerInterface $container) {
27+
// Since FormBase uses service traits, we can inject these services without
28+
// adding our own __construct() method.
29+
$form = new static($container);
30+
$form->setStringTranslation($container->get('string_translation'));
31+
$form->setMessenger($container->get('messenger'));
32+
return $form;
33+
}
34+
2235
/**
2336
* {@inheritdoc}
2437
*/
@@ -204,8 +217,7 @@ public function prevSubmit(array $form, FormStateInterface $form_state) {
204217
* Save away the current information.
205218
*/
206219
public function submitForm(array &$form, FormStateInterface $form_state) {
207-
208-
$messenger = \Drupal::messenger();
220+
$messenger = $this->messenger();
209221
$messenger->addMessage($this->t('Your information has been submitted:'));
210222
$messenger->addMessage($this->t('Name: @name', ['@name' => $form_state->getValue(['step1', 'name'])]));
211223
$messenger->addMessage($this->t('Address: @address', ['@address' => $form_state->getValue(['step2', 'address'])]));

cron_example/src/Form/CronExampleForm.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ public function buildForm(array $form, FormStateInterface $form_state) {
9494
'#markup' => $this->t('The cron example demonstrates hook_cron() and hook_queue_info() processing. If you have administrative privileges you can run cron from this page and see the results.'),
9595
];
9696

97-
$next_execution = \Drupal::state()->get('cron_example.next_execution');
97+
$next_execution = $this->state->get('cron_example.next_execution');
9898
$next_execution = !empty($next_execution) ? $next_execution : REQUEST_TIME;
9999

100100
$args = [
101-
'%time' => date_iso8601(\Drupal::state()->get('cron_example.next_execution')),
101+
'%time' => date_iso8601($this->state->get('cron_example.next_execution')),
102102
'%seconds' => $next_execution - REQUEST_TIME,
103103
];
104104
$form['status']['last'] = [
@@ -193,7 +193,7 @@ public function cronRun(array &$form, FormStateInterface &$form_state) {
193193

194194
$cron_reset = $form_state->getValue('cron_reset');
195195
if (!empty($cron_reset)) {
196-
\Drupal::state()->set('cron_example.next_execution', 0);
196+
$this->state->set('cron_example.next_execution', 0);
197197
}
198198

199199
// Use a state variable to signal that cron was run manually from this form.

0 commit comments

Comments
 (0)