5 * Contains install and update functions for Views.
8 use Drupal\Core\Config\Schema\ArrayElement;
9 use Drupal\views\Views;
12 * Implements hook_install().
14 function views_install() {
15 module_set_weight('views', 10);
19 * Update views field plugins.
21 function views_update_8001(&$sandbox) {
22 $config_factory = \Drupal::configFactory();
35 foreach ($config_factory->listAll('views.view.') as $view_config_name) {
36 $view = $config_factory->getEditable($view_config_name);
38 $displays = $view->get('display');
40 foreach ($displays as $display_name => $display) {
41 if (!empty($display['display_options']['fields'])) {
42 foreach ($display['display_options']['fields'] as $field_name => $field) {
43 if (isset($field['entity_type']) && $field['plugin_id'] === 'date') {
44 $ids[] = $view->get('id');
46 // Grab the settings we need to move to a different place in the
48 $date_format = !empty($field['date_format']) ? $field['date_format'] : 'medium';
49 $custom_date_format = !empty($field['custom_date_format']) ? $field['custom_date_format'] : '';
50 $timezone = !empty($field['timezone']) ? $field['timezone'] : '';
52 // Save off the base part of the config path we are updating.
53 $base = "display.$display_name.display_options.fields.$field_name";
55 if (in_array($date_format, $ago_formats)) {
56 // Update the field to use the Field API formatter.
57 $view->set($base . '.plugin_id', 'field');
58 $view->set($base . '.type', 'timestamp_ago');
60 // Ensure the granularity is an integer, which is defined in the
61 // field.formatter.settings.timestamp_ago schema.
62 $granularity = is_numeric($custom_date_format) ? (int) $custom_date_format : 2;
64 // Add the new settings.
65 if ($date_format === 'time ago' || $date_format === 'time hence' || $date_format === 'time span') {
66 $view->set($base . '.settings.future_format', '@interval hence');
67 $view->set($base . '.settings.past_format', '@interval ago');
68 $view->set($base . '.settings.granularity', $granularity);
70 elseif ($date_format === 'raw time ago' || $date_format === 'raw time hence') {
71 $view->set($base . '.settings.future_format', '@interval');
72 $view->set($base . '.settings.past_format', '@interval');
73 $view->set($base . '.settings.granularity', $granularity);
75 elseif ($date_format === 'raw time span') {
76 $view->set($base . '.settings.future_format', '@interval');
77 $view->set($base . '.settings.past_format', '-@interval');
78 $view->set($base . '.settings.granularity', $granularity);
80 elseif ($date_format === 'inverse time span') {
81 $view->set($base . '.settings.future_format', '-@interval');
82 $view->set($base . '.settings.past_format', '@interval');
83 $view->set($base . '.settings.granularity', $granularity);
87 // Update the field to use the Field API formatter.
88 $view->set($base . '.plugin_id', 'field');
89 $view->set($base . '.type', 'timestamp');
91 // Add the new settings, and make sure everything is a string
92 // to conform with the field.formatter.settings.timestamp schema.
93 $view->set($base . '.settings.date_format', (string) $date_format);
94 $view->set($base . '.settings.custom_date_format', (string) $custom_date_format);
95 $view->set($base . '.settings.timezone', (string) $timezone);
98 // Remove the old settings.
99 $view->clear($base . '.date_format');
100 $view->clear($base . '.custom_date_format');
101 $view->clear($base . '.timezone');
111 $message = \Drupal::translation()->translate('Updated field plugins for views: @ids', ['@ids' => implode(', ', array_unique($ids))]);
118 * Updates %1 and !1 tokens to argument tokens.
120 function views_update_8002() {
121 $config_factory = \Drupal::configFactory();
122 foreach ($config_factory->listAll('views.view.') as $view_config_name) {
123 $view = $config_factory->getEditable($view_config_name);
125 $displays = $view->get('display');
126 $argument_map_per_display = _views_update_argument_map($displays);
130 // Update all the field settings, which support tokens.
131 foreach ($displays as $display_name => &$display) {
132 if (!empty($display['display_options']['fields'])) {
149 foreach ($display['display_options']['fields'] as $field_name => &$field) {
150 foreach ($token_values as $token_name) {
151 if (!empty($field['alter'][$token_name])) {
152 if (is_array($field['alter'][$token_name])) {
153 foreach (array_keys($field['alter'][$token_name]) as $key) {
154 $field['alter'][$token_name][$key] = _views_update_8002_token_update($field['alter'][$token_name][$key], $argument_map_per_display[$display_name]);
159 $field['alter'][$token_name] = _views_update_8002_token_update($field['alter'][$token_name], $argument_map_per_display[$display_name]);
168 // Update the area handlers with tokens.
169 foreach ($displays as $display_name => &$display) {
170 $area_types = ['header', 'footer', 'empty'];
171 foreach ($area_types as $area_type) {
172 if (!empty($display['display_options'][$area_type])) {
173 foreach ($display['display_options'][$area_type] as &$area) {
174 switch ($area['plugin_id']) {
176 $area['title'] = _views_update_8002_token_update($area['title'], $argument_map_per_display[$display_name]);
180 $area['content'] = _views_update_8002_token_update($area['content'], $argument_map_per_display[$display_name]);
184 $area['content']['value'] = _views_update_8002_token_update($area['content']['value'], $argument_map_per_display[$display_name]);
188 $area['content'] = _views_update_8002_token_update($area['content'], $argument_map_per_display[$display_name]);
192 $area['target'] = _views_update_8002_token_update($area['target'], $argument_map_per_display[$display_name]);
201 // Update the argument title settings.
202 foreach ($displays as $display_name => &$display) {
203 if (!empty($display['display_options']['arguments'])) {
204 foreach ($display['display_options']['arguments'] as &$argument) {
205 if (isset($argument['exception']['title'])) {
206 $argument['exception']['title'] = _views_update_8002_token_update($argument['exception']['title'], $argument_map_per_display[$display_name]);
209 if (isset($argument['title'])) {
210 $argument['title'] = _views_update_8002_token_update($argument['title'], $argument_map_per_display[$display_name]);
217 // Update the display title settings.
218 // Update the more link text and more link URL.
219 foreach ($displays as $display_name => &$display) {
220 if (!empty($display['display_options']['title'])) {
221 $display['display_options']['title'] = _views_update_8002_token_update($display['display_options']['title'], $argument_map_per_display[$display_name]);
224 if (!empty($display['display_options']['use_more_text'])) {
225 $display['display_options']['use_more_text'] = _views_update_8002_token_update($display['display_options']['use_more_text'], $argument_map_per_display[$display_name]);
228 if (!empty($display['display_options']['link_url'])) {
229 $display['display_options']['link_url'] = _views_update_8002_token_update($display['display_options']['link_url'], $argument_map_per_display[$display_name]);
234 // Update custom classes for row class + grid classes.
235 // Update RSS description field.
236 foreach ($displays as $display_name => &$display) {
237 if (!empty($display['display_options']['style'])) {
238 if (!empty($display['display_options']['style']['options']['row_class_custom'])) {
239 $display['display_options']['style']['options']['row_class_custom'] = _views_update_8002_token_update($display['display_options']['style']['options']['row_class_custom'], $argument_map_per_display[$display_name]);
242 if (!empty($display['display_options']['style']['options']['col_class_custom'])) {
243 $display['display_options']['style']['options']['col_class_custom'] = _views_update_8002_token_update($display['display_options']['style']['options']['col_class_custom'], $argument_map_per_display[$display_name]);
246 if (!empty($display['display_options']['style']['options']['description'])) {
247 $display['display_options']['style']['options']['description'] = _views_update_8002_token_update($display['display_options']['style']['options']['description'], $argument_map_per_display[$display_name]);
254 $view->set('display', $displays);
261 * Updates a views configuration string from using %/! to twig tokens.
263 * @param string $text
264 * Text in which to search for argument tokens and replace them with their
265 * twig representation.
266 * @param array $argument_map
267 * A map of argument machine names keyed by their previous index.
272 function _views_update_8002_token_update($text, array $argument_map) {
273 $text = preg_replace_callback('/%(\d)/', function ($match) use ($argument_map) {
274 return "{{ arguments.{$argument_map[$match[1]]} }}";
276 $text = preg_replace_callback('/!(\d)/', function ($match) use ($argument_map) {
277 return "{{ raw_arguments.{$argument_map[$match[1]]} }}";
284 * Builds an argument map for each Views display.
286 * @param array $displays
287 * A list of Views displays.
290 * The argument map keyed by display id.
292 function _views_update_argument_map($displays) {
294 foreach ($displays as $display_id => $display) {
295 $argument_map[$display_id] = [];
296 if (isset($display['display_options']['arguments'])) {
297 foreach (array_keys($display['display_options']['arguments']) as $number => $name) {
298 $argument_map[$display_id][$number + 1] = $name;
301 elseif (isset($displays['default']['display_options']['arguments'])) {
302 foreach (array_keys($displays['default']['display_options']['arguments']) as $number => $name) {
303 $argument_map[$display_id][$number + 1] = $name;
308 return $argument_map;
312 * Clear caches to fix entity operations field.
314 function views_update_8003() {
315 // Empty update to cause a cache flush so that views data is rebuilt. Entity
316 // types that don't implement a list builder cannot have the entity operations
321 * Clear caches due to updated entity views data.
323 function views_update_8004() {
324 // Empty update to cause a cache flush so that views data is rebuilt.
328 * Clear views data cache.
330 function views_update_8005() {
331 // Empty update function to rebuild the views data.
335 * Clear caches due to updated entity views data.
337 function views_update_8100() {
338 // Empty update to cause a cache flush so that views data is rebuilt.
342 * Set default values for enabled/expanded flag on page displays.
344 function views_update_8101() {
345 $config_factory = \Drupal::configFactory();
346 foreach ($config_factory->listAll('views.view.') as $view_config_name) {
347 $view = $config_factory->getEditable($view_config_name);
349 foreach ($view->get('display') as $display_id => $display) {
350 if ($display['display_plugin'] == 'page') {
351 $display['display_options']['menu']['enabled'] = TRUE;
352 $display['display_options']['menu']['expanded'] = FALSE;
353 $view->set("display.$display_id", $display);
364 * Rebuild the container to add a new container parameter.
366 function views_update_8200() {
367 // Empty update to cause a cache rebuild so that the container is rebuilt.
371 * Rebuild cache to refresh the views config schema.
373 function views_update_8201() {
374 // Empty update to cause a cache rebuild so that config schema get refreshed.
378 * Update field names for multi-value base fields.
380 function views_update_8500() {
381 // Find all multi-value base fields for content entities.
382 $entity_type_manager = \Drupal::entityTypeManager();
383 $entity_field_manager = \Drupal::service('entity_field.manager');
384 $table_update_info = [];
386 foreach ($entity_type_manager->getDefinitions() as $entity_type_id => $entity_type) {
387 if ($entity_type->hasHandlerClass('views_data')) {
388 $base_field_definitions = $entity_field_manager->getBaseFieldDefinitions($entity_type_id);
390 $entity_storage = $entity_type_manager->getStorage($entity_type_id);
391 $table_mapping = $entity_storage->getTableMapping($base_field_definitions);
393 foreach ($base_field_definitions as $field_name => $base_field_definition) {
394 $base_field_storage_definition = $base_field_definition->getFieldStorageDefinition();
396 // Skip single value and custom storage base fields.
397 if (!$base_field_storage_definition->isMultiple() || $base_field_storage_definition->hasCustomStorage()) {
401 // Get the actual table, as well as the column for the main property
402 // name so we can perform an update later on the views.
403 $table_name = $table_mapping->getFieldTableName($field_name);
404 $main_property_name = $base_field_storage_definition->getMainPropertyName();
406 $table_update_info[$table_name][$field_name] = $table_mapping->getFieldColumnName($base_field_storage_definition, $main_property_name);
411 if (empty($table_update_info)) {
415 $config_factory = \Drupal::configFactory();
416 /** @var \Drupal\Core\Config\TypedConfigManagerInterface $typed_config_manager */
417 $typed_config_manager = \Drupal::service('config.typed');
418 $views_data = Views::viewsData();
419 $handler_types = ['field', 'argument', 'sort', 'relationship', 'filter'];
421 $required_cleanup_handlers = [];
422 foreach ($config_factory->listAll('views.view.') as $id) {
423 $view = $config_factory->getEditable($id);
426 foreach ($view->get('display') as $display_id => &$display) {
427 foreach ($handler_types as $handler_type_singular) {
428 $handler_type_plural = $handler_type_singular . 's';
429 $handler_data = $view->get("display.$display_id.display_options.$handler_type_plural");
431 if (empty($handler_data)) {
435 foreach ($handler_data as $key => $data) {
436 // If this handler has a table we're interested in, update the field
438 $table = $data['table'];
439 if (isset($table_update_info[$table])) {
440 $path_to_handler = "display.$display_id.display_options.$handler_type_plural.$key";
441 $path_field = "{$path_to_handler}.field";
442 $path_plugin_id = "{$path_to_handler}.plugin_id";
443 $original_field_name = $view->get($path_field);
445 // Only if the wrong field name is set do we change the field. It
446 // could already be using the correct field. Like
447 // user__roles/roles_target_id.
448 if (isset($table_update_info[$table][$original_field_name])) {
449 $required_cleanup_handlers[$id][] = $path_to_handler;
451 // Set both the new table field as well as new 'plugin_id' field.
452 $view->set($path_field, $table_update_info[$table][$original_field_name]);
453 $view->set($path_plugin_id, $views_data->get($table)[$table_update_info[$table][$original_field_name]][$handler_type_singular]['id']);
467 // Beside of updating the field and plugin ID we also need to truncate orphan
468 // keys so he configuration applies to the config schema.
469 // We cannot do that inline in the other code, due to caching issues with
470 // typed configuration.
471 foreach ($required_cleanup_handlers as $id => $paths_to_handlers) {
473 $typed_view = $typed_config_manager->get($id);
474 $view = $config_factory->getEditable($id);
475 foreach ($paths_to_handlers as $path_to_handler) {
476 /** @var \Drupal\Core\Config\Schema\TypedConfigInterface $typed_view */
478 /** @var \Drupal\Core\Config\Schema\ArrayElement $typed_config */
479 $typed_config = $typed_view->get($path_to_handler);
480 $config = $typed_config->getValue();
482 // Filter values we want to convert from a string to an array.
483 if (strpos($path_to_handler, 'filters') !== FALSE && $typed_config->get('value') instanceof ArrayElement && is_string($config['value'])) {
484 // An empty string casted to an array is an array with one
486 if ($config['value'] === '') {
487 $config['value'] = [];
490 $config['value'] = (array) $config['value'];
494 // For all the other fields we try to determine the fields using
495 // config schema and remove everything which is not needed.
496 foreach (array_keys($config) as $config_key) {
497 if (!isset($typed_config->getDataDefinition()['mapping'][$config_key])) {
498 unset($config[$config_key]);
502 $typed_config->setValue($config);
503 $view->set($path_to_handler, $typed_config->getValue());