5 * Contains install and update functions for Layout Builder.
8 use Drupal\Core\Cache\Cache;
9 use Drupal\Core\Database\Database;
10 use Drupal\Core\Entity\EntityTypeInterface;
11 use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay;
12 use Drupal\layout_builder\Section;
15 * Implements hook_install().
17 function layout_builder_install() {
18 $display_changed = FALSE;
20 $displays = LayoutBuilderEntityViewDisplay::loadMultiple();
21 /** @var \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface[] $displays */
22 foreach ($displays as $display) {
23 // Create the first section from any existing Field Layout settings.
24 $field_layout = $display->getThirdPartySettings('field_layout');
25 if (isset($field_layout['id'])) {
26 $field_layout += ['settings' => []];
28 ->enableLayoutBuilder()
29 ->appendSection(new Section($field_layout['id'], $field_layout['settings']))
31 $display_changed = TRUE;
35 // Clear the rendered cache to ensure the new layout builder flow is used.
36 // While in many cases the above change will not affect the rendered output,
37 // the cacheability metadata will have changed and should be processed to
38 // prepare for future changes.
39 if ($display_changed) {
40 Cache::invalidateTags(['rendered']);
45 * Enable Layout Builder for existing entity displays.
47 function layout_builder_update_8601(&$sandbox) {
48 $config_factory = \Drupal::configFactory();
50 if (!isset($sandbox['count'])) {
51 $sandbox['ids'] = $config_factory->listAll('core.entity_view_display.');
52 $sandbox['count'] = count($sandbox['ids']);
55 $ids = array_splice($sandbox['ids'], 0, 50);
56 foreach ($ids as $id) {
57 $display = $config_factory->getEditable($id);
58 if ($display->get('third_party_settings.layout_builder')) {
60 ->set('third_party_settings.layout_builder.enabled', TRUE)
65 $sandbox['#finished'] = empty($sandbox['ids']) ? 1 : ($sandbox['count'] - count($sandbox['ids'])) / $sandbox['count'];
69 * Implements hook_schema().
71 function layout_builder_schema() {
72 $schema['inline_block_usage'] = [
73 'description' => 'Track where a block_content entity is used.',
75 'block_content_id' => [
76 'description' => 'The block_content entity ID.',
81 'layout_entity_type' => [
82 'description' => 'The entity type of the parent entity.',
83 'type' => 'varchar_ascii',
84 'length' => EntityTypeInterface::ID_MAX_LENGTH,
88 'layout_entity_id' => [
89 'description' => 'The ID of the parent entity.',
90 'type' => 'varchar_ascii',
96 'primary key' => ['block_content_id'],
98 'type_id' => ['layout_entity_type', 'layout_entity_id'],
105 * Create the 'inline_block_usage' table.
107 function layout_builder_update_8602() {
108 $inline_block_usage = [
109 'description' => 'Track where a block_content entity is used.',
111 'block_content_id' => [
112 'description' => 'The block_content entity ID.',
117 'layout_entity_type' => [
118 'description' => 'The entity type of the parent entity.',
119 'type' => 'varchar_ascii',
120 'length' => EntityTypeInterface::ID_MAX_LENGTH,
124 'layout_entity_id' => [
125 'description' => 'The ID of the parent entity.',
126 'type' => 'varchar_ascii',
132 'primary key' => ['block_content_id'],
134 'type_id' => ['layout_entity_type', 'layout_entity_id'],
137 Database::getConnection()->schema()->createTable('inline_block_usage', $inline_block_usage);