diff --git a/core/modules/layout_builder/src/Form/AddBlockForm.php b/core/modules/layout_builder/src/Form/AddBlockForm.php index 83effd6226..14d9646f9e 100644 --- a/core/modules/layout_builder/src/Form/AddBlockForm.php +++ b/core/modules/layout_builder/src/Form/AddBlockForm.php @@ -2,8 +2,9 @@ namespace Drupal\layout_builder\Form; -use Drupal\layout_builder\Section; +use Drupal\Core\Form\FormStateInterface; use Drupal\layout_builder\SectionComponent; +use Drupal\layout_builder\SectionStorageInterface; /** * Provides a form to add a block. @@ -27,10 +28,28 @@ protected function submitLabel() { } /** - * {@inheritdoc} + * Builds the form for the block. + * + * @param array $form + * An associative array containing the structure of the form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + * @param \Drupal\layout_builder\SectionStorageInterface $section_storage + * The section storage being configured. + * @param int $delta + * The delta of the section. + * @param string $region + * The region of the block. + * @param string|null $plugin_id + * The plugin ID of the block to add. + * + * @return array + * The form array. */ - protected function submitBlock(Section $section, $region, $uuid, array $configuration) { - $section->appendComponent(new SectionComponent($uuid, $region, $configuration)); + public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL, $delta = NULL, $region = NULL, $plugin_id = NULL) { + $component = new SectionComponent($this->uuidGenerator->generate(), $region, ['id' => $plugin_id]); + $section_storage->getSection($delta)->appendComponent($component); + return $this->doBuildForm($form, $form_state, $section_storage, $delta, $component); } } diff --git a/core/modules/layout_builder/src/Form/ConfigureBlockFormBase.php b/core/modules/layout_builder/src/Form/ConfigureBlockFormBase.php index b75e88f083..e1103e9e44 100644 --- a/core/modules/layout_builder/src/Form/ConfigureBlockFormBase.php +++ b/core/modules/layout_builder/src/Form/ConfigureBlockFormBase.php @@ -17,7 +17,7 @@ use Drupal\layout_builder\Context\LayoutBuilderContextTrait; use Drupal\layout_builder\Controller\LayoutRebuildTrait; use Drupal\layout_builder\LayoutTempstoreRepositoryInterface; -use Drupal\layout_builder\Section; +use Drupal\layout_builder\SectionComponent; use Drupal\layout_builder\SectionStorageInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -59,7 +59,7 @@ * * @var \Drupal\Component\Uuid\UuidInterface */ - protected $uuid; + protected $uuidGenerator; /** * The plugin form manager. @@ -82,6 +82,13 @@ */ protected $region; + /** + * The UUID of the component. + * + * @var string + */ + protected $uuid; + /** * The section storage. * @@ -109,7 +116,7 @@ public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore $this->layoutTempstoreRepository = $layout_tempstore_repository; $this->contextRepository = $context_repository; $this->blockManager = $block_manager; - $this->uuid = $uuid; + $this->uuidGenerator = $uuid; $this->classResolver = $class_resolver; $this->pluginFormFactory = $plugin_form_manager; } @@ -128,25 +135,6 @@ public static function create(ContainerInterface $container) { ); } - /** - * Prepares the block plugin based on the block ID. - * - * @param string $block_id - * Either a block ID, or the plugin ID used to create a new block. - * @param array $configuration - * The block configuration. - * - * @return \Drupal\Core\Block\BlockPluginInterface - * The block plugin. - */ - protected function prepareBlock($block_id, array $configuration) { - if (!isset($configuration['uuid'])) { - $configuration['uuid'] = $this->uuid->generate(); - } - - return $this->blockManager->createInstance($block_id, $configuration); - } - /** * Builds the form for the block. * @@ -158,21 +146,17 @@ protected function prepareBlock($block_id, array $configuration) { * The section storage being configured. * @param int $delta * The delta of the section. - * @param string $region - * The region of the block. - * @param string|null $plugin_id - * The plugin ID of the block to add. - * @param array $configuration - * (optional) The array of configuration for the block. + * @param \Drupal\layout_builder\SectionComponent $component + * The section component containing the block. * * @return array * The form array. */ - public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL, $delta = NULL, $region = NULL, $plugin_id = NULL, array $configuration = []) { + public function doBuildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL, $delta = NULL, SectionComponent $component = NULL) { $this->sectionStorage = $section_storage; $this->delta = $delta; - $this->region = $region; - $this->block = $this->prepareBlock($plugin_id, $configuration); + $this->uuid = $component->getUuid(); + $this->block = $component->getPlugin(); $form_state->setTemporaryValue('gathered_contexts', $this->getAvailableContexts($section_storage)); @@ -204,20 +188,6 @@ public function buildForm(array $form, FormStateInterface $form_state, SectionSt */ abstract protected function submitLabel(); - /** - * Handles the submission of a block. - * - * @param \Drupal\layout_builder\Section $section - * The layout section. - * @param string $region - * The region name. - * @param string $uuid - * The UUID of the block. - * @param array $configuration - * The block configuration. - */ - abstract protected function submitBlock(Section $section, $region, $uuid, array $configuration); - /** * {@inheritdoc} */ @@ -242,7 +212,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $configuration = $this->block->getConfiguration(); $section = $this->sectionStorage->getSection($this->delta); - $this->submitBlock($section, $this->region, $configuration['uuid'], $configuration); + $section->getComponent($this->uuid)->setConfiguration($configuration); $this->layoutTempstoreRepository->set($this->sectionStorage); $form_state->setRedirectUrl($this->sectionStorage->getLayoutBuilderUrl()); diff --git a/core/modules/layout_builder/src/Form/UpdateBlockForm.php b/core/modules/layout_builder/src/Form/UpdateBlockForm.php index afca0d25c9..c00b406eb2 100644 --- a/core/modules/layout_builder/src/Form/UpdateBlockForm.php +++ b/core/modules/layout_builder/src/Form/UpdateBlockForm.php @@ -2,9 +2,7 @@ namespace Drupal\layout_builder\Form; -use Drupal\Component\Plugin\ConfigurablePluginInterface; use Drupal\Core\Form\FormStateInterface; -use Drupal\layout_builder\Section; use Drupal\layout_builder\SectionStorageInterface; /** @@ -36,19 +34,13 @@ public function getFormId() { * The region of the block. * @param string $uuid * The UUID of the block being updated. - * @param array $configuration - * (optional) The array of configuration for the block. * * @return array * The form array. */ - public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL, $delta = NULL, $region = NULL, $uuid = NULL, array $configuration = []) { - $plugin = $section_storage->getSection($delta)->getComponent($uuid)->getPlugin(); - if ($plugin instanceof ConfigurablePluginInterface) { - $configuration = $plugin->getConfiguration(); - } - - return parent::buildForm($form, $form_state, $section_storage, $delta, $region, $plugin->getPluginId(), $configuration); + public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL, $delta = NULL, $region = NULL, $uuid = NULL) { + $component = $section_storage->getSection($delta)->getComponent($uuid); + return $this->doBuildForm($form, $form_state, $section_storage, $delta, $component); } /** @@ -58,11 +50,4 @@ protected function submitLabel() { return $this->t('Update'); } - /** - * {@inheritdoc} - */ - protected function submitBlock(Section $section, $region, $uuid, array $configuration) { - $section->getComponent($uuid)->setConfiguration($configuration); - } - }