diff --git a/core/modules/layout_builder/src/Controller/LayoutBuilderController.php b/core/modules/layout_builder/src/Controller/LayoutBuilderController.php index a4163a40a8..ed579a9f2a 100644 --- a/core/modules/layout_builder/src/Controller/LayoutBuilderController.php +++ b/core/modules/layout_builder/src/Controller/LayoutBuilderController.php @@ -193,8 +193,9 @@ protected function buildAdministrativeSection(LayoutSectionItemInterface $item, $entity_type_id = $entity->getEntityTypeId(); $entity_id = $entity->id(); - $layout = $this->layoutManager->createInstance($item->layout, $item->layout_settings); - $build = $this->builder->buildSectionFromLayout($layout, $item->section); + $section = $item->getSection(); + $layout = $this->layoutManager->createInstance($section->getLayoutId(), $section->getLayoutSettings()); + $build = $this->builder->buildSectionFromLayout($layout, $section->getComponents()); $layout_definition = $layout->getPluginDefinition(); foreach ($layout_definition->getRegions() as $region => $info) { diff --git a/core/modules/layout_builder/src/Field/LayoutSectionItemInterface.php b/core/modules/layout_builder/src/Field/LayoutSectionItemInterface.php index e630e40c00..0f19aaaf29 100644 --- a/core/modules/layout_builder/src/Field/LayoutSectionItemInterface.php +++ b/core/modules/layout_builder/src/Field/LayoutSectionItemInterface.php @@ -13,9 +13,7 @@ * experimental modules and development releases of contributed modules. * See https://www.drupal.org/core/experimental for more information. * - * @property string layout - * @property array[] layout_settings - * @property array[] section + * @property \Drupal\layout_builder\Section section * * @todo Remove in https://www.drupal.org/project/drupal/issues/2928450. */ diff --git a/core/modules/layout_builder/src/Form/ConfigureSectionForm.php b/core/modules/layout_builder/src/Form/ConfigureSectionForm.php index 17913237d5..a46f778e6f 100644 --- a/core/modules/layout_builder/src/Form/ConfigureSectionForm.php +++ b/core/modules/layout_builder/src/Form/ConfigureSectionForm.php @@ -14,6 +14,7 @@ use Drupal\Core\Plugin\PluginWithFormsInterface; use Drupal\layout_builder\Controller\LayoutRebuildTrait; use Drupal\layout_builder\LayoutTempstoreRepositoryInterface; +use Drupal\layout_builder\Section; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -121,14 +122,15 @@ public function buildForm(array $form, FormStateInterface $form_state, EntityInt $this->delta = $delta; $this->isUpdate = is_null($plugin_id); - $configuration = []; if ($this->isUpdate) { /** @var \Drupal\layout_builder\Field\LayoutSectionItemInterface $field */ $field = $this->entity->layout_builder__layout->get($this->delta); - $plugin_id = $field->layout; - $configuration = $field->layout_settings; + $section = $field->getSection(); } - $this->layout = $this->layoutManager->createInstance($plugin_id, $configuration); + else { + $section = new Section($plugin_id); + } + $this->layout = $section->getLayout(); $form['#tree'] = TRUE; $form['layout_settings'] = []; @@ -170,8 +172,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $field_list = $this->entity->layout_builder__layout; if ($this->isUpdate) { $field = $field_list->get($this->delta); - $field->layout = $plugin_id; - $field->layout_settings = $configuration; + $field->section = new Section($plugin_id, $configuration); } else { $field_list->addItem($this->delta, [ diff --git a/core/modules/layout_builder/src/LayoutSectionBuilder.php b/core/modules/layout_builder/src/LayoutSectionBuilder.php index ae21d2ba82..25c9e758d6 100644 --- a/core/modules/layout_builder/src/LayoutSectionBuilder.php +++ b/core/modules/layout_builder/src/LayoutSectionBuilder.php @@ -8,7 +8,6 @@ use Drupal\Core\Layout\LayoutPluginManagerInterface; use Drupal\Core\Plugin\Context\ContextHandlerInterface; use Drupal\Core\Plugin\Context\ContextRepositoryInterface; -use Drupal\Core\Plugin\ContextAwarePluginInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; diff --git a/core/modules/layout_builder/src/Plugin/DataType/SectionData.php b/core/modules/layout_builder/src/Plugin/DataType/SectionData.php new file mode 100644 index 0000000000..be05cb61f6 --- /dev/null +++ b/core/modules/layout_builder/src/Plugin/DataType/SectionData.php @@ -0,0 +1,36 @@ + $item) { - $elements[$delta] = $this->builder->buildSection($item->layout, $item->layout_settings, $item->section); + /** @var \Drupal\layout_builder\SectionStorageInterface $items */ + foreach ($items->getSections() as $delta => $section) { + $elements[$delta] = $this->builder->buildSection($section->getLayoutId(), $section->getLayoutSettings(), $section->getComponents()); } return $elements; diff --git a/core/modules/layout_builder/src/Plugin/Field/FieldType/LayoutSectionItem.php b/core/modules/layout_builder/src/Plugin/Field/FieldType/LayoutSectionItem.php index 83d82f00df..82f22d7503 100644 --- a/core/modules/layout_builder/src/Plugin/Field/FieldType/LayoutSectionItem.php +++ b/core/modules/layout_builder/src/Plugin/Field/FieldType/LayoutSectionItem.php @@ -7,7 +7,6 @@ use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Core\TypedData\DataDefinition; -use Drupal\Core\TypedData\MapDataDefinition; use Drupal\layout_builder\Field\LayoutSectionItemInterface; use Drupal\layout_builder\Section; @@ -26,9 +25,7 @@ * cardinality = \Drupal\Core\Field\FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED * ) * - * @property string layout - * @property array[] layout_settings - * @property array[] section + * @property \Drupal\layout_builder\Section section */ class LayoutSectionItem extends FieldItemBase implements LayoutSectionItemInterface { @@ -36,15 +33,7 @@ class LayoutSectionItem extends FieldItemBase implements LayoutSectionItemInterf * {@inheritdoc} */ public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) { - // Prevent early t() calls by using the TranslatableMarkup. - $properties['layout'] = DataDefinition::create('string') - ->setLabel(new TranslatableMarkup('Layout')) - ->setSetting('case_sensitive', FALSE) - ->setRequired(TRUE); - $properties['layout_settings'] = MapDataDefinition::create('map') - ->setLabel(new TranslatableMarkup('Layout Settings')) - ->setRequired(FALSE); - $properties['section'] = MapDataDefinition::create('map') + $properties['section'] = DataDefinition::create('layout_section') ->setLabel(new TranslatableMarkup('Layout Section')) ->setRequired(FALSE); @@ -77,17 +66,6 @@ public static function mainPropertyName() { public static function schema(FieldStorageDefinitionInterface $field_definition) { $schema = [ 'columns' => [ - 'layout' => [ - 'type' => 'varchar', - 'length' => '255', - 'binary' => FALSE, - ], - 'layout_settings' => [ - 'type' => 'blob', - 'size' => 'normal', - // @todo Address in https://www.drupal.org/node/2914503. - 'serialize' => TRUE, - ], 'section' => [ 'type' => 'blob', 'size' => 'normal', @@ -104,10 +82,8 @@ public static function schema(FieldStorageDefinitionInterface $field_definition) * {@inheritdoc} */ public static function generateSampleValue(FieldDefinitionInterface $field_definition) { - $values['layout'] = 'layout_onecol'; - $values['layout_settings'] = []; // @todo Expand this in https://www.drupal.org/node/2912331. - $values['section'] = []; + $values['section'] = new Section('layout_onecol'); return $values; } @@ -115,23 +91,21 @@ public static function generateSampleValue(FieldDefinitionInterface $field_defin * {@inheritdoc} */ public function isEmpty() { - return empty($this->layout); + return empty($this->section); } /** * {@inheritdoc} */ public function getSection() { - return new Section($this->layout, $this->layout_settings, $this->section); + return $this->section; } /** * {@inheritdoc} */ public function updateFromSection(Section $section) { - $this->layout = $section->getLayoutId(); - $this->layout_settings = $section->getLayoutSettings(); - $this->section = $section->getComponents(); + $this->section = $section; return $this; } diff --git a/core/modules/layout_builder/src/Section.php b/core/modules/layout_builder/src/Section.php index b7ed02751d..1a2360dff6 100644 --- a/core/modules/layout_builder/src/Section.php +++ b/core/modules/layout_builder/src/Section.php @@ -29,7 +29,7 @@ class Section { * * @var array */ - protected $layoutSettings; + protected $layoutSettings = []; /** * An array of components, keyed by UUID. diff --git a/core/modules/layout_builder/src/SectionComponent.php b/core/modules/layout_builder/src/SectionComponent.php index aef8368284..71d76b7108 100644 --- a/core/modules/layout_builder/src/SectionComponent.php +++ b/core/modules/layout_builder/src/SectionComponent.php @@ -80,41 +80,6 @@ public function __construct($uuid, $region, array $configuration = [], $weight = $this->additional = $additional; } - /** - * Creates a new SectionComponent from an array. - * - * @param array $component - * An array representing a component. - * - * @return static - */ - public static function fromArray(array $component) { - $component += [ - 'uuid' => NULL, - 'region' => NULL, - 'configuration' => [], - 'weight' => NULL, - 'additional' => [], - ]; - return new SectionComponent($component['uuid'], $component['region'], $component['configuration'], $component['weight'], $component['additional']); - } - - /** - * Returns an array representation of a component. - * - * @return array - * An array representing a component. - */ - public function toArray() { - return [ - 'configuration' => $this->configuration, - 'region' => $this->region, - 'uuid' => $this->uuid, - 'weight' => $this->weight, - 'additional' => $this->additional, - ]; - } - /** * Builds and returns the renderable array for this component. * diff --git a/core/modules/layout_builder/tests/src/Functional/LayoutSectionTest.php b/core/modules/layout_builder/tests/src/Functional/LayoutSectionTest.php index e59af76d1c..fcf0326add 100644 --- a/core/modules/layout_builder/tests/src/Functional/LayoutSectionTest.php +++ b/core/modules/layout_builder/tests/src/Functional/LayoutSectionTest.php @@ -4,6 +4,7 @@ use Drupal\Core\Entity\Entity\EntityViewDisplay; use Drupal\language\Entity\ConfigurableLanguage; +use Drupal\layout_builder\Section; use Drupal\layout_builder\SectionComponent; use Drupal\Tests\BrowserTestBase; @@ -57,15 +58,14 @@ public function providerTestLayoutSectionFormatter() { $data['block_with_context'] = [ [ [ - 'layout' => 'layout_onecol', - 'section' => [ + 'section' => new Section('layout_onecol', [], [ 'baz' => new SectionComponent('baz', 'content', [ 'id' => 'test_context_aware', 'context_mapping' => [ 'user' => '@user.current_user_context:current_user', ], ], 0), - ], + ]), ], ], [ @@ -83,12 +83,11 @@ public function providerTestLayoutSectionFormatter() { $data['single_section_single_block'] = [ [ [ - 'layout' => 'layout_onecol', - 'section' => [ + 'section' => new Section('layout_onecol', [], [ 'baz' => new SectionComponent('baz', 'content', [ 'id' => 'system_powered_by_block', ], 0), - ], + ]), ], ], '.layout--onecol', @@ -100,16 +99,14 @@ public function providerTestLayoutSectionFormatter() { $data['multiple_sections'] = [ [ [ - 'layout' => 'layout_onecol', - 'section' => [ + 'section' => new Section('layout_onecol', [], [ 'baz' => new SectionComponent('baz', 'content', [ 'id' => 'system_powered_by_block', ], 0), - ], + ]), ], [ - 'layout' => 'layout_twocol', - 'section' => [ + 'section' => new Section('layout_twocol', [], [ 'foo' => new SectionComponent('foo', 'first', [ 'id' => 'test_block_instantiation', 'display_message' => 'foo text', @@ -118,7 +115,7 @@ public function providerTestLayoutSectionFormatter() { 'id' => 'test_block_instantiation', 'display_message' => 'bar text', ], 0), - ], + ]), ], ], [ @@ -158,12 +155,11 @@ public function testLayoutSectionFormatter($layout_data, $expected_selector, $ex public function testLayoutSectionFormatterAccess() { $node = $this->createSectionNode([ [ - 'layout' => 'layout_onecol', - 'section' => [ + 'section' => new Section('layout_onecol', [], [ 'baz' => new SectionComponent('baz', 'content', [ 'id' => 'test_access', ], 0), - ], + ]), ], ]); @@ -193,20 +189,18 @@ public function testMultilingualLayoutSectionFormatter() { $entity = $this->createSectionNode([ [ - 'layout' => 'layout_onecol', - 'section' => [ + 'section' => new Section('layout_onecol', [], [ 'baz' => new SectionComponent('baz', 'content', [ 'id' => 'system_powered_by_block', ], 0), - ], + ]), ], ]); $entity->addTranslation('es', [ 'title' => 'Translated node title', $this->fieldName => [ [ - 'layout' => 'layout_twocol', - 'section' => [ + 'section' => new Section('layout_twocol', [], [ 'foo' => new SectionComponent('foo', 'first', [ 'id' => 'test_block_instantiation', 'display_message' => 'foo text', @@ -215,7 +209,7 @@ public function testMultilingualLayoutSectionFormatter() { 'id' => 'test_block_instantiation', 'display_message' => 'bar text', ], 0), - ], + ]), ], ], ]); diff --git a/core/modules/layout_builder/tests/src/Kernel/SectionStorageTestBase.php b/core/modules/layout_builder/tests/src/Kernel/SectionStorageTestBase.php index 4100b2513d..b8e8b3462c 100644 --- a/core/modules/layout_builder/tests/src/Kernel/SectionStorageTestBase.php +++ b/core/modules/layout_builder/tests/src/Kernel/SectionStorageTestBase.php @@ -37,20 +37,14 @@ protected function setUp() { $section_data = [ [ - 'layout' => 'layout_test_plugin', - 'layout_settings' => [], - 'section' => [ + 'section' => new Section('layout_test_plugin', [], [ 'first-uuid' => new SectionComponent('first-uuid', 'content'), - ], + ]), ], [ - 'layout' => 'layout_test_plugin', - 'layout_settings' => [ - 'setting_1' => 'bar', - ], - 'section' => [ + 'section' => new Section('layout_test_plugin', ['setting_1' => 'bar'], [ 'second-uuid' => new SectionComponent('second-uuid', 'content'), - ], + ]), ], ]; $this->sectionStorage = $this->getEntity($section_data);