diff --git a/core/modules/layout_builder/src/Controller/MoveBlockController.php b/core/modules/layout_builder/src/Controller/MoveBlockController.php index 654ddb1d9a..994054f5a4 100644 --- a/core/modules/layout_builder/src/Controller/MoveBlockController.php +++ b/core/modules/layout_builder/src/Controller/MoveBlockController.php @@ -88,7 +88,7 @@ public function build(EntityInterface $entity, $delta_from, $delta_to, $region_f // block to the front. $component->setRegion($region_to); if (isset($preceding_block_uuid)) { - $section->insertAfterComponent($component, $preceding_block_uuid); + $section->insertAfterComponent($preceding_block_uuid, $component); } else { $section->appendComponent($component); diff --git a/core/modules/layout_builder/src/Field/LayoutSectionItemList.php b/core/modules/layout_builder/src/Field/LayoutSectionItemList.php index 5ce678b398..0a3715dab4 100644 --- a/core/modules/layout_builder/src/Field/LayoutSectionItemList.php +++ b/core/modules/layout_builder/src/Field/LayoutSectionItemList.php @@ -3,7 +3,6 @@ namespace Drupal\layout_builder\Field; use Drupal\Core\Field\FieldItemList; -use Drupal\layout_builder\Plugin\Field\FieldType\LayoutSectionItem; use Drupal\layout_builder\Section; /** @@ -30,8 +29,7 @@ public function addItem($index, $value) { */ public function insertSection($delta, Section $section) { if ($this->get($delta)) { - $item = $this->createItem($delta); - $this->updateItemFromSection($item, $section); + $item = $this->createItem($delta)->updateFromSection($section); $start = array_slice($this->list, 0, $delta); $end = array_slice($this->list, $delta); @@ -51,7 +49,7 @@ public function insertSection($delta, Section $section) { public function appendItem($value = NULL) { $item = parent::appendItem(); if ($value) { - $this->updateItemFromSection($item, $this->convertSectionValue($value)); + $item->updateFromSection($this->convertSectionValue($value)); } return $item; } @@ -83,8 +81,7 @@ protected function convertSectionValue($value) { * {@inheritdoc} */ public function appendSection(Section $section) { - $item = $this->appendItem(); - $this->updateItemFromSection($item, $section); + $this->appendItem()->updateFromSection($section); return $this; } @@ -111,18 +108,6 @@ public function getSection($delta) { return $item->getSection(); } - /** - * {@inheritdoc} - */ - public function updateSection($delta, Section $new_section) { - if (!$item = $this->get($delta)) { - throw new \OutOfBoundsException(sprintf('Invalid delta "%s" for the "%s" entity', $delta, $this->getEntity()->label())); - } - - $this->updateItemFromSection($item, $new_section); - return $this; - } - /** * {@inheritdoc} */ @@ -131,19 +116,4 @@ public function removeSection($delta) { return $this; } - /** - * Updates the item value based on the domain object. - * - * @param \Drupal\layout_builder\Plugin\Field\FieldType\LayoutSectionItem $item - * The layout section item. - * @param \Drupal\layout_builder\Section $section - * The layout section. - * - * @return $this - */ - protected function updateItemFromSection(LayoutSectionItem $item, Section $section) { - $item->updateFromSection($section); - return $this; - } - } diff --git a/core/modules/layout_builder/src/Form/ConfigureBlockFormBase.php b/core/modules/layout_builder/src/Form/ConfigureBlockFormBase.php index 7356ef4ccd..9cebad21ae 100644 --- a/core/modules/layout_builder/src/Form/ConfigureBlockFormBase.php +++ b/core/modules/layout_builder/src/Form/ConfigureBlockFormBase.php @@ -249,7 +249,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { /** @var \Drupal\layout_builder\Field\LayoutSectionItemInterface $field */ $field = $this->entity->layout_builder__layout->get($this->delta); $section = $field->getSection(); - $this->submitBlock($section, $this->region, $configuration['uuid'], ['block' => $configuration]); + $this->submitBlock($section, $this->region, $configuration['uuid'], $configuration); $field->updateFromSection($section); $this->layoutTempstoreRepository->set($this->entity); diff --git a/core/modules/layout_builder/src/LayoutSectionBuilder.php b/core/modules/layout_builder/src/LayoutSectionBuilder.php index ddaf3222d3..ae21d2ba82 100644 --- a/core/modules/layout_builder/src/LayoutSectionBuilder.php +++ b/core/modules/layout_builder/src/LayoutSectionBuilder.php @@ -96,9 +96,10 @@ public function buildSectionFromLayout(LayoutInterface $layout, array $component $regions = array_fill_keys($layout->getPluginDefinition()->getRegionNames(), []); foreach ($components as $component) { - if ($block_output = $this->buildBlock($component, $cacheability)) { + if ($block_output = $component->build()) { $regions[$component->getRegion()][$component->getUuid()] = $block_output; } + $cacheability->addCacheableDependency($component); } $result = $layout->build($regions); @@ -124,55 +125,4 @@ public function buildSection($layout_id, array $layout_settings, array $componen return $this->buildSectionFromLayout($layout, $components); } - /** - * Builds the render array for a given block. - * - * @param \Drupal\layout_builder\SectionComponent $component - * The component. - * @param \Drupal\Core\Cache\CacheableMetadata $cacheability - * The cacheability metadata. - * - * @return array|null - * The render array representing this block, if accessible. NULL otherwise. - */ - protected function buildBlock(SectionComponent $component, CacheableMetadata $cacheability) { - $block = $this->getBlock($component); - - $access = $block->access($this->account, TRUE); - $cacheability->addCacheableDependency($access); - - $block_output = NULL; - if ($access->isAllowed()) { - $block_output = [ - '#theme' => 'block', - '#configuration' => $block->getConfiguration(), - '#plugin_id' => $block->getPluginId(), - '#base_plugin_id' => $block->getBaseId(), - '#derivative_plugin_id' => $block->getDerivativeId(), - '#weight' => $component->getWeight(), - 'content' => $block->build(), - ]; - $cacheability->addCacheableDependency($block); - } - return $block_output; - } - - /** - * Gets a block instance. - * - * @param \Drupal\layout_builder\SectionComponent $component - * The section component. - * - * @return \Drupal\Core\Block\BlockPluginInterface - * The block instance. - */ - protected function getBlock(SectionComponent $component) { - $block = $component->getPlugin(); - if ($block instanceof ContextAwarePluginInterface) { - $contexts = $this->contextRepository->getRuntimeContexts(array_values($block->getContextMapping())); - $this->contextHandler->applyContextMapping($block, $contexts); - } - return $block; - } - } diff --git a/core/modules/layout_builder/src/Section.php b/core/modules/layout_builder/src/Section.php index afe93dc650..b7ed02751d 100644 --- a/core/modules/layout_builder/src/Section.php +++ b/core/modules/layout_builder/src/Section.php @@ -8,8 +8,7 @@ * A section consists of three parts: * - A layout plugin ID * - An array of settings for the layout plugin - * - A multi-dimensional array keyed by block UUID, containing information like - * region, weight, and block configuration. + * - An array of components that can be rendered. * * @internal * Layout Builder is currently experimental and should only be leveraged by @@ -50,10 +49,6 @@ class Section { * (optional) The components. */ public function __construct($layout_id, array $layout_settings = [], array $components = []) { - if (!is_string($layout_id)) { - throw new \InvalidArgumentException(sprintf('Layout plugin ID of type "%s" must be a string', gettype($layout_id))); - } - $this->layoutId = $layout_id; $this->layoutSettings = $layout_settings; foreach ($components as $component) { @@ -134,7 +129,7 @@ public function getComponents() { */ public function getComponent($uuid) { if (!isset($this->components[$uuid])) { - throw new \InvalidArgumentException('Invalid UUID'); + throw new \InvalidArgumentException(sprintf('Invalid UUID "%s"', $uuid)); } return $this->components[$uuid]; @@ -219,22 +214,22 @@ protected function getComponentsByRegion($region) { /** * Inserts a component after a specified existing component. * - * @param \Drupal\layout_builder\SectionComponent $component - * The component being inserted. * @param string $preceding_uuid * The UUID of the existing component to insert after. + * @param \Drupal\layout_builder\SectionComponent $component + * The component being inserted. * * @return $this * * @throws \InvalidArgumentException - * Thrown when the expected region does not exist. + * Thrown when the expected UUID does not exist. */ - public function insertAfterComponent(SectionComponent $component, $preceding_uuid) { + public function insertAfterComponent($preceding_uuid, SectionComponent $component) { // Find the delta of the specified UUID. $uuids = array_keys($this->getComponentsByRegion($component->getRegion())); $delta = array_search($preceding_uuid, $uuids, TRUE); if ($delta === FALSE) { - throw new \InvalidArgumentException('Invalid preceding UUID'); + throw new \InvalidArgumentException(sprintf('Invalid preceding UUID "%s"', $preceding_uuid)); } return $this->insertComponent($delta + 1, $component); } diff --git a/core/modules/layout_builder/src/SectionComponent.php b/core/modules/layout_builder/src/SectionComponent.php index 9494edaae3..aef8368284 100644 --- a/core/modules/layout_builder/src/SectionComponent.php +++ b/core/modules/layout_builder/src/SectionComponent.php @@ -3,11 +3,25 @@ namespace Drupal\layout_builder; use Drupal\Component\Plugin\Exception\PluginException; +use Drupal\Core\Block\BlockPluginInterface; +use Drupal\Core\Cache\RefinableCacheableDependencyInterface; +use Drupal\Core\Cache\RefinableCacheableDependencyTrait; +use Drupal\Core\Plugin\ContextAwarePluginInterface; /** * Provides a value object for a section component. + * + * A component represents the smallest part of a layout. Components wrap a + * renderable plugin, currently using \Drupal\Core\Block\BlockPluginInterface. + * + * @internal + * Layout Builder is currently experimental and should only be leveraged by + * experimental modules and development releases of contributed modules. + * See https://www.drupal.org/core/experimental for more information. */ -class SectionComponent { +class SectionComponent implements RefinableCacheableDependencyInterface { + + use RefinableCacheableDependencyTrait; /** * The UUID of the component. @@ -101,6 +115,36 @@ public function toArray() { ]; } + /** + * Builds and returns the renderable array for this component. + * + * @return array + * A renderable array representing the content of the component. + */ + public function build() { + $output = []; + + $plugin = $this->getPlugin(); + if ($plugin instanceof BlockPluginInterface) { + $access = $plugin->access($this->currentUser(), TRUE); + $this->addCacheableDependency($access); + + if ($access->isAllowed()) { + $this->addCacheableDependency($plugin); + $output = [ + '#theme' => 'block', + '#configuration' => $plugin->getConfiguration(), + '#plugin_id' => $plugin->getPluginId(), + '#base_plugin_id' => $plugin->getBaseId(), + '#derivative_plugin_id' => $plugin->getDerivativeId(), + '#weight' => $this->getWeight(), + 'content' => $plugin->build(), + ]; + } + } + return $output; + } + /** * Gets any arbitrary property. * @@ -244,11 +288,16 @@ public function getUuid() { /** * Gets the plugin for this component. * - * @return \Drupal\Core\Block\BlockPluginInterface + * @return \Drupal\Component\Plugin\PluginInspectionInterface * The plugin. */ public function getPlugin() { - return $this->pluginManager()->createInstance($this->getPluginId(), $this->getConfiguration()); + $plugin = $this->pluginManager()->createInstance($this->getPluginId(), $this->getConfiguration()); + if ($plugin instanceof ContextAwarePluginInterface) { + $contexts = $this->contextRepository()->getRuntimeContexts(array_values($plugin->getContextMapping())); + $this->contextHandler()->applyContextMapping($plugin, $contexts); + } + return $plugin; } /** @@ -261,4 +310,34 @@ protected function pluginManager() { return \Drupal::service('plugin.manager.block'); } + /** + * Wraps the context repository. + * + * @return \Drupal\Core\Plugin\Context\ContextRepositoryInterface + * The context repository. + */ + protected function contextRepository() { + return \Drupal::service('context.repository'); + } + + /** + * Wraps the context handler. + * + * @return \Drupal\Core\Plugin\Context\ContextHandlerInterface + * The context handler. + */ + protected function contextHandler() { + return \Drupal::service('context.handler'); + } + + /** + * Wraps the current user. + * + * @return \Drupal\Core\Session\AccountInterface + * The current user. + */ + protected function currentUser() { + return \Drupal::currentUser(); + } + } diff --git a/core/modules/layout_builder/src/SectionStorageInterface.php b/core/modules/layout_builder/src/SectionStorageInterface.php index 88f8fa4046..b7acab7e97 100644 --- a/core/modules/layout_builder/src/SectionStorageInterface.php +++ b/core/modules/layout_builder/src/SectionStorageInterface.php @@ -31,18 +31,6 @@ public function getSections(); */ public function getSection($delta); - /** - * Updates the stored value based on the domain object. - * - * @param int $delta - * The delta of the section. - * @param \Drupal\layout_builder\Section $new_section - * The layout section. - * - * @return $this - */ - public function updateSection($delta, Section $new_section); - /** * Appends a new section to the end of the list. * diff --git a/core/modules/layout_builder/tests/src/Kernel/SectionStorageTestBase.php b/core/modules/layout_builder/tests/src/Kernel/SectionStorageTestBase.php index 25c34e7554..4100b2513d 100644 --- a/core/modules/layout_builder/tests/src/Kernel/SectionStorageTestBase.php +++ b/core/modules/layout_builder/tests/src/Kernel/SectionStorageTestBase.php @@ -97,28 +97,6 @@ public function testGetSectionInvalidDelta() { $this->sectionStorage->getSection(2); } - /** - * @covers ::updateSection - */ - public function testUpdateSection() { - $section = $this->sectionStorage->getSection(0); - - $section->setLayoutSettings(['setting_1' => 'bar']); - // The values are not yet updated on the entity. - $this->assertEquals([], $this->sectionStorage->getSection(0)->getLayoutSettings()); - - $this->sectionStorage->updateSection(0, $section); - $this->assertEquals(['setting_1' => 'bar'], $this->sectionStorage->getSection(0)->getLayoutSettings()); - } - - /** - * @covers ::updateSection - */ - public function testUpdateSectionInvalidDelta() { - $this->setExpectedException(\OutOfBoundsException::class, 'Invalid delta "2" for the "The test entity"'); - $this->sectionStorage->updateSection(2, new Section('some_layout')); - } - /** * @covers ::insertSection */ diff --git a/core/modules/layout_builder/tests/src/Unit/LayoutSectionBuilderTest.php b/core/modules/layout_builder/tests/src/Unit/LayoutSectionBuilderTest.php index b8968629bd..680906d824 100644 --- a/core/modules/layout_builder/tests/src/Unit/LayoutSectionBuilderTest.php +++ b/core/modules/layout_builder/tests/src/Unit/LayoutSectionBuilderTest.php @@ -92,7 +92,10 @@ protected function setUp() { $this->layoutPluginManager->createInstance('layout_onecol', [])->willReturn($this->layout->reveal()); $container = new ContainerBuilder(); + $container->set('current_user', $this->account->reveal()); $container->set('plugin.manager.block', $this->blockManager->reveal()); + $container->set('context.handler', $this->contextHandler->reveal()); + $container->set('context.repository', $this->contextRepository->reveal()); \Drupal::setContainer($container); } @@ -190,7 +193,6 @@ public function testBuildSectionEmpty() { /** * @covers ::buildSection - * @covers ::getBlock */ public function testContextAwareBlock() { $render_array = [ @@ -241,7 +243,6 @@ public function testContextAwareBlock() { /** * @covers ::buildSection - * @covers ::getBlock */ public function testBuildSectionMissingPluginId() { $this->setExpectedException(PluginException::class, 'No plugin ID specified for component with "some_uuid" UUID'); diff --git a/core/modules/layout_builder/tests/src/Unit/SectionTest.php b/core/modules/layout_builder/tests/src/Unit/SectionTest.php index 2a3707084d..1803ee0f7f 100644 --- a/core/modules/layout_builder/tests/src/Unit/SectionTest.php +++ b/core/modules/layout_builder/tests/src/Unit/SectionTest.php @@ -32,14 +32,6 @@ protected function setUp() { ]); } - /** - * @covers ::__construct - */ - public function testConstructInvalidLayoutId() { - $this->setExpectedException(\InvalidArgumentException::class, 'Layout plugin ID of type "NULL" must be a string'); - new Section(NULL); - } - /** * @covers ::__construct * @covers ::setComponent @@ -59,7 +51,7 @@ public function testGetComponents() { * @covers ::getComponent */ public function testGetComponentInvalidUuid() { - $this->setExpectedException(\InvalidArgumentException::class, 'Invalid UUID'); + $this->setExpectedException(\InvalidArgumentException::class, 'Invalid UUID "invalid-uuid"'); $this->section->getComponent('invalid-uuid'); } @@ -114,7 +106,7 @@ public function testInsertAfterComponent() { 'new-uuid' => new SectionComponent('new-uuid', 'ordered-region', [], 3), ]; - $this->section->insertAfterComponent(new SectionComponent('new-uuid', 'ordered-region'), 'first-uuid'); + $this->section->insertAfterComponent('first-uuid', new SectionComponent('new-uuid', 'ordered-region')); $this->assertComponents($expected, $this->section); } @@ -122,16 +114,16 @@ public function testInsertAfterComponent() { * @covers ::insertAfterComponent */ public function testInsertAfterComponentValidUuidRegionMismatch() { - $this->setExpectedException(\InvalidArgumentException::class, 'Invalid preceding UUID'); - $this->section->insertAfterComponent(new SectionComponent('new-uuid', 'ordered-region'), 'existing-uuid'); + $this->setExpectedException(\InvalidArgumentException::class, 'Invalid preceding UUID "existing-uuid"'); + $this->section->insertAfterComponent('existing-uuid', new SectionComponent('new-uuid', 'ordered-region')); } /** * @covers ::insertAfterComponent */ public function testInsertAfterComponentInvalidUuid() { - $this->setExpectedException(\InvalidArgumentException::class, 'Invalid preceding UUID'); - $this->section->insertAfterComponent(new SectionComponent('new-uuid', 'ordered-region'), 'invalid-uuid'); + $this->setExpectedException(\InvalidArgumentException::class, 'Invalid preceding UUID "invalid-uuid"'); + $this->section->insertAfterComponent('invalid-uuid', new SectionComponent('new-uuid', 'ordered-region')); } /**