layout_builder.install 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * @file
  4. * Contains install and update functions for Layout Builder.
  5. */
  6. use Drupal\Core\Cache\Cache;
  7. use Drupal\Core\Database\Database;
  8. use Drupal\Core\Entity\EntityTypeInterface;
  9. use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay;
  10. use Drupal\layout_builder\Section;
  11. /**
  12. * Implements hook_install().
  13. */
  14. function layout_builder_install() {
  15. $display_changed = FALSE;
  16. $displays = LayoutBuilderEntityViewDisplay::loadMultiple();
  17. /** @var \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface[] $displays */
  18. foreach ($displays as $display) {
  19. // Create the first section from any existing Field Layout settings.
  20. $field_layout = $display->getThirdPartySettings('field_layout');
  21. if (isset($field_layout['id'])) {
  22. $field_layout += ['settings' => []];
  23. $display
  24. ->enableLayoutBuilder()
  25. ->appendSection(new Section($field_layout['id'], $field_layout['settings']))
  26. ->save();
  27. $display_changed = TRUE;
  28. }
  29. }
  30. // Clear the rendered cache to ensure the new layout builder flow is used.
  31. // While in many cases the above change will not affect the rendered output,
  32. // the cacheability metadata will have changed and should be processed to
  33. // prepare for future changes.
  34. if ($display_changed) {
  35. Cache::invalidateTags(['rendered']);
  36. }
  37. }
  38. /**
  39. * Enable Layout Builder for existing entity displays.
  40. */
  41. function layout_builder_update_8601(&$sandbox) {
  42. $config_factory = \Drupal::configFactory();
  43. if (!isset($sandbox['count'])) {
  44. $sandbox['ids'] = $config_factory->listAll('core.entity_view_display.');
  45. $sandbox['count'] = count($sandbox['ids']);
  46. }
  47. $ids = array_splice($sandbox['ids'], 0, 50);
  48. foreach ($ids as $id) {
  49. $display = $config_factory->getEditable($id);
  50. if ($display->get('third_party_settings.layout_builder')) {
  51. $display
  52. ->set('third_party_settings.layout_builder.enabled', TRUE)
  53. ->save();
  54. }
  55. }
  56. $sandbox['#finished'] = empty($sandbox['ids']) ? 1 : ($sandbox['count'] - count($sandbox['ids'])) / $sandbox['count'];
  57. }
  58. /**
  59. * Implements hook_schema().
  60. */
  61. function layout_builder_schema() {
  62. $schema['inline_block_usage'] = [
  63. 'description' => 'Track where a block_content entity is used.',
  64. 'fields' => [
  65. 'block_content_id' => [
  66. 'description' => 'The block_content entity ID.',
  67. 'type' => 'int',
  68. 'unsigned' => TRUE,
  69. 'not null' => TRUE,
  70. ],
  71. 'layout_entity_type' => [
  72. 'description' => 'The entity type of the parent entity.',
  73. 'type' => 'varchar_ascii',
  74. 'length' => EntityTypeInterface::ID_MAX_LENGTH,
  75. 'not null' => FALSE,
  76. 'default' => '',
  77. ],
  78. 'layout_entity_id' => [
  79. 'description' => 'The ID of the parent entity.',
  80. 'type' => 'varchar_ascii',
  81. 'length' => 128,
  82. 'not null' => FALSE,
  83. 'default' => 0,
  84. ],
  85. ],
  86. 'primary key' => ['block_content_id'],
  87. 'indexes' => [
  88. 'type_id' => ['layout_entity_type', 'layout_entity_id'],
  89. ],
  90. ];
  91. return $schema;
  92. }
  93. /**
  94. * Create the 'inline_block_usage' table.
  95. */
  96. function layout_builder_update_8602() {
  97. $inline_block_usage = [
  98. 'description' => 'Track where a block_content entity is used.',
  99. 'fields' => [
  100. 'block_content_id' => [
  101. 'description' => 'The block_content entity ID.',
  102. 'type' => 'int',
  103. 'unsigned' => TRUE,
  104. 'not null' => TRUE,
  105. ],
  106. 'layout_entity_type' => [
  107. 'description' => 'The entity type of the parent entity.',
  108. 'type' => 'varchar_ascii',
  109. 'length' => EntityTypeInterface::ID_MAX_LENGTH,
  110. 'not null' => FALSE,
  111. 'default' => '',
  112. ],
  113. 'layout_entity_id' => [
  114. 'description' => 'The ID of the parent entity.',
  115. 'type' => 'varchar_ascii',
  116. 'length' => 128,
  117. 'not null' => FALSE,
  118. 'default' => 0,
  119. ],
  120. ],
  121. 'primary key' => ['block_content_id'],
  122. 'indexes' => [
  123. 'type_id' => ['layout_entity_type', 'layout_entity_id'],
  124. ],
  125. ];
  126. Database::getConnection()->schema()->createTable('inline_block_usage', $inline_block_usage);
  127. }