Commit cc4cf852 authored by Mateu Aguiló Bosch's avatar Mateu Aguiló Bosch Committed by Mateu Aguiló Bosch
Browse files

Issue #3298570 by e0ipso: Validate form form submission from user

parent b6d1bd98
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -13,6 +13,6 @@
    "require": {
        "php": ">=8.1",
        "ext-json": "*",
        "e0ipso/schema-forms": "^1"
        "e0ipso/schema-forms": "^2"
    }
}
+18 −20
Original line number Diff line number Diff line
@@ -215,20 +215,19 @@ class ComponentBlock extends BlockBase implements ContainerFactoryPluginInterfac
    $component = $this->getComponent();
    // Here is where we derive the form from the metadata.
    $metadata = $component->getMetadata();
    $component_schema = $metadata->getSchemas()['props'] ?? [];
    // Encode & decode so we transform an associative array to an stdClass
    $schemas = $metadata->getSchemas();
    $component_schema = $schemas['props'] ?? [];
    // Encode & decode, so we transform an associative array to an stdClass
    // recursively.
    $schema = json_decode(json_encode($component_schema));
    $form['component']['data'] = $this->formGenerator->transform($schema);
    $form['component']['data']['#weight'] = 2;
    $current_data = $this->getConfiguration()['component']['data'] ?? [];
    $form['component']['data'] = $form['component']['data'] ?? [];
    $element = &$form['component']['data'];
    $element = $this->formGenerator->transform($schema);
    $element['#weight'] = 2;
    $configuration = $this->getConfiguration();
    $current_data = $configuration['component']['data'] ?? [];
    foreach ($current_data as $key => $value) {
      NestedArray::setValue($form, [
        'component',
        'data',
        $key,
        '#default_value',
      ], $value);
      NestedArray::setValue($element, [$key, '#default_value'], $value);
    }
    // Add the component data to the form via AJAX.
    $form['component']['twig_blocks_wrapper'] = [
@@ -243,7 +242,7 @@ class ComponentBlock extends BlockBase implements ContainerFactoryPluginInterfac
  /**
   * Render API callback: builds the component data elements.
   */
  public function componentDataProcessCallback(array &$element, FormStateInterface $form_state, array &$complete_form) {
  public function componentDataProcessCallback(array &$element, FormStateInterface $form_state): array {
    $component = NULL;
    try {
      $component = $this->getComponent();
@@ -268,21 +267,20 @@ class ComponentBlock extends BlockBase implements ContainerFactoryPluginInterfac
        ...$carry,
        $name => ['ui:widget' => 'textarea'],
      ], []);
      $element['twig_blocks_wrapper']['twig_blocks'] = $this->formGenerator
        ->transform($schema, new Context($ui_hints));
      $twig_blocks = &$element['twig_blocks_wrapper']['twig_blocks'];
      $context = new Context(['ui_hints' => $ui_hints]);
      $twig_blocks = $this->formGenerator->transform($schema, $context);
      $current_data = $this->getConfiguration()['component']['twig_blocks'] ?? [];
      foreach ($current_data as $key => $value) {
        NestedArray::setValue($element['twig_blocks_wrapper']['twig_blocks'], [
          $key,
          '#default_value',
        ], $value);
        $keys = [$key, '#default_value'];
        NestedArray::setValue($twig_blocks, $keys, $value);
      }
      $element['twig_blocks_wrapper']['twig_blocks']['#parents'] = array_merge(
      $twig_blocks['#parents'] = array_merge(
        $element['#parents'],
        ['twig_blocks']
      );
      // Store the array parents for our element so that we can retrieve the
      // formatter settings in our AJAX callback.
      // twig block settings in our AJAX callback.
      $form_state->set('twig_block_array_parents', $element['#array_parents']);
    }
    $element['#weight'] = 5;