views.post_update.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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\Plugin\views\filter\NumericFilter;
  9. use Drupal\views\Plugin\views\filter\StringFilter;
  10. use Drupal\views\Views;
  11. /**
  12. * Update the cacheability metadata for all views.
  13. */
  14. function views_post_update_update_cacheability_metadata() {
  15. // Load all views.
  16. $views = \Drupal::entityManager()->getStorage('view')->loadMultiple();
  17. /* @var \Drupal\views\Entity\View[] $views */
  18. foreach ($views as $view) {
  19. $displays = $view->get('display');
  20. foreach (array_keys($displays) as $display_id) {
  21. $display =& $view->getDisplay($display_id);
  22. // Unset the cache_metadata key, so all cacheability metadata for the
  23. // display is recalculated.
  24. unset($display['cache_metadata']);
  25. }
  26. $view->save();
  27. }
  28. }
  29. /**
  30. * Update some views fields that were previously duplicated.
  31. */
  32. function views_post_update_cleanup_duplicate_views_data() {
  33. $config_factory = \Drupal::configFactory();
  34. $ids = [];
  35. $message = NULL;
  36. $data_tables = [];
  37. $base_tables = [];
  38. $revision_tables = [];
  39. $entities_by_table = [];
  40. $duplicate_fields = [];
  41. $handler_types = Views::getHandlerTypes();
  42. /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
  43. $entity_type_manager = \Drupal::service('entity_type.manager');
  44. // This will allow us to create an index of all entity types of the site.
  45. foreach ($entity_type_manager->getDefinitions() as $entity_type_id => $entity_type) {
  46. // Store the entity keyed by base table. If it has a data table, use that as
  47. // well.
  48. if ($data_table = $entity_type->getDataTable()) {
  49. $entities_by_table[$data_table] = $entity_type;
  50. }
  51. if ($base_table = $entity_type->getBaseTable()) {
  52. $entities_by_table[$base_table] = $entity_type;
  53. }
  54. // The following code basically contains the same kind of logic as
  55. // \Drupal\Core\Entity\Sql\SqlContentEntityStorage::initTableLayout() to
  56. // prefetch all tables (base, data, revision, and revision data).
  57. $base_tables[$entity_type_id] = $entity_type->getBaseTable() ?: $entity_type->id();
  58. $revisionable = $entity_type->isRevisionable();
  59. $revision_table = '';
  60. if ($revisionable) {
  61. $revision_table = $entity_type->getRevisionTable() ?: $entity_type->id() . '_revision';
  62. }
  63. $revision_tables[$entity_type_id] = $revision_table;
  64. $translatable = $entity_type->isTranslatable();
  65. $data_table = '';
  66. // For example the data table just exists, when the entity type is
  67. // translatable.
  68. if ($translatable) {
  69. $data_table = $entity_type->getDataTable() ?: $entity_type->id() . '_field_data';
  70. }
  71. $data_tables[$entity_type_id] = $data_table;
  72. $duplicate_fields[$entity_type_id] = array_intersect_key($entity_type->getKeys(), array_flip(['id', 'revision', 'bundle']));
  73. }
  74. foreach ($config_factory->listAll('views.view.') as $view_config_name) {
  75. $changed = FALSE;
  76. $view = $config_factory->getEditable($view_config_name);
  77. $displays = $view->get('display');
  78. if (isset($entities_by_table[$view->get('base_table')])) {
  79. $entity_type = $entities_by_table[$view->get('base_table')];
  80. $entity_type_id = $entity_type->id();
  81. $data_table = $data_tables[$entity_type_id];
  82. $base_table = $base_tables[$entity_type_id];
  83. $revision_table = $revision_tables[$entity_type_id];
  84. if ($data_table) {
  85. foreach ($displays as $display_name => &$display) {
  86. foreach ($handler_types as $handler_type) {
  87. if (!empty($display['display_options'][$handler_type['plural']])) {
  88. foreach ($display['display_options'][$handler_type['plural']] as $field_name => &$field) {
  89. $table = $field['table'];
  90. if (($table === $base_table || $table === $revision_table) && in_array($field_name, $duplicate_fields[$entity_type_id])) {
  91. $field['table'] = $data_table;
  92. $changed = TRUE;
  93. }
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }
  100. if ($changed) {
  101. $view->set('display', $displays);
  102. $view->save();
  103. $ids[] = $view->get('id');
  104. }
  105. }
  106. if (!empty($ids)) {
  107. $message = new TranslatableMarkup('Updated tables for field handlers for views: @ids', ['@ids' => implode(', ', array_unique($ids))]);
  108. }
  109. return $message;
  110. }
  111. /**
  112. * Include field formatter dependencies in a view when the formatter is used.
  113. */
  114. function views_post_update_field_formatter_dependencies() {
  115. $views = View::loadMultiple();
  116. array_walk($views, function (View $view) {
  117. $view->save();
  118. });
  119. }
  120. /**
  121. * Fix views with dependencies on taxonomy terms that don't exist.
  122. */
  123. function views_post_update_taxonomy_index_tid() {
  124. $views = View::loadMultiple();
  125. array_walk($views, function (View $view) {
  126. $old_dependencies = $view->getDependencies();
  127. $new_dependencies = $view->calculateDependencies()->getDependencies();
  128. if ($old_dependencies !== $new_dependencies) {
  129. $view->save();
  130. }
  131. });
  132. }
  133. /**
  134. * Fix views with serializer dependencies.
  135. */
  136. function views_post_update_serializer_dependencies() {
  137. $views = View::loadMultiple();
  138. array_walk($views, function (View $view) {
  139. $old_dependencies = $view->getDependencies();
  140. $new_dependencies = $view->calculateDependencies()->getDependencies();
  141. if ($old_dependencies !== $new_dependencies) {
  142. $view->save();
  143. }
  144. });
  145. }
  146. /**
  147. * Set all boolean filter values to strings.
  148. */
  149. function views_post_update_boolean_filter_values() {
  150. $config_factory = \Drupal::configFactory();
  151. foreach ($config_factory->listAll('views.view.') as $view_config_name) {
  152. $view = $config_factory->getEditable($view_config_name);
  153. $save = FALSE;
  154. foreach ($view->get('display') as $display_name => $display) {
  155. if (isset($display['display_options']['filters'])) {
  156. foreach ($display['display_options']['filters'] as $filter_name => $filter) {
  157. if (isset($filter['plugin_id']) && $filter['plugin_id'] === 'boolean') {
  158. $new_value = FALSE;
  159. // Update all boolean and integer values to strings.
  160. if ($filter['value'] === TRUE || $filter['value'] === 1) {
  161. $new_value = '1';
  162. }
  163. elseif ($filter['value'] === FALSE || $filter['value'] === 0) {
  164. $new_value = '0';
  165. }
  166. if ($new_value !== FALSE) {
  167. $view->set("display.$display_name.display_options.filters.$filter_name.value", $new_value);
  168. $save = TRUE;
  169. }
  170. }
  171. }
  172. }
  173. }
  174. if ($save) {
  175. $view->save();
  176. }
  177. }
  178. }
  179. /**
  180. * Rebuild caches to ensure schema changes are read in.
  181. */
  182. function views_post_update_grouped_filters() {
  183. // Empty update to cause a cache rebuild so that the schema changes are read.
  184. }
  185. /**
  186. * Fix table names for revision metadata fields.
  187. */
  188. function views_post_update_revision_metadata_fields() {
  189. // The table names are fixed automatically in
  190. // \Drupal\views\Entity\View::preSave(), so we just need to re-save all views.
  191. $views = View::loadMultiple();
  192. array_walk($views, function (View $view) {
  193. $view->save();
  194. });
  195. }
  196. /**
  197. * Add additional settings to the entity link field and convert node_path usage
  198. * to entity_link.
  199. */
  200. function views_post_update_entity_link_url() {
  201. // Load all views.
  202. $views = \Drupal::entityTypeManager()->getStorage('view')->loadMultiple();
  203. /* @var \Drupal\views\Entity\View[] $views */
  204. foreach ($views as $view) {
  205. $displays = $view->get('display');
  206. $changed = FALSE;
  207. foreach ($displays as $display_name => &$display) {
  208. if (isset($display['display_options']['fields'])) {
  209. foreach ($display['display_options']['fields'] as $field_name => &$field) {
  210. if (isset($field['plugin_id']) && $field['plugin_id'] === 'entity_link') {
  211. // Add any missing settings for entity_link.
  212. if (!isset($field['output_url_as_text'])) {
  213. $field['output_url_as_text'] = FALSE;
  214. $changed = TRUE;
  215. }
  216. if (!isset($field['absolute'])) {
  217. $field['absolute'] = FALSE;
  218. $changed = TRUE;
  219. }
  220. }
  221. elseif (isset($field['plugin_id']) && $field['plugin_id'] === 'node_path') {
  222. // Convert the use of node_path to entity_link.
  223. $field['plugin_id'] = 'entity_link';
  224. $field['field'] = 'view_node';
  225. $field['output_url_as_text'] = TRUE;
  226. $changed = TRUE;
  227. }
  228. }
  229. }
  230. }
  231. if ($changed) {
  232. $view->set('display', $displays);
  233. $view->save();
  234. }
  235. }
  236. }
  237. /**
  238. * Update dependencies for moved bulk field plugin.
  239. */
  240. function views_post_update_bulk_field_moved() {
  241. $views = View::loadMultiple();
  242. array_walk($views, function (View $view) {
  243. $old_dependencies = $view->getDependencies();
  244. $new_dependencies = $view->calculateDependencies()->getDependencies();
  245. if ($old_dependencies !== $new_dependencies) {
  246. $view->save();
  247. }
  248. });
  249. }
  250. /**
  251. * Add placeholder settings to string or numeric filters.
  252. */
  253. function views_post_update_filter_placeholder_text() {
  254. // Load all views.
  255. $views = \Drupal::entityTypeManager()->getStorage('view')->loadMultiple();
  256. /** @var \Drupal\views\Plugin\ViewsHandlerManager $filter_manager */
  257. $filter_manager = \Drupal::service('plugin.manager.views.filter');
  258. /* @var \Drupal\views\Entity\View[] $views */
  259. foreach ($views as $view) {
  260. $displays = $view->get('display');
  261. $save = FALSE;
  262. foreach ($displays as $display_name => &$display) {
  263. if (isset($display['display_options']['filters'])) {
  264. foreach ($display['display_options']['filters'] as $filter_name => &$filter) {
  265. // Any of the children of the modified classes will also be inheriting
  266. // the new settings.
  267. $filter_instance = $filter_manager->getHandler($filter);
  268. if ($filter_instance instanceof StringFilter) {
  269. if (!isset($filter['expose']['placeholder'])) {
  270. $filter['expose']['placeholder'] = '';
  271. $save = TRUE;
  272. }
  273. }
  274. elseif ($filter_instance instanceof NumericFilter) {
  275. if (!isset($filter['expose']['placeholder'])) {
  276. $filter['expose']['placeholder'] = '';
  277. $save = TRUE;
  278. }
  279. if (!isset($filter['expose']['min_placeholder'])) {
  280. $filter['expose']['min_placeholder'] = '';
  281. $save = TRUE;
  282. }
  283. if (!isset($filter['expose']['max_placeholder'])) {
  284. $filter['expose']['max_placeholder'] = '';
  285. $save = TRUE;
  286. }
  287. }
  288. }
  289. }
  290. }
  291. if ($save) {
  292. $view->set('display', $displays);
  293. $view->save();
  294. }
  295. }
  296. }
  297. /**
  298. * Include views data table provider in views dependencies.
  299. */
  300. function views_post_update_views_data_table_dependencies(&$sandbox = NULL) {
  301. $storage = \Drupal::entityTypeManager()->getStorage('view');
  302. if (!isset($sandbox['views'])) {
  303. $sandbox['views'] = $storage->getQuery()->accessCheck(FALSE)->execute();
  304. $sandbox['count'] = count($sandbox['views']);
  305. }
  306. // Process 10 views at a time.
  307. $views = $storage->loadMultiple(array_splice($sandbox['views'], 0, 10));
  308. foreach ($views as $view) {
  309. $original_dependencies = $view->getDependencies();
  310. // Only re-save if dependencies have changed.
  311. if ($view->calculateDependencies()->getDependencies() !== $original_dependencies) {
  312. // We can trust the data because we've already recalculated the
  313. // dependencies.
  314. $view->trustData();
  315. $view->save();
  316. }
  317. }
  318. $sandbox['#finished'] = empty($sandbox['views']) ? 1 : ($sandbox['count'] - count($sandbox['views'])) / $sandbox['count'];
  319. }
  320. /**
  321. * Fix cache max age for table displays.
  322. */
  323. function views_post_update_table_display_cache_max_age(&$sandbox = NULL) {
  324. $storage = \Drupal::entityTypeManager()->getStorage('view');
  325. if (!isset($sandbox['views'])) {
  326. $sandbox['views'] = $storage->getQuery()->accessCheck(FALSE)->execute();
  327. $sandbox['count'] = count($sandbox['views']);
  328. }
  329. for ($i = 0; $i < 10 && count($sandbox['views']); $i++) {
  330. $view_id = array_shift($sandbox['views']);
  331. if ($view = $storage->load($view_id)) {
  332. $displays = $view->get('display');
  333. foreach ($displays as $display_name => &$display) {
  334. if (isset($display['display_options']['style']['type']) && $display['display_options']['style']['type'] === 'table') {
  335. $view->save();
  336. }
  337. }
  338. }
  339. }
  340. $sandbox['#finished'] = empty($sandbox['views']) ? 1 : ($sandbox['count'] - count($sandbox['views'])) / $sandbox['count'];
  341. }