|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright (C) 2025 SYSTOPIA GmbH |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 2 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +declare(strict_types=1); |
| 21 | + |
| 22 | +namespace Drupal\json_forms\Form\Control; |
| 23 | + |
| 24 | +use Assert\Assertion; |
| 25 | +use Drupal\Core\Form\FormStateInterface; |
| 26 | +use Drupal\json_forms\Form\AbstractConcreteFormArrayFactory; |
| 27 | +use Drupal\json_forms\Form\Control\Callbacks\HtmlCallbacks; |
| 28 | +use Drupal\json_forms\Form\Control\Callbacks\StringValueCallback; |
| 29 | +use Drupal\json_forms\Form\Control\Util\BasicFormPropertiesFactory; |
| 30 | +use Drupal\json_forms\Form\FormArrayFactoryInterface; |
| 31 | +use Drupal\json_forms\Form\Util\FormCallbackRegistrator; |
| 32 | +use Drupal\json_forms\Form\Util\FormValidationUtil; |
| 33 | +use Drupal\json_forms\JsonForms\Definition\Control\ControlDefinition; |
| 34 | +use Drupal\json_forms\JsonForms\Definition\Control\StringControlDefinition; |
| 35 | +use Drupal\json_forms\JsonForms\Definition\DefinitionInterface; |
| 36 | + |
| 37 | +class HtmlArrayFactory extends AbstractConcreteFormArrayFactory { |
| 38 | + |
| 39 | + public static function getPriority(): int { |
| 40 | + return StringArrayFactory::getPriority() + 1; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * {@inheritDoc} |
| 45 | + */ |
| 46 | + public function createFormArray( |
| 47 | + DefinitionInterface $definition, |
| 48 | + FormStateInterface $formState, |
| 49 | + FormArrayFactoryInterface $formArrayFactory |
| 50 | + ): array { |
| 51 | + Assertion::isInstanceOf($definition, ControlDefinition::class); |
| 52 | + /** @var \Drupal\json_forms\JsonForms\Definition\Control\ControlDefinition $definition */ |
| 53 | + $definition = StringControlDefinition::fromDefinition($definition); |
| 54 | + /** @var \Drupal\json_forms\JsonForms\Definition\Control\StringControlDefinition $definition */ |
| 55 | + |
| 56 | + $form = [ |
| 57 | + '#type' => 'text_format', |
| 58 | + '#value_callback' => [StringValueCallback::class, 'convert'], |
| 59 | + ] + BasicFormPropertiesFactory::createFieldProperties($definition, $formState); |
| 60 | + |
| 61 | + if (NULL !== $definition->getMaxLength()) { |
| 62 | + $form['#maxlength'] = $definition->getMaxLength(); |
| 63 | + } |
| 64 | + |
| 65 | + if (NULL !== $definition->getPattern()) { |
| 66 | + $form['#pattern'] = $definition->getPattern(); |
| 67 | + } |
| 68 | + |
| 69 | + /** @var list<int|string> $elementKey */ |
| 70 | + $elementKey = $form['#parents']; |
| 71 | + FormValidationUtil::addFormErrorMapping($formState, $elementKey, array_merge($elementKey, ['value'])); |
| 72 | + FormValidationUtil::addKeepFormErrorElementKey($formState, array_merge($elementKey, ['format'])); |
| 73 | + |
| 74 | + FormCallbackRegistrator::registerPreSchemaValidationCallback( |
| 75 | + $formState, |
| 76 | + $definition->getFullScope(), |
| 77 | + [HtmlCallbacks::class, 'convertValue'], |
| 78 | + $form['#parents'], |
| 79 | + ); |
| 80 | + |
| 81 | + return $form; |
| 82 | + } |
| 83 | + |
| 84 | + public function supportsDefinition(DefinitionInterface $definition): bool { |
| 85 | + return $definition instanceof ControlDefinition && 'string' === $definition->getType() |
| 86 | + && 'text/html' === $definition->getContentMediaType(); |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments