Skip to content
Snippets Groups Projects
Commit 2a6e00e8 authored by christian.wiedemann's avatar christian.wiedemann
Browse files

Resolve #3395953 "2.0.x add new"

parent 2d703620
No related branches found
No related tags found
1 merge request!48Resolve #3395953 "2.0.x add new"
......@@ -42,15 +42,14 @@ class ComponentBlock extends BlockBase {
public function blockForm($form, FormStateInterface $form_state) {
$form = parent::blockForm($form, $form_state);
$configuration = $this->getConfiguration();
$form = $this->buildComponentsForm($form_state, $configuration, NULL, TRUE, TRUE);
return $form;
return $this->buildComponentsForm($form_state, $configuration, NULL, TRUE, TRUE);
}
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
$this->submitComponentForm($form, $form_state);
$this->submitComponentForm($form['settings'], $form_state);
parent::blockSubmit($form, $form_state);
}
......
......@@ -42,10 +42,19 @@ class ComponentLayout extends LayoutDefault implements PluginFormInterface {
* {@inheritdoc}
*/
public function build(array $regions) {
$build = parent::build($regions);
$configuration = $this->getConfiguration();
$build = $this->buildComponentData($configuration, $this->getPluginId());
$data = $this->buildComponentData($configuration, $this->getPluginId());
$build['#layout'] = $this;
$build['#slots'] = $regions;
$build['#props'] = $data['#props'] ?? [];
$build['#type'] = $data['#type'];
$build['#component'] = $data['#component'];
foreach ($this->getPluginDefinition()->getRegionNames() as $region_name) {
if (array_key_exists($region_name, $regions)) {
$build['#slots'][$region_name] = $regions[$region_name];
}
}
return $build;
}
......@@ -63,7 +72,7 @@ class ComponentLayout extends LayoutDefault implements PluginFormInterface {
$form = parent::buildConfigurationForm($form, $form_state);
$configuration = $this->getConfiguration();
$component_id = $this->getPluginDefinition()->id();
return $this->buildComponentsForm($form_state->getCompleteFormState(), $configuration, $component_id, FALSE, TRUE);
return $this->buildComponentsForm($form_state, $configuration, $component_id, FALSE, TRUE);
}
/**
......
......@@ -43,7 +43,7 @@ class ComponentStyle extends StylePluginBase {
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$configuration = $this->options ?: $this->defineOptions();
return $this->buildComponentsForm($form_state->getCompleteFormState(), $configuration, NULL, TRUE, TRUE);
return $this->buildComponentsForm($form_state, $configuration, NULL, TRUE, TRUE);
}
/**
......
......@@ -7,12 +7,13 @@ namespace Drupal\ui_patterns\Form;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\SubformState;
use Drupal\Core\Render\Element;
use Drupal\sdc\Plugin\Component;
use Drupal\ui_patterns\SourcePluginManager;
/**
*
* Component form builder trait.
*/
trait ComponentFormBuilderTrait {
......@@ -37,7 +38,13 @@ trait ComponentFormBuilderTrait {
}
/**
* Returns the context for source plugins
*
* Overwrite this method to provide context for
* module implementations.
*
* @return \Drupal\Core\Plugin\Context\Context[]
* The provided source context.
*/
protected function getComponentSourceContexts(): array {
return [];
......@@ -49,12 +56,14 @@ trait ComponentFormBuilderTrait {
final public function getComponent($component_id): Component {
/** @var \Drupal\sdc\ComponentPluginManager $sdc_plugin_manager */
$sdc_plugin_manager = \Drupal::service("plugin.manager.sdc");
$component = $sdc_plugin_manager->find($component_id);
return $component;
return $sdc_plugin_manager->find($component_id);
}
/**
* Return the form api storage subkey
*
* Overwrite this method and provide a specifc subkey
* if your form handles multiple component form builders.
*/
protected function getComponentStorageSubKey(): string {
return 'default';
......@@ -195,6 +204,7 @@ trait ComponentFormBuilderTrait {
$render_slots = TRUE,
$render_props = TRUE
): array {
$form_state = $form_state instanceof SubformState ? $form_state->getCompleteFormState(): $form_state;
$configuration = $configuration['ui_patterns'] ?? [];
// TBD Make this id more flexible.
$wrapper_id = 'ui-patterns-component';
......@@ -243,7 +253,7 @@ trait ComponentFormBuilderTrait {
FormStateInterface $form_state
): array {
$configuration = $this->getComponentFormState('configuration', $form_state);
$variant_id = NULL;
$variant_id = $configuration['variant_id'] ?? NULL;
$trigger_element = $form_state->getTriggeringElement();
if ($form_state->isRebuilding(
) && isset($trigger_element['#ui_patterns']['id']) && $trigger_element['#ui_patterns']['id'] === 'variant_selector') {
......@@ -337,7 +347,7 @@ trait ComponentFormBuilderTrait {
): array {
$components = \Drupal::service("plugin.manager.sdc")->getDefinitions();
$options = [];
// $options['_empty'] = $this->t('Empty');
$options['_empty'] = $this->t('Empty');
foreach ($components as $component_id => $component) {
$options[$component_id] = $component["name"];
}
......@@ -380,6 +390,7 @@ trait ComponentFormBuilderTrait {
"#type" => "select",
"#title" => t("Variant"),
"#options" => $options,
'#ui_patterns' => ['id' => 'variant_selector'],
'#default_value' => $default_variant_id,
'#ajax' => [
'callback' => [$this, 'buildComponentVariantSelectorFormChangeAjax'],
......@@ -394,7 +405,7 @@ trait ComponentFormBuilderTrait {
*
* Call this to save the form to given configuration array.
*/
protected function submitComponentForm(
final protected function submitComponentForm(
$form,
FormStateInterface $form_state
) {
......@@ -661,7 +672,7 @@ trait ComponentFormBuilderTrait {
}
/**
*
* Returns the source plugin manager.
*/
private function sourcePluginManager(): SourcePluginManager {
/** @var \Drupal\ui_patterns\SourcePluginManager $source_plugin_manager */
......@@ -678,7 +689,7 @@ trait ComponentFormBuilderTrait {
Component $component,
&$configuration
): array {
$contexts = $this->getComponentSourceContexts($form_state);
$contexts = $this->getComponentSourceContexts();
$form = [];
$build_sources = [];
$source_plugin_manager = $this->sourcePluginManager();
......@@ -810,7 +821,7 @@ trait ComponentFormBuilderTrait {
}
/**
*
* Build component data provided to the SDC element.
*/
final protected function buildComponentData($configuration, $initial_component_id = NULL) {
$configuration = $configuration['ui_patterns'] ?? [];
......
......@@ -7,6 +7,7 @@ namespace Drupal\ui_patterns;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\SubformState;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\Context\ContextRepositoryInterface;
use Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait;
......@@ -117,7 +118,7 @@ abstract class SourcePluginBase extends PluginBase implements
array &$form,
FormStateInterface $form_state
) {
$complete_state = $form_state->getCompleteFormState();
$complete_state = $form_state instanceof SubformState ? $form_state->getCompleteFormState(): $form_state;
$parents = $form['#parents'];
$parents[] = 'value';
$value = $complete_state->getValue($parents);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment