Skip to content
Snippets Groups Projects
Commit 98575928 authored by christian.wiedemann's avatar christian.wiedemann Committed by Mikael Meulle
Browse files

Issue #3473344 by christian.wiedemann: Validate Layout before saving the layout

parent 8b68b153
Branches
Tags
1 merge request!197Resolve #3473344 "2.0.0 beta2 validate layout"
Pipeline #281896 passed with warnings
...@@ -48,7 +48,7 @@ class ComponentLayout extends LayoutDefault implements ContainerFactoryPluginInt ...@@ -48,7 +48,7 @@ class ComponentLayout extends LayoutDefault implements ContainerFactoryPluginInt
* @param mixed $plugin_definition * @param mixed $plugin_definition
* The plugin implementation definition. * The plugin implementation definition.
* @param \Drupal\ui_patterns\Resolver\ChainContextEntityResolverInterface $chainContextEntityResolver * @param \Drupal\ui_patterns\Resolver\ChainContextEntityResolverInterface $chainContextEntityResolver
* The context helper. * The context resolver.
*/ */
public function __construct( public function __construct(
array $configuration, array $configuration,
...@@ -67,7 +67,7 @@ class ComponentLayout extends LayoutDefault implements ContainerFactoryPluginInt ...@@ -67,7 +67,7 @@ class ComponentLayout extends LayoutDefault implements ContainerFactoryPluginInt
$configuration, $configuration,
$plugin_id, $plugin_id,
$plugin_definition, $plugin_definition,
$container->get('ui_patterns.chain_context_entity_resolver') $container->get('ui_patterns.chain_context_entity_resolver'),
); );
return $plugin; return $plugin;
} }
...@@ -152,12 +152,6 @@ class ComponentLayout extends LayoutDefault implements ContainerFactoryPluginInt ...@@ -152,12 +152,6 @@ class ComponentLayout extends LayoutDefault implements ContainerFactoryPluginInt
return $form; return $form;
} }
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) : void {
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
...@@ -6,6 +6,7 @@ use Drupal\Component\Utility\NestedArray; ...@@ -6,6 +6,7 @@ use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\Context\Context; use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextDefinition; use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Render\RenderContext;
/** /**
* Provides a Component form builder element. * Provides a Component form builder element.
...@@ -116,7 +117,7 @@ class ComponentForm extends ComponentFormBase { ...@@ -116,7 +117,7 @@ class ComponentForm extends ComponentFormBase {
$component_selector_form = array_merge(self::buildComponentSelectorForm( $component_selector_form = array_merge(self::buildComponentSelectorForm(
$wrapper_id, $wrapper_id,
$component_id $component_id
), ["#ajax_url" => $element["#ajax_url"]]); ), ["#ajax_url" => $element["#ajax_url"] ?? NULL]);
$element["component_id"] = self::expandAjax($component_selector_form); $element["component_id"] = self::expandAjax($component_selector_form);
} }
self::buildComponentForm( self::buildComponentForm(
...@@ -223,7 +224,7 @@ class ComponentForm extends ComponentFormBase { ...@@ -223,7 +224,7 @@ class ComponentForm extends ComponentFormBase {
'#component_id' => $component_id, '#component_id' => $component_id,
'#source_contexts' => $element['#source_contexts'], '#source_contexts' => $element['#source_contexts'],
'#tag_filter' => $element['#tag_filter'], '#tag_filter' => $element['#tag_filter'],
'#ajax_url' => $element['#ajax_url'], '#ajax_url' => $element['#ajax_url'] ?? NULL,
'#access' => $element['#render_slots'] ?? TRUE, '#access' => $element['#render_slots'] ?? TRUE,
'#default_value' => [ '#default_value' => [
'slots' => $element['#default_value']['slots'], 'slots' => $element['#default_value']['slots'],
...@@ -241,7 +242,7 @@ class ComponentForm extends ComponentFormBase { ...@@ -241,7 +242,7 @@ class ComponentForm extends ComponentFormBase {
'#component_id' => $component_id, '#component_id' => $component_id,
'#source_contexts' => $element['#source_contexts'], '#source_contexts' => $element['#source_contexts'],
'#tag_filter' => $element['#tag_filter'], '#tag_filter' => $element['#tag_filter'],
'#ajax_url' => $element['#ajax_url'], '#ajax_url' => $element['#ajax_url'] ?? NULL,
'#access' => $element['#render_props'] ?? TRUE, '#access' => $element['#render_props'] ?? TRUE,
'#default_value' => [ '#default_value' => [
'props' => $element['#default_value']['props'], 'props' => $element['#default_value']['props'],
...@@ -266,9 +267,33 @@ class ComponentForm extends ComponentFormBase { ...@@ -266,9 +267,33 @@ class ComponentForm extends ComponentFormBase {
* Form element validation handler. * Form element validation handler.
*/ */
public static function validateFormElement(array &$element, FormStateInterface $form_state) : void { public static function validateFormElement(array &$element, FormStateInterface $form_state) : void {
$trigger_element = $form_state->getTriggeringElement();
if (isset($trigger_element['#ui_patterns']) === FALSE) { try {
$form_state->setValueForElement($element, $element['#value']); $trigger_element = $form_state->getTriggeringElement();
if (isset($trigger_element['#ui_patterns']) === FALSE) {
$build = [
'#type' => 'component',
'#component' => $element['#value']['component_id'] ?? $element['#component_id'],
'#ui_patterns' => $element['#value'],
'#source_contexts' => $element['#source_contexts'] ?? [],
];
$context = new RenderContext();
$renderer = \Drupal::service("renderer");
$renderer->executeInRenderContext($context, function () use (&$build, $renderer) {
return $renderer->render($build);
});
$form_state->setValueForElement($element, $element['#value']);
}
}
catch (\Throwable $e) {
// If a component_id is we just show the error message instead
// of highlighting the whole form.
if (!empty($element['#component_id'])) {
$form_state->setErrorByName('', $e->getMessage());
}
else {
$form_state->setError($element['component_id'], $e->getMessage());
}
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment