Skip to content

Commit 1c48ee2

Browse files
Issue #2931730 by drunken monkey, borisson_, chanderbhushan: Adapted to changes in drupal_set_message().
1 parent 86ce327 commit 1c48ee2

32 files changed

+601
-113
lines changed

Diff for: CHANGELOG.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
Search API 1.x, dev (xxxx-xx-xx):
22
---------------------------------
3+
- #2931730 by drunken monkey, borisson_, chanderbhushan: Adapted to changes in
4+
drupal_set_message().
35
- #2955607 by drunken monkey, borisson_: Removed our custom Query::__wakeup()
46
work-around.
57
- #2940138 by drunken monkey, borisson_: Switched to new Core tempstore service.

Diff for: search_api.api.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ function hook_search_api_index_items_alter(\Drupal\search_api\IndexInterface $in
241241
'%index' => $index->label(),
242242
'@ids' => implode(', ', array_keys($items)),
243243
];
244-
drupal_set_message(t('Indexing items on index %index with the following IDs: @ids', $arguments));
244+
$message = t('Indexing items on index %index with the following IDs: @ids', $arguments);
245+
\Drupal::messenger()->addStatus($message);
245246
}
246247

247248
/**
@@ -257,7 +258,8 @@ function hook_search_api_items_indexed(\Drupal\search_api\IndexInterface $index,
257258
// Note that this is just an example, and would only work if there are only
258259
// nodes indexed in that index (and even then the printed IDs would probably
259260
// not be as expected).
260-
drupal_set_message(t('Nodes indexed: @ids.', implode(', ', $item_ids)));
261+
$message = t('Nodes indexed: @ids.', implode(', ', $item_ids));
262+
\Drupal::messenger()->addStatus($message);
261263
}
262264
}
263265

Diff for: search_api.module

+2-1
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,8 @@ function search_api_view_insert(ViewEntityInterface $view) {
572572
}
573573

574574
if ($change) {
575-
drupal_set_message(\Drupal::translation()->translate('The selected caching mechanism does not work with views on Search API indexes. Please either use one of the Search API-specific caching options or "None". Caching was turned off for this view.'), 'warning');
575+
$warning = t('The selected caching mechanism does not work with views on Search API indexes. Please either use one of the Search API-specific caching options or "None". Caching was turned off for this view.');
576+
\Drupal::messenger()->addWarning($warning);
576577
$view->set('display', $displays);
577578
$view->save();
578579
}

Diff for: search_api.services.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ services:
8686

8787
search_api.task_manager:
8888
class: Drupal\search_api\Task\TaskManager
89-
arguments: ['@entity_type.manager', '@event_dispatcher', '@string_translation']
89+
arguments: ['@entity_type.manager', '@event_dispatcher', '@string_translation', '@messenger']
9090

9191
search_api.vbo_view_data_provider:
9292
class: Drupal\search_api\Contrib\ViewsBulkOperationsEventSubscriber

Diff for: src/Backend/BackendPluginBase.php

+46-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Drupal\search_api\Backend;
44

5+
use Drupal\Core\Messenger\MessengerInterface;
56
use Drupal\search_api\Entity\Server;
67
use Drupal\search_api\Item\ItemInterface;
78
use Drupal\search_api\LoggerTrait;
@@ -11,6 +12,7 @@
1112
use Drupal\search_api\Plugin\ConfigurablePluginBase;
1213
use Drupal\search_api\ServerInterface;
1314
use Drupal\search_api\Utility\FieldsHelper;
15+
use Symfony\Component\DependencyInjection\ContainerInterface;
1416

1517
/**
1618
* Defines a base class for backend plugins.
@@ -66,6 +68,13 @@ abstract class BackendPluginBase extends ConfigurablePluginBase implements Backe
6668
*/
6769
protected $fieldsHelper;
6870

71+
/**
72+
* The messenger.
73+
*
74+
* @var \Drupal\Core\Messenger\MessengerInterface|null
75+
*/
76+
protected $messenger;
77+
6978
/**
7079
* {@inheritdoc}
7180
*/
@@ -77,6 +86,19 @@ public function __construct(array $configuration, $plugin_id, array $plugin_defi
7786
parent::__construct($configuration, $plugin_id, $plugin_definition);
7887
}
7988

89+
/**
90+
* {@inheritdoc}
91+
*/
92+
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
93+
/** @var static $plugin */
94+
$plugin = parent::create($container, $configuration, $plugin_id, $plugin_definition);
95+
96+
$plugin->setFieldsHelper($container->get('search_api.fields_helper'));
97+
$plugin->setMessenger($container->get('messenger'));
98+
99+
return $plugin;
100+
}
101+
80102
/**
81103
* Retrieves the fields helper.
82104
*
@@ -100,6 +122,29 @@ public function setFieldsHelper(FieldsHelper $fields_helper) {
100122
return $this;
101123
}
102124

125+
/**
126+
* Retrieves the messenger.
127+
*
128+
* @return \Drupal\Core\Messenger\MessengerInterface
129+
* The messenger.
130+
*/
131+
public function getMessenger() {
132+
return $this->messenger ?: \Drupal::service('messenger');
133+
}
134+
135+
/**
136+
* Sets the messenger.
137+
*
138+
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
139+
* The new messenger.
140+
*
141+
* @return $this
142+
*/
143+
public function setMessenger(MessengerInterface $messenger) {
144+
$this->messenger = $messenger;
145+
return $this;
146+
}
147+
103148
/**
104149
* {@inheritdoc}
105150
*/
@@ -172,7 +217,7 @@ public function preDelete() {
172217
'%server' => $this->getServer()->label(),
173218
];
174219
$this->logException($e, '%type while deleting items from server %server: @message in %function (line %line of %file).', $vars);
175-
drupal_set_message($this->t('Deleting some of the items on the server failed. Check the logs for details. The server was still removed.'), 'error');
220+
$this->getMessenger()->addError($this->t('Deleting some of the items on the server failed. Check the logs for details. The server was still removed.'));
176221
}
177222
}
178223

Diff for: src/Backend/BackendSpecificInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function addIndex(IndexInterface $index);
127127
* Notifies the server that an index attached to it has been changed.
128128
*
129129
* If any user action is necessary as a result of this, the method should
130-
* use drupal_set_message() to notify the user.
130+
* set a message to notify the user.
131131
*
132132
* @param \Drupal\search_api\IndexInterface $index
133133
* The updated index.

Diff for: src/Controller/IndexController.php

+35-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Drupal\Core\Ajax\RemoveCommand;
88
use Drupal\Core\Controller\ControllerBase;
99
use Drupal\Core\EventSubscriber\AjaxResponseSubscriber;
10+
use Drupal\Core\Messenger\MessengerInterface;
1011
use Drupal\search_api\IndexInterface;
1112
use Drupal\search_api\SearchApiException;
1213
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -25,6 +26,13 @@ class IndexController extends ControllerBase {
2526
*/
2627
protected $requestStack;
2728

29+
/**
30+
* The messenger.
31+
*
32+
* @var \Drupal\Core\Messenger\MessengerInterface|null
33+
*/
34+
protected $messenger;
35+
2836
/**
2937
* {@inheritdoc}
3038
*/
@@ -33,6 +41,7 @@ public static function create(ContainerInterface $container) {
3341
$controller = parent::create($container);
3442

3543
$controller->setRequestStack($container->get('request_stack'));
44+
$controller->setMessenger($container->get('messenger'));
3645

3746
return $controller;
3847
}
@@ -70,6 +79,29 @@ public function setRequestStack(RequestStack $request_stack) {
7079
return $this;
7180
}
7281

82+
/**
83+
* Retrieves the messenger.
84+
*
85+
* @return \Drupal\Core\Messenger\MessengerInterface
86+
* The messenger.
87+
*/
88+
public function getMessenger() {
89+
return $this->messenger ?: \Drupal::service('messenger');
90+
}
91+
92+
/**
93+
* Sets the messenger.
94+
*
95+
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
96+
* The new messenger.
97+
*
98+
* @return $this
99+
*/
100+
public function setMessenger(MessengerInterface $messenger) {
101+
$this->messenger = $messenger;
102+
return $this;
103+
}
104+
73105
/**
74106
* Displays information about a search index.
75107
*
@@ -125,11 +157,11 @@ public function indexBypassEnable(IndexInterface $search_api_index) {
125157
// enabled if its server is not set or disabled.
126158
if ($search_api_index->status()) {
127159
// Notify the user about the status change.
128-
drupal_set_message($this->t('The search index %name has been enabled.', ['%name' => $search_api_index->label()]));
160+
$this->getMessenger()->addStatus($this->t('The search index %name has been enabled.', ['%name' => $search_api_index->label()]));
129161
}
130162
else {
131163
// Notify the user that the status change did not succeed.
132-
drupal_set_message($this->t('The search index %name could not be enabled. Check if its server is set and enabled.', ['%name' => $search_api_index->label()]));
164+
$this->getMessenger()->addStatus($this->t('The search index %name could not be enabled. Check if its server is set and enabled.', ['%name' => $search_api_index->label()]));
133165
}
134166

135167
// Redirect to the index's "View" page.
@@ -162,7 +194,7 @@ public function removeField(IndexInterface $search_api_index, $field_id) {
162194
}
163195
catch (SearchApiException $e) {
164196
$args['%field'] = $fields[$field_id]->getLabel();
165-
drupal_set_message($this->t('The field %field is locked and cannot be removed.', $args), 'error');
197+
$this->getMessenger()->addError($this->t('The field %field is locked and cannot be removed.', $args));
166198
}
167199
}
168200
else {

Diff for: src/Controller/ServerController.php

+45-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,57 @@
44

55
use Drupal\Component\Render\FormattableMarkup;
66
use Drupal\Core\Controller\ControllerBase;
7+
use Drupal\Core\Messenger\MessengerInterface;
78
use Drupal\search_api\ServerInterface;
9+
use Symfony\Component\DependencyInjection\ContainerInterface;
810

911
/**
1012
* Provides block routines for search server-specific routes.
1113
*/
1214
class ServerController extends ControllerBase {
1315

16+
/**
17+
* The messenger.
18+
*
19+
* @var \Drupal\Core\Messenger\MessengerInterface|null
20+
*/
21+
protected $messenger;
22+
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public static function create(ContainerInterface $container) {
27+
/** @var static $controller */
28+
$controller = parent::create($container);
29+
30+
$controller->setMessenger($container->get('messenger'));
31+
32+
return $controller;
33+
}
34+
35+
/**
36+
* Retrieves the messenger.
37+
*
38+
* @return \Drupal\Core\Messenger\MessengerInterface
39+
* The messenger.
40+
*/
41+
public function getMessenger() {
42+
return $this->messenger ?: \Drupal::service('messenger');
43+
}
44+
45+
/**
46+
* Sets the messenger.
47+
*
48+
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
49+
* The new messenger.
50+
*
51+
* @return $this
52+
*/
53+
public function setMessenger(MessengerInterface $messenger) {
54+
$this->messenger = $messenger;
55+
return $this;
56+
}
57+
1458
/**
1559
* Displays information about a search server.
1660
*
@@ -65,7 +109,7 @@ public function serverBypassEnable(ServerInterface $search_api_server) {
65109
$search_api_server->setStatus(TRUE)->save();
66110

67111
// Notify the user about the status change.
68-
drupal_set_message($this->t('The search server %name has been enabled.', ['%name' => $search_api_server->label()]));
112+
$this->getMessenger()->addStatus($this->t('The search server %name has been enabled.', ['%name' => $search_api_server->label()]));
69113

70114
// Redirect to the server's "View" page.
71115
$url = $search_api_server->toUrl('canonical');

Diff for: src/Form/FieldConfigurationForm.php

+26-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Drupal\Core\Entity\EntityTypeManagerInterface;
1111
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
1212
use Drupal\Core\Form\FormStateInterface;
13+
use Drupal\Core\Messenger\MessengerInterface;
1314
use Drupal\Core\Render\RendererInterface;
1415
use Drupal\search_api\Processor\ConfigurablePropertyInterface;
1516
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -44,11 +45,11 @@ class FieldConfigurationForm extends EntityForm {
4445
protected $formIdAttribute;
4546

4647
/**
47-
* {@inheritdoc}
48+
* The messenger.
49+
*
50+
* @var \Drupal\Core\Messenger\MessengerInterface
4851
*/
49-
public function getFormId() {
50-
return 'search_api_field_config';
51-
}
52+
protected $messenger;
5253

5354
/**
5455
* Constructs a FieldConfigurationForm object.
@@ -61,12 +62,15 @@ public function getFormId() {
6162
* The date formatter.
6263
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
6364
* The request stack.
65+
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
66+
* The messenger.
6467
*/
65-
public function __construct(EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer, DateFormatterInterface $date_formatter, RequestStack $request_stack) {
68+
public function __construct(EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer, DateFormatterInterface $date_formatter, RequestStack $request_stack, MessengerInterface $messenger) {
6669
$this->entityTypeManager = $entity_type_manager;
6770
$this->renderer = $renderer;
6871
$this->dateFormatter = $date_formatter;
6972
$this->requestStack = $request_stack;
73+
$this->messenger = $messenger;
7074
}
7175

7276
/**
@@ -77,8 +81,23 @@ public static function create(ContainerInterface $container) {
7781
$renderer = $container->get('renderer');
7882
$date_formatter = $container->get('date.formatter');
7983
$request_stack = $container->get('request_stack');
84+
$messenger = $container->get('messenger');
8085

81-
return new static($entity_type_manager, $renderer, $date_formatter, $request_stack);
86+
return new static($entity_type_manager, $renderer, $date_formatter, $request_stack, $messenger);
87+
}
88+
89+
/**
90+
* {@inheritdoc}
91+
*/
92+
public function getBaseFormId() {
93+
return NULL;
94+
}
95+
96+
/**
97+
* {@inheritdoc}
98+
*/
99+
public function getFormId() {
100+
return 'search_api_field_config';
82101
}
83102

84103
/**
@@ -179,7 +198,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
179198
$property = $field->getDataDefinition();
180199
$property->submitConfigurationForm($field, $form, $form_state);
181200

182-
drupal_set_message($this->t('The field configuration was successfully saved.'));
201+
$this->messenger->addStatus($this->t('The field configuration was successfully saved.'));
183202
if ($this->getRequest()->query->get('modal_redirect')) {
184203
$url = $this->entity->toUrl('add-fields-ajax')
185204
->setOption('query', [

0 commit comments

Comments
 (0)