views.post_update.php 13 KB

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