datetime_range.post_update.php 3.1 KB

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