annotate core/modules/layout_builder/layout_builder.install @ 4:a9cd425dd02b

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