views.post_update.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * @file
  4. * Post update functions for Views.
  5. */
  6. use Drupal\Core\StringTranslation\TranslatableMarkup;
  7. use Drupal\views\Entity\View;
  8. use Drupal\views\Views;
  9. /**
  10. * Update the cacheability metadata for all views.
  11. */
  12. function views_post_update_update_cacheability_metadata() {
  13. // Load all views.
  14. $views = \Drupal::entityManager()->getStorage('view')->loadMultiple();
  15. /* @var \Drupal\views\Entity\View[] $views */
  16. foreach ($views as $view) {
  17. $displays = $view->get('display');
  18. foreach (array_keys($displays) as $display_id) {
  19. $display =& $view->getDisplay($display_id);
  20. // Unset the cache_metadata key, so all cacheability metadata for the
  21. // display is recalculated.
  22. unset($display['cache_metadata']);
  23. }
  24. $view->save();
  25. }
  26. }
  27. /**
  28. * Update some views fields that were previously duplicated.
  29. */
  30. function views_post_update_cleanup_duplicate_views_data() {
  31. $config_factory = \Drupal::configFactory();
  32. $ids = [];
  33. $message = NULL;
  34. $data_tables = [];
  35. $base_tables = [];
  36. $revision_tables = [];
  37. $entities_by_table = [];
  38. $duplicate_fields = [];
  39. $handler_types = Views::getHandlerTypes();
  40. /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
  41. $entity_type_manager = \Drupal::service('entity_type.manager');
  42. // This will allow us to create an index of all entity types of the site.
  43. foreach ($entity_type_manager->getDefinitions() as $entity_type_id => $entity_type) {
  44. // Store the entity keyed by base table. If it has a data table, use that as
  45. // well.
  46. if ($data_table = $entity_type->getDataTable()) {
  47. $entities_by_table[$data_table] = $entity_type;
  48. }
  49. if ($base_table = $entity_type->getBaseTable()) {
  50. $entities_by_table[$base_table] = $entity_type;
  51. }
  52. // The following code basically contains the same kind of logic as
  53. // \Drupal\Core\Entity\Sql\SqlContentEntityStorage::initTableLayout() to
  54. // prefetch all tables (base, data, revision, and revision data).
  55. $base_tables[$entity_type_id] = $entity_type->getBaseTable() ?: $entity_type->id();
  56. $revisionable = $entity_type->isRevisionable();
  57. $revision_table = '';
  58. if ($revisionable) {
  59. $revision_table = $entity_type->getRevisionTable() ?: $entity_type->id() . '_revision';
  60. }
  61. $revision_tables[$entity_type_id] = $revision_table;
  62. $translatable = $entity_type->isTranslatable();
  63. $data_table = '';
  64. // For example the data table just exists, when the entity type is
  65. // translatable.
  66. if ($translatable) {
  67. $data_table = $entity_type->getDataTable() ?: $entity_type->id() . '_field_data';
  68. }
  69. $data_tables[$entity_type_id] = $data_table;
  70. $duplicate_fields[$entity_type_id] = array_intersect_key($entity_type->getKeys(), array_flip(['id', 'revision', 'bundle']));
  71. }
  72. foreach ($config_factory->listAll('views.view.') as $view_config_name) {
  73. $changed = FALSE;
  74. $view = $config_factory->getEditable($view_config_name);
  75. $displays = $view->get('display');
  76. if (isset($entities_by_table[$view->get('base_table')])) {
  77. $entity_type = $entities_by_table[$view->get('base_table')];
  78. $entity_type_id = $entity_type->id();
  79. $data_table = $data_tables[$entity_type_id];
  80. $base_table = $base_tables[$entity_type_id];
  81. $revision_table = $revision_tables[$entity_type_id];
  82. if ($data_table) {
  83. foreach ($displays as $display_name => &$display) {
  84. foreach ($handler_types as $handler_type) {
  85. if (!empty($display['display_options'][$handler_type['plural']])) {
  86. foreach ($display['display_options'][$handler_type['plural']] as $field_name => &$field) {
  87. $table = $field['table'];
  88. if (($table === $base_table || $table === $revision_table) && in_array($field_name, $duplicate_fields[$entity_type_id])) {
  89. $field['table'] = $data_table;
  90. $changed = TRUE;
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }
  98. if ($changed) {
  99. $view->set('display', $displays);
  100. $view->save();
  101. $ids[] = $view->get('id');
  102. }
  103. }
  104. if (!empty($ids)) {
  105. $message = new TranslatableMarkup('Updated tables for field handlers for views: @ids', ['@ids' => implode(', ', array_unique($ids))]);
  106. }
  107. return $message;
  108. }
  109. /**
  110. * Include field formatter dependencies in a view when the formatter is used.
  111. */
  112. function views_post_update_field_formatter_dependencies() {
  113. $views = View::loadMultiple();
  114. array_walk($views, function(View $view) {
  115. $view->save();
  116. });
  117. }
  118. /**
  119. * Fix views with dependencies on taxonomy terms that don't exist.
  120. */
  121. function views_post_update_taxonomy_index_tid() {
  122. $views = View::loadMultiple();
  123. array_walk($views, function(View $view) {
  124. $old_dependencies = $view->getDependencies();
  125. $new_dependencies = $view->calculateDependencies()->getDependencies();
  126. if ($old_dependencies !== $new_dependencies) {
  127. $view->save();
  128. }
  129. });
  130. }
  131. /**
  132. * Fix views with serializer dependencies.
  133. */
  134. function views_post_update_serializer_dependencies() {
  135. $views = View::loadMultiple();
  136. array_walk($views, function(View $view) {
  137. $old_dependencies = $view->getDependencies();
  138. $new_dependencies = $view->calculateDependencies()->getDependencies();
  139. if ($old_dependencies !== $new_dependencies) {
  140. $view->save();
  141. }
  142. });
  143. }
  144. /**
  145. * Set all boolean filter values to strings.
  146. */
  147. function views_post_update_boolean_filter_values() {
  148. $config_factory = \Drupal::configFactory();
  149. foreach ($config_factory->listAll('views.view.') as $view_config_name) {
  150. $view = $config_factory->getEditable($view_config_name);
  151. $save = FALSE;
  152. foreach ($view->get('display') as $display_name => $display) {
  153. if (isset($display['display_options']['filters'])) {
  154. foreach ($display['display_options']['filters'] as $filter_name => $filter) {
  155. if (isset($filter['plugin_id']) && $filter['plugin_id'] === 'boolean') {
  156. $new_value = FALSE;
  157. // Update all boolean and integer values to strings.
  158. if ($filter['value'] === TRUE || $filter['value'] === 1) {
  159. $new_value = '1';
  160. }
  161. elseif ($filter['value'] === FALSE || $filter['value'] === 0) {
  162. $new_value = '0';
  163. }
  164. if ($new_value !== FALSE) {
  165. $view->set("display.$display_name.display_options.filters.$filter_name.value", $new_value);
  166. $save = TRUE;
  167. }
  168. }
  169. }
  170. }
  171. }
  172. if ($save) {
  173. $view->save();
  174. }
  175. }
  176. }
  177. /**
  178. * Rebuild caches to ensure schema changes are read in.
  179. */
  180. function views_post_update_grouped_filters() {
  181. // Empty update to cause a cache rebuild so that the schema changes are read.
  182. }