layoutTempstoreRepository = $layout_tempstore_repository; $this->contextRepository = $context_repository; $this->blockManager = $block_manager; $this->uuidGenerator = $uuid; $this->classResolver = $class_resolver; $this->pluginFormFactory = $plugin_form_manager; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { return new static( $container->get('layout_builder.tempstore_repository'), $container->get('context.repository'), $container->get('plugin.manager.block'), $container->get('uuid'), $container->get('class_resolver'), $container->get('plugin_form.factory') ); } /** * 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 \Drupal\layout_builder\SectionComponent $component * The section component containing the block. * * @return array * The form array. */ 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->uuid = $component->getUuid(); $this->block = $component->getPlugin(); $form_state->setTemporaryValue('gathered_contexts', $this->getAvailableContexts($section_storage)); // @todo Remove once https://www.drupal.org/node/2268787 is resolved. $form_state->set('block_theme', $this->config('system.theme')->get('default')); $form['#tree'] = TRUE; $form['settings'] = []; $subform_state = SubformState::createForSubform($form['settings'], $form, $form_state); $form['settings'] = $this->getPluginForm($this->block)->buildConfigurationForm($form['settings'], $subform_state); $form['actions']['submit'] = [ '#type' => 'submit', '#value' => $this->submitLabel(), '#button_type' => 'primary', ]; if ($this->isAjax()) { $form['actions']['submit']['#ajax']['callback'] = '::ajaxSubmit'; } return $form; } /** * Returns the label for the submit button. * * @return string * Submit label. */ abstract protected function submitLabel(); /** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { $subform_state = SubformState::createForSubform($form['settings'], $form, $form_state); $this->getPluginForm($this->block)->validateConfigurationForm($form['settings'], $subform_state); } /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { // Call the plugin submit handler. $subform_state = SubformState::createForSubform($form['settings'], $form, $form_state); $this->getPluginForm($this->block)->submitConfigurationForm($form, $subform_state); // If this block is context-aware, set the context mapping. if ($this->block instanceof ContextAwarePluginInterface) { $this->block->setContextMapping($subform_state->getValue('context_mapping', [])); } $configuration = $this->block->getConfiguration(); $section = $this->sectionStorage->getSection($this->delta); $section->getComponent($this->uuid)->setConfiguration($configuration); $this->layoutTempstoreRepository->set($this->sectionStorage); $form_state->setRedirectUrl($this->sectionStorage->getLayoutBuilderUrl()); } /** * {@inheritdoc} */ protected function successfulAjaxSubmit(array $form, FormStateInterface $form_state) { return $this->rebuildAndClose($this->sectionStorage); } /** * Retrieves the plugin form for a given block. * * @param \Drupal\Core\Block\BlockPluginInterface $block * The block plugin. * * @return \Drupal\Core\Plugin\PluginFormInterface * The plugin form for the block. */ protected function getPluginForm(BlockPluginInterface $block) { if ($block instanceof PluginWithFormsInterface) { return $this->pluginFormFactory->createInstance($block, 'configure'); } return $block; } }