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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
<?php
namespace Drupal\views;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a BC layer for modules providing old configurations.
*
* @internal
*/
class ViewsConfigUpdater implements ContainerInjectionInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
* The typed config manager.
*
* @var \Drupal\Core\Config\TypedConfigManagerInterface
*/
protected $typedConfigManager;
/**
* The views data service.
*
* @var \Drupal\views\ViewsData
*/
protected $viewsData;
/**
* The formatter plugin manager service.
*
* @var \Drupal\Component\Plugin\PluginManagerInterface
*/
protected $formatterPluginManager;
/**
* Flag determining whether deprecations should be triggered.
*
* @var bool
*/
protected $deprecationsEnabled = TRUE;
/**
* Stores which deprecations were triggered.
*
* @var bool
*/
protected $triggeredDeprecations = [];
/**
* ViewsConfigUpdater constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager.
* @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config_manager
* The typed config manager.
* @param \Drupal\views\ViewsData $views_data
* The views data service.
* @param \Drupal\Component\Plugin\PluginManagerInterface $formatter_plugin_manager
* The formatter plugin manager service.
*/
public function __construct(
EntityTypeManagerInterface $entity_type_manager,
EntityFieldManagerInterface $entity_field_manager,
TypedConfigManagerInterface $typed_config_manager,
ViewsData $views_data,
PluginManagerInterface $formatter_plugin_manager,
) {
$this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager;
$this->typedConfigManager = $typed_config_manager;
$this->viewsData = $views_data;
$this->formatterPluginManager = $formatter_plugin_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager'),
$container->get('entity_field.manager'),
$container->get('config.typed'),
$container->get('views.views_data'),
$container->get('plugin.manager.field.formatter')
);
}
/**
* Sets the deprecations enabling status.
*
* @param bool $enabled
* Whether deprecations should be enabled.
*/
public function setDeprecationsEnabled($enabled) {
$this->deprecationsEnabled = $enabled;
}
/**
* Performs all required updates.
*
* @param \Drupal\views\ViewEntityInterface $view
* The View to update.
*
* @return bool
* Whether the view was updated.
*/
public function updateAll(ViewEntityInterface $view) {
return $this->processDisplayHandlers($view, FALSE, function (&$handler, $handler_type, $key, $display_id) use ($view) {
$changed = FALSE;
if ($this->processEntityArgumentUpdate($view)) {
$changed = TRUE;
}
if ($this->processRememberRolesUpdate($handler, $handler_type)) {
$changed = TRUE;
}
return $changed;
});
}
/**
* Processes all display handlers.
*
* @param \Drupal\views\ViewEntityInterface $view
* The View to update.
* @param bool $return_on_changed
* Whether processing should stop after a change is detected.
* @param callable $handler_processor
* A callback performing the actual update.
*
* @return bool
* Whether the view was updated.
*/
protected function processDisplayHandlers(ViewEntityInterface $view, $return_on_changed, callable $handler_processor) {
$changed = FALSE;
$displays = $view->get('display');
$handler_types = [
'field' => 'fields',
'argument' => 'arguments',
'sort' => 'sorts',
'relationship' => 'relationships',
'filter' => 'filters',
'pager' => 'pager',
];
$compound_display_handlers = [
'pager',
];
foreach ($displays as $display_id => &$display) {
foreach ($handler_types as $handler_type => $handler_type_lookup) {
if (!empty($display['display_options'][$handler_type_lookup])) {
if (in_array($handler_type_lookup, $compound_display_handlers)) {
if ($handler_processor($display['display_options'][$handler_type_lookup], $handler_type, NULL, $display_id)) {
$changed = TRUE;
if ($return_on_changed) {
return $changed;
}
}
continue;
}
foreach ($display['display_options'][$handler_type_lookup] as $key => &$handler) {
if (is_array($handler) && $handler_processor($handler, $handler_type, $key, $display_id)) {
$changed = TRUE;
if ($return_on_changed) {
return $changed;
}
}
}
}
}
}
if ($changed) {
$view->set('display', $displays);
}
return $changed;
}
/**
* Checks if 'numeric' arguments should be converted to 'entity_target_id'.
*
* @param \Drupal\views\ViewEntityInterface $view
* The view entity.
*
* @return bool
* TRUE if the view has any arguments that reference an entity reference
* that need to be converted from 'numeric' to 'entity_target_id'.
*/
public function needsEntityArgumentUpdate(ViewEntityInterface $view): bool {
return $this->processDisplayHandlers($view, TRUE, function (&$handler, $handler_type) use ($view) {
return $this->processEntityArgumentUpdate($view);
});
}
/**
* Processes arguments and convert 'numeric' to 'entity_target_id' if needed.
*
* Note that since this update will trigger deprecations if called by
* views_view_presave(), we cannot rely on the usual handler-specific checking
* and processing. That would still hit views_view_presave(), even when
* invoked from post_update. We must directly update the view here, so that
* it's already correct by the time views_view_presave() sees it.
*
* @param \Drupal\views\ViewEntityInterface $view
* The View being updated.
*
* @return bool
* Whether the view was updated.
*/
public function processEntityArgumentUpdate(ViewEntityInterface $view): bool {
$changed = FALSE;
$displays = $view->get('display');
foreach ($displays as &$display) {
if (isset($display['display_options']['arguments'])) {
foreach ($display['display_options']['arguments'] as $argument_id => $argument) {
$plugin_id = $argument['plugin_id'] ?? '';
if ($plugin_id === 'numeric') {
$argument_table_data = $this->viewsData->get($argument['table']);
$argument_definition = $argument_table_data[$argument['field']]['argument'] ?? [];
if (isset($argument_definition['id']) && $argument_definition['id'] === 'entity_target_id') {
$argument['plugin_id'] = 'entity_target_id';
$argument['target_entity_type_id'] = $argument_definition['target_entity_type_id'];
$display['display_options']['arguments'][$argument_id] = $argument;
$changed = TRUE;
}
}
}
}
}
if ($changed) {
$view->set('display', $displays);
}
$deprecations_triggered = &$this->triggeredDeprecations['2640994'][$view->id()];
if ($this->deprecationsEnabled && $changed && !$deprecations_triggered) {
$deprecations_triggered = TRUE;
@trigger_error(sprintf('The update to convert "numeric" arguments to "entity_target_id" for entity reference fields for view "%s" is deprecated in drupal:10.3.0 and is removed from drupal:12.0.0. Profile, module and theme provided configuration should be updated. See https://www.drupal.org/node/3441945', $view->id()), E_USER_DEPRECATED);
}
return $changed;
}
/**
* Checks if 'remember_roles' setting of an exposed filter has disabled roles.
*
* @param \Drupal\views\ViewEntityInterface $view
* The view entity.
*
* @return bool
* TRUE if the view has any disabled roles.
*/
public function needsRememberRolesUpdate(ViewEntityInterface $view): bool {
return $this->processDisplayHandlers($view, TRUE, function (&$handler, $handler_type) {
return $this->processRememberRolesUpdate($handler, $handler_type);
});
}
/**
* Processes filters and removes disabled remember roles.
*
* @param array $handler
* A display handler.
* @param string $handler_type
* The handler type.
*
* @return bool
* Whether the handler was updated.
*/
public function processRememberRolesUpdate(array &$handler, string $handler_type): bool {
if ($handler_type === 'filter' && !empty($handler['expose']['remember_roles'])) {
$needsUpdate = FALSE;
foreach (array_keys($handler['expose']['remember_roles'], '0', TRUE) as $role_key) {
unset($handler['expose']['remember_roles'][$role_key]);
$needsUpdate = TRUE;
}
return $needsUpdate;
}
return FALSE;
}
/**
* Checks for table style views needing a default CSS table class value.
*
* @param \Drupal\views\ViewEntityInterface $view
* The view entity.
*
* @return bool
* TRUE if the view has any table styles that need to have
* a default table CSS class added.
*/
public function needsTableCssClassUpdate(ViewEntityInterface $view): bool {
return $this->processDisplayHandlers($view, TRUE, function (&$handler, $handler_type) use ($view) {
return $this->processTableCssClassUpdate($view);
});
}
/**
* Processes views and adds default CSS table class value if necessary.
*
* @param \Drupal\views\ViewEntityInterface $view
* The view entity.
*
* @return bool
* TRUE if the view was updated with a default table CSS class value.
*/
public function processTableCssClassUpdate(ViewEntityInterface $view): bool {
$changed = FALSE;
$displays = $view->get('display');
foreach ($displays as &$display) {
if (
isset($display['display_options']['style']) &&
$display['display_options']['style']['type'] === 'table' &&
!isset($display['display_options']['style']['options']['class'])
) {
$display['display_options']['style']['options']['class'] = '';
$changed = TRUE;
}
}
if ($changed) {
$view->set('display', $displays);
}
return $changed;
}
}
|