diff --git a/core/modules/layout_builder/src/LayoutSectionItemInterface.php b/core/modules/layout_builder/src/LayoutSectionItemInterface.php new file mode 100644 index 0000000..1a39cde --- /dev/null +++ b/core/modules/layout_builder/src/LayoutSectionItemInterface.php @@ -0,0 +1,15 @@ +account = $account; $this->layoutPluginManager = $layoutPluginManager; $this->blockManager = $blockManager; @@ -81,7 +81,18 @@ public function __construct(AccountInterface $account, LayoutPluginManager $layo * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { - return new static($container->get('current_user'), $container->get('plugin.manager.core.layout'), $container->get('plugin.manager.block'), $plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings']); + return new static( + $container->get('current_user'), + $container->get('plugin.manager.core.layout'), + $container->get('plugin.manager.block'), + $plugin_id, + $plugin_definition, + $configuration['field_definition'], + $configuration['settings'], + $configuration['label'], + $configuration['view_mode'], + $configuration['third_party_settings'] + ); } /** @@ -91,26 +102,26 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $elements = []; foreach ($items as $delta => $item) { - $elements[$delta] = $this->viewValue($item); + $elements[$delta] = $this->buildSection($item); } return $elements; } /** - * Build the render array for the field item. + * Builds the render array for the layout section. * - * @param FieldItemInterface $item - * The field item. + * @param \Drupal\layout_builder\LayoutSectionItemInterface $item + * The layout section item. * * @return array + * A render array for the field item. */ - protected function viewValue(FieldItemInterface $item) { + protected function buildSection(LayoutSectionItemInterface $item) { /** @var \Drupal\Core\Layout\LayoutInterface $layout */ - $layout = $this->layoutPluginManager->createInstance($item->get('layout')->getValue()); - $section = $item->get('section')->getValue(); + $layout = $this->layoutPluginManager->createInstance($item->layout); $regions = []; - foreach ($section as $region => $blocks) { + foreach ($item->section as $region => $blocks) { foreach ($blocks as $uuid => $configuration) { /** @var \Drupal\Core\Block\BlockPluginInterface $block */ $block = $this->blockManager->createInstance($configuration['plugin_id'], $configuration); diff --git a/core/modules/layout_builder/src/Plugin/Field/FieldType/LayoutSection.php b/core/modules/layout_builder/src/Plugin/Field/FieldType/LayoutSectionItem.php similarity index 92% rename from core/modules/layout_builder/src/Plugin/Field/FieldType/LayoutSection.php rename to core/modules/layout_builder/src/Plugin/Field/FieldType/LayoutSectionItem.php index 9da1937..8cf547d 100644 --- a/core/modules/layout_builder/src/Plugin/Field/FieldType/LayoutSection.php +++ b/core/modules/layout_builder/src/Plugin/Field/FieldType/LayoutSectionItem.php @@ -8,6 +8,7 @@ use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Core\TypedData\DataDefinition; use Drupal\Core\TypedData\MapDataDefinition; +use Drupal\layout_builder\LayoutSectionItemInterface; /** * Plugin implementation of the 'layout_section' field type. @@ -19,7 +20,7 @@ * default_formatter = "layout_section" * ) */ -class LayoutSection extends FieldItemBase { +class LayoutSectionItem extends FieldItemBase implements LayoutSectionItemInterface { /** * {@inheritdoc} @@ -38,7 +39,7 @@ public static function propertyDefinitions(FieldStorageDefinitionInterface $fiel } /** - * @inheritDoc + * {@inheritdoc} */ public static function mainPropertyName() { return 'section'; @@ -60,7 +61,7 @@ public static function schema(FieldStorageDefinitionInterface $field_definition) 'size' => 'normal', 'serialize' => TRUE, ], - ] + ], ]; return $schema; diff --git a/core/modules/layout_builder/tests/src/Kernel/LayoutSectionFormatterTest.php b/core/modules/layout_builder/tests/src/Kernel/LayoutSectionFormatterTest.php new file mode 100644 index 0000000..fe540d0 --- /dev/null +++ b/core/modules/layout_builder/tests/src/Kernel/LayoutSectionFormatterTest.php @@ -0,0 +1,120 @@ +installConfig(['field']); + $this->installEntitySchema('entity_test'); + + $entity_type = 'entity_test'; + $bundle = $entity_type; + $this->fieldName = Unicode::strtolower($this->randomMachineName()); + + $field_storage = FieldStorageConfig::create([ + 'field_name' => $this->fieldName, + 'entity_type' => $entity_type, + 'type' => 'layout_section', + ]); + $field_storage->save(); + + $instance = FieldConfig::create([ + 'field_storage' => $field_storage, + 'bundle' => $bundle, + 'label' => $this->randomMachineName(), + ]); + $instance->save(); + + $this->display = EntityViewDisplay::create([ + 'targetEntityType' => $entity_type, + 'bundle' => $bundle, + 'mode' => 'default', + 'status' => TRUE, + ]); + $this->display->setComponent($this->fieldName, [ + 'type' => 'layout_section', + 'settings' => [], + ]); + $this->display->save(); + } + + /** + * Tests layout_section formatter output. + * + * @dataProvider providerTestLayoutSectionFormatter + */ + public function testLayoutSectionFormatter($layout, $section, $expected_selector, $expected_content) { + $entity = EntityTest::create([]); + $entity->{$this->fieldName}->layout = $layout; + $entity->{$this->fieldName}->section = $section; + + // Build and render the content. + $content = $this->display->build($entity); + $this->render($content); + + // Find the given selector. + $element = $this->cssSelect($expected_selector); + $this->assertNotEmpty($element); + + // Find the given content. + $this->assertRaw($expected_content); + } + + /** + * Provides test data to ::testLayoutSectionFormatter(). + */ + public function providerTestLayoutSectionFormatter() { + $data = []; + $data[] = [ + 'layout_onecol', + [ + 'content' => [ + 'this_should_be_a_UUID' => [ + 'plugin_id' => 'system_powered_by_block', + ], + ], + ], + '.layout--onecol', + 'Powered by', + ]; + return $data; + } + +}