Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / datetime_range / datetime_range.post_update.php
1 <?php
2
3 /**
4  * @file
5  * Post-update functions for Datetime Range module.
6  */
7
8 use Drupal\views\Views;
9
10 /**
11  * Clear caches to ensure schema changes are read.
12  */
13 function datetime_range_post_update_translatable_separator() {
14   // Empty post-update hook to cause a cache rebuild.
15 }
16
17 /**
18  * Update existing views using datetime_range fields.
19  */
20 function datetime_range_post_update_views_string_plugin_id() {
21
22   /* @var \Drupal\views\Entity\View[] $views */
23   $views = \Drupal::entityTypeManager()->getStorage('view')->loadMultiple();
24   $config_factory = \Drupal::configFactory();
25   $message = NULL;
26   $ids = [];
27
28   foreach ($views as $view) {
29     $displays = $view->get('display');
30     $needs_bc_layer_update = FALSE;
31
32     foreach ($displays as $display_name => $display) {
33
34       // Check if datetime_range filters need updates.
35       if (!$needs_bc_layer_update && isset($display['display_options']['filters'])) {
36         foreach ($display['display_options']['filters'] as $field_name => $filter) {
37           if ($filter['plugin_id'] == 'string') {
38
39             // Get field config.
40             $filter_views_data = Views::viewsData()->get($filter['table'])[$filter['field']]['filter'];
41             if (!isset($filter_views_data['entity_type']) || !isset($filter_views_data['field_name'])) {
42               continue;
43             }
44             $field_storage_name = 'field.storage.' . $filter_views_data['entity_type'] . '.' . $filter_views_data['field_name'];
45             $field_configuration = $config_factory->get($field_storage_name);
46
47             if ($field_configuration->get('type') == 'daterange') {
48               // Trigger the BC layer control.
49               $needs_bc_layer_update = TRUE;
50               continue 2;
51             }
52           }
53         }
54       }
55
56       // Check if datetime_range sort handlers need updates.
57       if (!$needs_bc_layer_update && isset($display['display_options']['sorts'])) {
58         foreach ($display['display_options']['sorts'] as $field_name => $sort) {
59           if ($sort['plugin_id'] == 'standard') {
60
61             // Get field config.
62             $sort_views_data = Views::viewsData()->get($sort['table'])[$sort['field']]['sort'];
63             if (!isset($sort_views_data['entity_type']) || !isset($sort_views_data['field_name'])) {
64               continue;
65             }
66             $field_storage_name = 'field.storage.' . $sort_views_data['entity_type'] . '.' . $sort_views_data['field_name'];
67             $field_configuration = $config_factory->get($field_storage_name);
68
69             if ($field_configuration->get('type') == 'daterange') {
70               // Trigger the BC layer control.
71               $needs_bc_layer_update = TRUE;
72               continue 2;
73             }
74           }
75         }
76       }
77     }
78
79     // If current view needs BC layer updates save it and the hook view_presave
80     // will do the rest.
81     if ($needs_bc_layer_update) {
82       $view->save();
83       $ids[] = $view->id();
84     }
85   }
86
87   if (!empty($ids)) {
88     $message = \Drupal::translation()->translate('Updated datetime_range filter/sort plugins for views: @ids', ['@ids' => implode(', ', array_unique($ids))]);
89   }
90
91   return $message;
92 }