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
319 // Use hook_post_update_NAME() instead to clear the cache.The use
320 // of hook_update_N to clear the cache has been deprecated see
321 // https://www.drupal.org/node/2960601 for more details.
325 * Clear caches due to updated entity views data.
327 function views_update_8004() {
328 // Empty update to cause a cache flush so that views data is rebuilt.
330 // Use hook_post_update_NAME() instead to clear the cache.The use
331 // of hook_update_N to clear the cache has been deprecated see
332 // https://www.drupal.org/node/2960601 for more details.
336 * Clear views data cache.
338 function views_update_8005() {
339 // Empty update function to rebuild the views data.
341 // Use hook_post_update_NAME() instead to clear the cache.The use
342 // of hook_update_N to clear the cache has been deprecated see
343 // https://www.drupal.org/node/2960601 for more details.
347 * Clear caches due to updated entity views data.
349 function views_update_8100() {
350 // Empty update to cause a cache flush so that views data is rebuilt.
352 // Use hook_post_update_NAME() instead to clear the cache.The use
353 // of hook_update_N to clear the cache has been deprecated see
354 // https://www.drupal.org/node/2960601 for more details.
358 * Set default values for enabled/expanded flag on page displays.
360 function views_update_8101() {
361 $config_factory = \Drupal::configFactory();
362 foreach ($config_factory->listAll('views.view.') as $view_config_name) {
363 $view = $config_factory->getEditable($view_config_name);
365 foreach ($view->get('display') as $display_id => $display) {
366 if ($display['display_plugin'] == 'page') {
367 $display['display_options']['menu']['enabled'] = TRUE;
368 $display['display_options']['menu']['expanded'] = FALSE;
369 $view->set("display.$display_id", $display);
380 * Rebuild the container to add a new container parameter.
382 function views_update_8200() {
383 // Empty update to cause a cache rebuild so that the container is rebuilt.
385 // Use hook_post_update_NAME() instead to clear the cache.The use
386 // of hook_update_N to clear the cache has been deprecated see
387 // https://www.drupal.org/node/2960601 for more details.
391 * Rebuild cache to refresh the views config schema.
393 function views_update_8201() {
394 // Empty update to cause a cache rebuild so that config schema get refreshed.
396 // Use hook_post_update_NAME() instead to clear the cache.The use
397 // of hook_update_N to clear the cache has been deprecated see
398 // https://www.drupal.org/node/2960601 for more details.
402 * Update field names for multi-value base fields.
404 function views_update_8500() {
405 // Find all multi-value base fields for content entities.
406 $entity_type_manager = \Drupal::entityTypeManager();
407 $entity_field_manager = \Drupal::service('entity_field.manager');
408 $table_update_info = [];
410 foreach ($entity_type_manager->getDefinitions() as $entity_type_id => $entity_type) {
411 if ($entity_type->hasHandlerClass('views_data')) {
412 $base_field_definitions = $entity_field_manager->getBaseFieldDefinitions($entity_type_id);
414 $entity_storage = $entity_type_manager->getStorage($entity_type_id);
415 $table_mapping = $entity_storage->getTableMapping($base_field_definitions);
417 foreach ($base_field_definitions as $field_name => $base_field_definition) {
418 $base_field_storage_definition = $base_field_definition->getFieldStorageDefinition();
420 // Skip single value and custom storage base fields.
421 if (!$base_field_storage_definition->isMultiple() || $base_field_storage_definition->hasCustomStorage()) {
425 // Get the actual table, as well as the column for the main property
426 // name so we can perform an update later on the views.
427 $table_name = $table_mapping->getFieldTableName($field_name);
428 $main_property_name = $base_field_storage_definition->getMainPropertyName();
430 $table_update_info[$table_name][$field_name] = $table_mapping->getFieldColumnName($base_field_storage_definition, $main_property_name);
435 if (empty($table_update_info)) {
439 $config_factory = \Drupal::configFactory();
440 /** @var \Drupal\Core\Config\TypedConfigManagerInterface $typed_config_manager */
441 $typed_config_manager = \Drupal::service('config.typed');
442 $views_data = Views::viewsData();
443 $handler_types = ['field', 'argument', 'sort', 'relationship', 'filter'];
445 $required_cleanup_handlers = [];
446 foreach ($config_factory->listAll('views.view.') as $id) {
447 $view = $config_factory->getEditable($id);
450 foreach ($view->get('display') as $display_id => &$display) {
451 foreach ($handler_types as $handler_type_singular) {
452 $handler_type_plural = $handler_type_singular . 's';
453 $handler_data = $view->get("display.$display_id.display_options.$handler_type_plural");
455 if (empty($handler_data)) {
459 foreach ($handler_data as $key => $data) {
460 // If this handler has a table we're interested in, update the field
462 $table = $data['table'];
463 if (isset($table_update_info[$table])) {
464 $path_to_handler = "display.$display_id.display_options.$handler_type_plural.$key";
465 $path_field = "{$path_to_handler}.field";
466 $path_plugin_id = "{$path_to_handler}.plugin_id";
467 $original_field_name = $view->get($path_field);
469 // Only if the wrong field name is set do we change the field. It
470 // could already be using the correct field. Like
471 // user__roles/roles_target_id.
472 if (isset($table_update_info[$table][$original_field_name])) {
473 $required_cleanup_handlers[$id][] = $path_to_handler;
475 // Set both the new table field as well as new 'plugin_id' field.
476 $view->set($path_field, $table_update_info[$table][$original_field_name]);
477 $view->set($path_plugin_id, $views_data->get($table)[$table_update_info[$table][$original_field_name]][$handler_type_singular]['id']);
491 // Beside of updating the field and plugin ID we also need to truncate orphan
492 // keys so he configuration applies to the config schema.
493 // We cannot do that inline in the other code, due to caching issues with
494 // typed configuration.
495 foreach ($required_cleanup_handlers as $id => $paths_to_handlers) {
497 $typed_view = $typed_config_manager->get($id);
498 $view = $config_factory->getEditable($id);
499 foreach ($paths_to_handlers as $path_to_handler) {
500 /** @var \Drupal\Core\Config\Schema\TypedConfigInterface $typed_view */
502 /** @var \Drupal\Core\Config\Schema\ArrayElement $typed_config */
503 $typed_config = $typed_view->get($path_to_handler);
504 $config = $typed_config->getValue();
506 // Filter values we want to convert from a string to an array.
507 if (strpos($path_to_handler, 'filters') !== FALSE && $typed_config->get('value') instanceof ArrayElement && is_string($config['value'])) {
508 // An empty string casted to an array is an array with one
510 if ($config['value'] === '') {
511 $config['value'] = [];
514 $config['value'] = (array) $config['value'];
518 // For all the other fields we try to determine the fields using
519 // config schema and remove everything which is not needed.
520 foreach (array_keys($config) as $config_key) {
521 if (!isset($typed_config->getDataDefinition()['mapping'][$config_key])) {
522 unset($config[$config_key]);
526 $typed_config->setValue($config);
527 $view->set($path_to_handler, $typed_config->getValue());