layout_builder.post_update.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. /**
  3. * @file
  4. * Post update functions for Layout Builder.
  5. */
  6. use Drupal\Core\Config\Entity\ConfigEntityUpdater;
  7. use Drupal\layout_builder\Entity\LayoutEntityDisplayInterface;
  8. use Drupal\layout_builder\TempStoreIdentifierInterface;
  9. use Drupal\user\Entity\Role;
  10. use Drupal\field\Entity\FieldStorageConfig;
  11. use Drupal\field\Entity\FieldConfig;
  12. use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage;
  13. /**
  14. * Rebuild plugin dependencies for all entity view displays.
  15. */
  16. function layout_builder_post_update_rebuild_plugin_dependencies(&$sandbox = NULL) {
  17. $storage = \Drupal::entityTypeManager()->getStorage('entity_view_display');
  18. if (!isset($sandbox['ids'])) {
  19. $sandbox['ids'] = $storage->getQuery()->accessCheck(FALSE)->execute();
  20. $sandbox['count'] = count($sandbox['ids']);
  21. }
  22. for ($i = 0; $i < 10 && count($sandbox['ids']); $i++) {
  23. $id = array_shift($sandbox['ids']);
  24. if ($display = $storage->load($id)) {
  25. $display->save();
  26. }
  27. }
  28. $sandbox['#finished'] = empty($sandbox['ids']) ? 1 : ($sandbox['count'] - count($sandbox['ids'])) / $sandbox['count'];
  29. }
  30. /**
  31. * Ensure all extra fields are properly stored on entity view displays.
  32. *
  33. * Previously
  34. * \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay::setComponent()
  35. * was not correctly setting the configuration for extra fields. This function
  36. * calls setComponent() for all extra field components to ensure the updated
  37. * logic is invoked on all extra fields to correct the settings.
  38. */
  39. function layout_builder_post_update_add_extra_fields(&$sandbox = NULL) {
  40. $entity_field_manager = \Drupal::service('entity_field.manager');
  41. \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'entity_view_display', function (LayoutEntityDisplayInterface $display) use ($entity_field_manager) {
  42. if (!$display->isLayoutBuilderEnabled()) {
  43. return FALSE;
  44. }
  45. $extra_fields = $entity_field_manager->getExtraFields($display->getTargetEntityTypeId(), $display->getTargetBundle());
  46. $components = $display->getComponents();
  47. // Sort the components to avoid them being reordered by setComponent().
  48. uasort($components, 'Drupal\Component\Utility\SortArray::sortByWeightElement');
  49. $result = FALSE;
  50. foreach ($components as $name => $component) {
  51. if (isset($extra_fields['display'][$name])) {
  52. $display->setComponent($name, $component);
  53. $result = TRUE;
  54. }
  55. }
  56. return $result;
  57. });
  58. }
  59. /**
  60. * Clear caches due to changes to section storage annotation changes.
  61. */
  62. function layout_builder_post_update_section_storage_context_definitions() {
  63. // Empty post-update hook.
  64. }
  65. /**
  66. * Clear caches due to changes to annotation changes to the Overrides plugin.
  67. */
  68. function layout_builder_post_update_overrides_view_mode_annotation() {
  69. // Empty post-update hook.
  70. }
  71. /**
  72. * Clear caches due to routing changes for the new discard changes form.
  73. */
  74. function layout_builder_post_update_cancel_link_to_discard_changes_form() {
  75. // Empty post-update hook.
  76. }
  77. /**
  78. * Clear caches due to the removal of the layout_is_rebuilding query string.
  79. */
  80. function layout_builder_post_update_remove_layout_is_rebuilding() {
  81. // Empty post-update hook.
  82. }
  83. /**
  84. * Clear caches due to routing changes to move the Layout Builder UI to forms.
  85. */
  86. function layout_builder_post_update_routing_entity_form() {
  87. // Empty post-update hook.
  88. }
  89. /**
  90. * Clear caches to discover new blank layout plugin.
  91. */
  92. function layout_builder_post_update_discover_blank_layout_plugin() {
  93. // Empty post-update hook.
  94. }
  95. /**
  96. * Clear caches due to routing changes to changing the URLs for defaults.
  97. */
  98. function layout_builder_post_update_routing_defaults() {
  99. // Empty post-update hook.
  100. }
  101. /**
  102. * Clear caches due to new link added to Layout Builder's contextual links.
  103. */
  104. function layout_builder_post_update_discover_new_contextual_links() {
  105. // Empty post-update hook.
  106. }
  107. /**
  108. * Fix Layout Builder tempstore keys of existing entries.
  109. */
  110. function layout_builder_post_update_fix_tempstore_keys() {
  111. /** @var \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface $section_storage_manager */
  112. $section_storage_manager = \Drupal::service('plugin.manager.layout_builder.section_storage');
  113. /** @var \Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface $key_value_factory */
  114. $key_value_factory = \Drupal::service('keyvalue.expirable');
  115. // Loop through each section storage type.
  116. foreach (array_keys($section_storage_manager->getDefinitions()) as $section_storage_type) {
  117. $key_value = $key_value_factory->get("tempstore.shared.layout_builder.section_storage.$section_storage_type");
  118. foreach ($key_value->getAll() as $key => $value) {
  119. $contexts = $section_storage_manager->loadEmpty($section_storage_type)->deriveContextsFromRoute($key, [], '', []);
  120. if ($section_storage = $section_storage_manager->load($section_storage_type, $contexts)) {
  121. // Some overrides were stored with an incorrect view mode value. Update
  122. // the view mode on the temporary section storage, if necessary.
  123. if ($section_storage_type === 'overrides') {
  124. $view_mode = $value->data['section_storage']->getContextValue('view_mode');
  125. $new_view_mode = $section_storage->getContextValue('view_mode');
  126. if ($view_mode !== $new_view_mode) {
  127. $value->data['section_storage']->setContextValue('view_mode', $new_view_mode);
  128. $key_value->set($key, $value);
  129. }
  130. }
  131. // The previous tempstore key names were exact matches with the section
  132. // storage ID. Attempt to load the corresponding section storage and
  133. // rename the tempstore entry if the section storage provides a more
  134. // granular tempstore key.
  135. if ($section_storage instanceof TempStoreIdentifierInterface) {
  136. $new_key = $section_storage->getTempstoreKey();
  137. if ($key !== $new_key) {
  138. if ($key_value->has($new_key)) {
  139. $key_value->delete($new_key);
  140. }
  141. $key_value->rename($key, $new_key);
  142. }
  143. }
  144. }
  145. }
  146. }
  147. }
  148. /**
  149. * Clear caches due to config schema additions.
  150. */
  151. function layout_builder_post_update_section_third_party_settings_schema() {
  152. // Empty post-update hook.
  153. }
  154. /**
  155. * Clear caches due to dependency changes in the layout_builder render element.
  156. */
  157. function layout_builder_post_update_layout_builder_dependency_change() {
  158. // Empty post-update hook.
  159. }
  160. /**
  161. * Add new custom block permission to all roles with 'configure any layout'.
  162. */
  163. function layout_builder_post_update_update_permissions() {
  164. foreach (Role::loadMultiple() as $role) {
  165. if ($role->hasPermission('configure any layout')) {
  166. $role->grantPermission('create and edit custom blocks')->save();
  167. }
  168. }
  169. }
  170. /**
  171. * Clear caches due to addition of service decorator for entity form controller.
  172. */
  173. function layout_builder_post_update_override_entity_form_controller() {
  174. // Empty post-update hook.
  175. }
  176. /**
  177. * Set the layout builder field as non-translatable where possible.
  178. */
  179. function layout_builder_post_update_make_layout_untranslatable() {
  180. /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager */
  181. $field_manager = \Drupal::service('entity_field.manager');
  182. $field_map = $field_manager->getFieldMap();
  183. foreach ($field_map as $entity_type_id => $field_infos) {
  184. if (isset($field_infos[OverridesSectionStorage::FIELD_NAME]['bundles'])) {
  185. $non_translatable_bundle_count = 0;
  186. foreach ($field_infos[OverridesSectionStorage::FIELD_NAME]['bundles'] as $bundle) {
  187. // The field map can contain stale information. If the field does not
  188. // exist, ignore it. The field map will be rebuilt when the cache is
  189. // cleared at the end of the update process.
  190. if (!$field_config = FieldConfig::loadByName($entity_type_id, $bundle, OverridesSectionStorage::FIELD_NAME)) {
  191. continue;
  192. }
  193. if (!$field_config->isTranslatable()) {
  194. $non_translatable_bundle_count++;
  195. // The layout field is already configured to be non-translatable so it
  196. // does not need to be updated.
  197. continue;
  198. }
  199. if (_layout_builder_bundle_has_no_translations($entity_type_id, $bundle) || _layout_builder_bundle_has_no_layouts($entity_type_id, $bundle)) {
  200. // Either none of the entities have layouts or none of them have
  201. // translations. In either case it is safe to set the field to be
  202. // non-translatable.
  203. $field_config->setTranslatable(FALSE);
  204. $field_config->save();
  205. $non_translatable_bundle_count++;
  206. }
  207. }
  208. // Set the field storage to untranslatable if the field config for each
  209. // bundle is now untranslatable. This removes layout fields for the
  210. // entity type from the Content Translation configuration form.
  211. if (count($field_infos[OverridesSectionStorage::FIELD_NAME]['bundles']) === $non_translatable_bundle_count) {
  212. $field_storage = FieldStorageConfig::loadByName($entity_type_id, OverridesSectionStorage::FIELD_NAME);
  213. $field_storage->setTranslatable(FALSE);
  214. $field_storage->save();
  215. }
  216. }
  217. }
  218. }
  219. /**
  220. * Determines if there are zero layout overrides for the bundle.
  221. *
  222. * @param string $entity_type_id
  223. * The entity type ID.
  224. * @param string $bundle
  225. * The bundle name.
  226. *
  227. * @return bool
  228. * TRUE if there are zero layout overrides for the bundle, otherwise FALSE.
  229. */
  230. function _layout_builder_bundle_has_no_layouts($entity_type_id, $bundle) {
  231. $entity_update_manager = \Drupal::entityDefinitionUpdateManager();
  232. $entity_type = $entity_update_manager->getEntityType($entity_type_id);
  233. $bundle_key = $entity_type->getKey('bundle');
  234. $query = \Drupal::entityTypeManager()->getStorage($entity_type_id)->getQuery();
  235. if ($bundle_key) {
  236. $query->condition($bundle_key, $bundle);
  237. }
  238. if ($entity_type->isRevisionable()) {
  239. $query->allRevisions();
  240. }
  241. $query->exists(OverridesSectionStorage::FIELD_NAME)
  242. ->accessCheck(FALSE)
  243. ->range(0, 1);
  244. $results = $query->execute();
  245. return empty($results);
  246. }
  247. /**
  248. * Determines if there are zero translations for the bundle.
  249. *
  250. * @param string $entity_type_id
  251. * The entity type ID.
  252. * @param string $bundle
  253. * The bundle name.
  254. *
  255. * @return bool
  256. * TRUE if there are zero translations for the bundle, otherwise FALSE.
  257. */
  258. function _layout_builder_bundle_has_no_translations($entity_type_id, $bundle) {
  259. $entity_update_manager = \Drupal::entityDefinitionUpdateManager();
  260. $entity_type = $entity_update_manager->getEntityType($entity_type_id);
  261. if (!$entity_type->isTranslatable()) {
  262. return TRUE;
  263. }
  264. $query = \Drupal::entityTypeManager()->getStorage($entity_type_id)->getQuery();
  265. $bundle_key = $entity_type->getKey('bundle');
  266. if ($entity_type->hasKey('default_langcode')) {
  267. if ($bundle_key) {
  268. $query->condition($bundle_key, $bundle);
  269. }
  270. if ($entity_type->isRevisionable()) {
  271. $query->allRevisions();
  272. }
  273. $query->condition($entity_type->getKey('default_langcode'), 0)
  274. ->accessCheck(FALSE)
  275. ->range(0, 1);
  276. $results = $query->execute();
  277. return empty($results);
  278. }
  279. // A translatable entity type should always have a default_langcode key. If it
  280. // doesn't we have no way to determine if there are translations.
  281. return FALSE;
  282. }