Skip to content

Commit 000c39f

Browse files
Mile23paul-m
authored andcommitted
Issue #2975897 by Mile23: Remove usages of @deprecated drupal_set_message()
1 parent 3c0a51d commit 000c39f

File tree

35 files changed

+153
-121
lines changed

35 files changed

+153
-121
lines changed

ajax_example/src/Form/Autotextfields.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
101101
* {@inheritdoc}
102102
*/
103103
public function submitForm(array &$form, FormStateInterface $form_state) {
104-
drupal_set_message(
104+
$this->messenger()->addMessage(
105105
$this->t('Submit handler: First name: @first_name Last name: @last_name',
106106
[
107107
'@first_name' => $form_state->getValue('first_name', 'n/a'),

ajax_example/src/Form/DependentDropdown.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
161161
switch ($trigger) {
162162
case 'Submit':
163163
// Submit: We're done.
164-
drupal_set_message($this->t('Your values have been submitted. Instrument family: @family, Instrument: @instrument', [
164+
$this->messenger()->addMessage($this->t('Your values have been submitted. Instrument family: @family, Instrument: @instrument', [
165165
'@family' => $form_state->getValue('instrument_family_dropdown'),
166166
'@instrument' => $form_state->getValue('instrument_dropdown'),
167167
]));

ajax_example/src/Form/DynamicFormSections.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ public function buildForm(array $form, FormStateInterface $form_state, $nojs = N
172172
* Reports what values were finally set.
173173
*/
174174
public function submitForm(array &$form, FormStateInterface $form_state) {
175+
$messenger = $this->messenger();
175176
// This is only executed when a button is pressed, not when the AJAXfield
176177
// select is changed.
177178
// Now handle the case of the next, previous, and submit buttons.
@@ -189,10 +190,10 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
189190
$answer = $form['questions_fieldset']['question']['#title'];
190191
}
191192
if ($answer == $this->t('George Washington')) {
192-
drupal_set_message($this->t('You got the right answer: @answer', ['@answer' => $answer]));
193+
$messenger->addMessage($this->t('You got the right answer: @answer', ['@answer' => $answer]));
193194
}
194195
else {
195-
drupal_set_message($this->t('Sorry, your answer (@answer) is wrong', ['@answer' => $answer]));
196+
$messenger->addMessage($this->t('Sorry, your answer (@answer) is wrong', ['@answer' => $answer]));
196197
}
197198
return;
198199
}

ajax_example/src/Form/EntityAutocomplete.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Drupal\Core\Entity\EntityTypeManagerInterface;
77
use Drupal\Core\Form\FormInterface;
88
use Drupal\Core\Form\FormStateInterface;
9+
use Drupal\Core\Messenger\MessengerTrait;
910
use Drupal\Core\StringTranslation\StringTranslationTrait;
1011
use Symfony\Component\DependencyInjection\ContainerInterface;
1112

@@ -17,6 +18,7 @@
1718
class EntityAutocomplete implements FormInterface, ContainerInjectionInterface {
1819

1920
use StringTranslationTrait;
21+
use MessengerTrait;
2022

2123
/**
2224
* The entity type manager service.
@@ -41,6 +43,7 @@ public static function create(ContainerInterface $container) {
4143
$container->get('entity_type.manager')
4244
);
4345
$form->setStringTranslation($container->get('string_translation'));
46+
$form->setMessenger($container->get('messenger'));
4447
return $form;
4548
}
4649

@@ -120,7 +123,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
120123
$uid = $state_user['target_id'];
121124
$users[] = $this->entityTypeManager->getStorage('user')->load($uid)->getUsername();
122125
}
123-
drupal_set_message('These are your users: ' . implode(' ', $users));
126+
$this->messenger()->addMessage('These are your users: ' . implode(' ', $users));
124127
}
125128

126129
}

ajax_example/src/Form/Wizard.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
116116
$value_message .= "$key=$value, ";
117117
}
118118
}
119-
drupal_set_message($value_message);
119+
$this->messenger()->addMessage($value_message);
120120
$form_state->setRebuild(FALSE);
121121
// Redirect to #action, else return.
122122
return;

batch_example/batch_example.module

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,18 @@ function batch_example_op_2($operation_details, &$context) {
107107
* Batch 'finished' callback used by both batch 1 and batch 2.
108108
*/
109109
function batch_example_finished($success, $results, $operations) {
110+
$messenger = \Drupal::messenger();
110111
if ($success) {
111112
// Here we could do something meaningful with the results.
112113
// We just display the number of nodes we processed...
113-
drupal_set_message(t('@count results processed.', ['@count' => count($results)]));
114-
drupal_set_message(t('The final result was "%final"', ['%final' => end($results)]));
114+
$messenger->addMessage(t('@count results processed.', ['@count' => count($results)]));
115+
$messenger->addMessage(t('The final result was "%final"', ['%final' => end($results)]));
115116
}
116117
else {
117118
// An error occurred.
118119
// $operations contains the operations that remained unprocessed.
119120
$error_operation = reset($operations);
120-
drupal_set_message(
121+
$messenger->addMessage(
121122
t('An error occurred while processing @operation with arguments : @args',
122123
[
123124
'@operation' => $error_operation[0],

batch_example/src/Form/BatchExampleForm.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
7575
*/
7676
public function generateBatch1() {
7777
$num_operations = 1000;
78-
drupal_set_message(t('Creating an array of @num operations', ['@num' => $num_operations]));
78+
$this->messenger()->addMessage($this->t('Creating an array of @num operations', ['@num' => $num_operations]));
7979

8080
$operations = [];
8181
// Set up an operations array with 1000 elements, each doing function

batch_example/tests/src/functional/BatchExampleWebTest.php renamed to batch_example/tests/src/Functional/BatchExampleWebTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* Functional tests for the Batch Example module.
99
*
10-
* @group fapi_example
10+
* @group batch_example
1111
*
1212
* @ingroup batch_example
1313
*/

cache_example/src/Form/CacheExampleForm.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,14 @@ public static function create(ContainerInterface $container) {
6464
// @link https://www.drupal.org/node/2203931.
6565
// Those services are passed in the $container through the static create
6666
// method.
67-
return new static(
67+
$form = new static(
6868
$container->get('request_stack'),
6969
$container->get('string_translation'),
7070
$container->get('current_user'),
7171
$container->get('cache.default')
7272
);
73+
$form->setMessenger($container->get('messenger'));
74+
return $form;
7375
}
7476

7577
/**
@@ -237,7 +239,7 @@ public function expireFiles($form, &$form_state) {
237239
$this->cacheBackend->delete('cache_example_files_count');
238240

239241
// Display message to the user.
240-
drupal_set_message($this->t('Cached data key "cache_example_files_count" was cleared.'), 'status');
242+
$this->messenger()->addMessage($this->t('Cached data key "cache_example_files_count" was cleared.'), 'status');
241243
}
242244

243245
/**
@@ -262,7 +264,7 @@ public function createExpiringItem($form, &$form_state) {
262264
// required interval. Also add a tag to it to be able to clear caches more
263265
// precise.
264266
$this->cacheBackend->set('cache_example_expiring_item', $expiration_friendly, $expiration, $tags);
265-
drupal_set_message($this->t('cache_example_expiring_item was set to expire at %time', ['%time' => $expiration_friendly]));
267+
$this->messenger()->addMessage($this->t('cache_example_expiring_item was set to expire at %time', ['%time' => $expiration_friendly]));
266268
}
267269

268270
/**
@@ -274,14 +276,14 @@ public function cacheClearing($form, &$form_state) {
274276
// Here we'll remove all cache keys in the 'cache' bin that have
275277
// expired.
276278
$this->cacheBackend->garbageCollection();
277-
drupal_set_message($this->t('\Drupal::cache()->garbageCollection() was called, removing any expired cache items.'));
279+
$this->messenger()->addMessage($this->t('\Drupal::cache()->garbageCollection() was called, removing any expired cache items.'));
278280
break;
279281

280282
case 'remove_all':
281283
// This removes all keys in a bin using a super-wildcard. This
282284
// has nothing to do with expiration. It's just brute-force removal.
283285
$this->cacheBackend->deleteAll();
284-
drupal_set_message($this->t('ALL entries in the "cache" bin were removed with \Drupal::cache()->deleteAll().'));
286+
$this->messenger()->addMessage($this->t('ALL entries in the "cache" bin were removed with \Drupal::cache()->deleteAll().'));
285287
break;
286288

287289
case 'remove_tag':
@@ -291,7 +293,7 @@ public function cacheClearing($form, &$form_state) {
291293
'cache_example:1',
292294
];
293295
Cache::invalidateTags($tags);
294-
drupal_set_message($this->t('Cache entries with the tag "cache_example" set to 1 in the "cache" bin were invalidated with \Drupal\Core\Cache\Cache::invalidateTags($tags).'));
296+
$this->messenger()->addMessage($this->t('Cache entries with the tag "cache_example" set to 1 in the "cache" bin were invalidated with \Drupal\Core\Cache\Cache::invalidateTags($tags).'));
295297
break;
296298
}
297299
}

config_entity_example/src/Form/RobotDeleteForm.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
7878
$this->entity->delete();
7979

8080
// Set a message that the entity was deleted.
81-
drupal_set_message($this->t('Robot %label was deleted.', [
81+
$this->messenger()->addMessage($this->t('Robot %label was deleted.', [
8282
'%label' => $this->entity->label(),
8383
]));
8484

0 commit comments

Comments
 (0)