View source
<?php
namespace Drupal\layout_builder\Form;
use Drupal\Component\Utility\Html;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Ajax\AjaxFormHelperTrait;
use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Form\BaseFormIdInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\SubformState;
use Drupal\Core\Plugin\Context\ContextRepositoryInterface;
use Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Core\Plugin\PluginFormFactoryInterface;
use Drupal\Core\Plugin\PluginWithFormsInterface;
use Drupal\layout_builder\Context\LayoutBuilderContextTrait;
use Drupal\layout_builder\Controller\LayoutRebuildTrait;
use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
use Drupal\layout_builder\SectionComponent;
use Drupal\layout_builder\SectionStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class ConfigureBlockFormBase extends FormBase implements BaseFormIdInterface {
use AjaxFormHelperTrait;
use ContextAwarePluginAssignmentTrait;
use LayoutBuilderContextTrait;
use LayoutRebuildTrait;
protected $block;
protected $layoutTempstoreRepository;
protected $blockManager;
protected $uuidGenerator;
protected $pluginFormFactory;
protected $delta;
protected $region;
protected $uuid;
protected $sectionStorage;
public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, ContextRepositoryInterface $context_repository, BlockManagerInterface $block_manager, UuidInterface $uuid, PluginFormFactoryInterface $plugin_form_manager) {
$this->layoutTempstoreRepository = $layout_tempstore_repository;
$this->contextRepository = $context_repository;
$this->blockManager = $block_manager;
$this->uuidGenerator = $uuid;
$this->pluginFormFactory = $plugin_form_manager;
}
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('plugin_form.factory'));
}
public function getBaseFormId() {
return 'layout_builder_configure_block';
}
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
->getPopulatedContexts($section_storage));
$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';
$form['#id'] = Html::getId($form_state
->getBuildInfo()['form_id']);
}
$form['#attached']['drupalSettings']['path']['currentPathIsAdmin'] = TRUE;
return $form;
}
protected abstract function submitLabel();
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);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$subform_state = SubformState::createForSubform($form['settings'], $form, $form_state);
$this
->getPluginForm($this->block)
->submitConfigurationForm($form, $subform_state);
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());
}
protected function successfulAjaxSubmit(array $form, FormStateInterface $form_state) {
return $this
->rebuildAndClose($this->sectionStorage);
}
protected function getPluginForm(BlockPluginInterface $block) {
if ($block instanceof PluginWithFormsInterface) {
return $this->pluginFormFactory
->createInstance($block, 'configure');
}
return $block;
}
public function getSectionStorage() {
return $this->sectionStorage;
}
public function getCurrentSection() {
return $this->sectionStorage
->getSection($this->delta);
}
public function getCurrentComponent() {
return $this
->getCurrentSection()
->getComponent($this->uuid);
}
}