Added Entity and Entity Reference Revisions which got dropped somewhere along the...
[yaffs-website] / web / core / modules / layout_builder / layout_builder.install
1 <?php
2
3 /**
4  * @file
5  * Contains install and update functions for Layout Builder.
6  */
7
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;
13
14 /**
15  * Implements hook_install().
16  */
17 function layout_builder_install() {
18   $display_changed = FALSE;
19
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' => []];
27       $display
28         ->enableLayoutBuilder()
29         ->appendSection(new Section($field_layout['id'], $field_layout['settings']))
30         ->save();
31       $display_changed = TRUE;
32     }
33   }
34
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']);
41   }
42 }
43
44 /**
45  * Enable Layout Builder for existing entity displays.
46  */
47 function layout_builder_update_8601(&$sandbox) {
48   $config_factory = \Drupal::configFactory();
49
50   if (!isset($sandbox['count'])) {
51     $sandbox['ids'] = $config_factory->listAll('core.entity_view_display.');
52     $sandbox['count'] = count($sandbox['ids']);
53   }
54
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')) {
59       $display
60         ->set('third_party_settings.layout_builder.enabled', TRUE)
61         ->save();
62     }
63   }
64
65   $sandbox['#finished'] = empty($sandbox['ids']) ? 1 : ($sandbox['count'] - count($sandbox['ids'])) / $sandbox['count'];
66 }
67
68 /**
69  * Implements hook_schema().
70  */
71 function layout_builder_schema() {
72   $schema['inline_block_usage'] = [
73     'description' => 'Track where a block_content entity is used.',
74     'fields' => [
75       'block_content_id' => [
76         'description' => 'The block_content entity ID.',
77         'type' => 'int',
78         'unsigned' => TRUE,
79         'not null' => TRUE,
80       ],
81       'layout_entity_type' => [
82         'description' => 'The entity type of the parent entity.',
83         'type' => 'varchar_ascii',
84         'length' => EntityTypeInterface::ID_MAX_LENGTH,
85         'not null' => FALSE,
86         'default' => '',
87       ],
88       'layout_entity_id' => [
89         'description' => 'The ID of the parent entity.',
90         'type' => 'varchar_ascii',
91         'length' => 128,
92         'not null' => FALSE,
93         'default' => 0,
94       ],
95     ],
96     'primary key' => ['block_content_id'],
97     'indexes' => [
98       'type_id' => ['layout_entity_type', 'layout_entity_id'],
99     ],
100   ];
101   return $schema;
102 }
103
104 /**
105  * Create the 'inline_block_usage' table.
106  */
107 function layout_builder_update_8602() {
108   $inline_block_usage = [
109     'description' => 'Track where a block_content entity is used.',
110     'fields' => [
111       'block_content_id' => [
112         'description' => 'The block_content entity ID.',
113         'type' => 'int',
114         'unsigned' => TRUE,
115         'not null' => TRUE,
116       ],
117       'layout_entity_type' => [
118         'description' => 'The entity type of the parent entity.',
119         'type' => 'varchar_ascii',
120         'length' => EntityTypeInterface::ID_MAX_LENGTH,
121         'not null' => FALSE,
122         'default' => '',
123       ],
124       'layout_entity_id' => [
125         'description' => 'The ID of the parent entity.',
126         'type' => 'varchar_ascii',
127         'length' => 128,
128         'not null' => FALSE,
129         'default' => 0,
130       ],
131     ],
132     'primary key' => ['block_content_id'],
133     'indexes' => [
134       'type_id' => ['layout_entity_type', 'layout_entity_id'],
135     ],
136   ];
137   Database::getConnection()->schema()->createTable('inline_block_usage', $inline_block_usage);
138 }