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