diff --git a/src/bundle/DependencyInjection/Configuration.php b/src/bundle/DependencyInjection/Configuration.php index a19b819d..e4fa52b2 100644 --- a/src/bundle/DependencyInjection/Configuration.php +++ b/src/bundle/DependencyInjection/Configuration.php @@ -10,7 +10,9 @@ use Ibexa\Bundle\Core\DependencyInjection\Configuration\SiteAccessAware\Configuration as SiteAccessConfiguration; use Symfony\Component\Config\Definition\Builder\NodeBuilder; +use Symfony\Component\Config\Definition\Builder\NodeDefinition; use Symfony\Component\Config\Definition\Builder\TreeBuilder; +use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; class Configuration extends SiteAccessConfiguration { @@ -28,17 +30,63 @@ public function getConfigTreeBuilder() $rootNode = $treeBuilder->getRootNode(); $sections = $rootNode->children(); + $this + ->addEnabledAttributeTypesSection($sections); $this ->addCustomTagsSection($sections); $this ->addCustomStylesSection($sections); $this - ->addAlloyEditorSection($sections) + ->addAlloyEditorSection($sections); + + $this + ->validateAttributeTypes($rootNode) ->end(); return $treeBuilder; } + private function addEnabledAttributeTypesSection(NodeBuilder $richTextNode): NodeBuilder + { + return $richTextNode + ->arrayNode('enabled_attribute_types') + ->defaultValue(self::CUSTOM_TAG_ATTRIBUTE_TYPES) + ->scalarPrototype() + ->end() + ->end() + ; + } + + private function validateAttributeTypes(NodeDefinition $rootNode): NodeDefinition + { + return $rootNode + ->validate() + ->ifTrue(static function (array $v): bool { + if (!isset($v['enabled_attribute_types']) || !isset($v['custom_tags'])) { + return false; + } + $enabledTypes = $v['enabled_attribute_types']; + foreach ($v['custom_tags'] as $tagIdentifier => $tag) { + foreach ($tag['attributes'] as $attribute) { + if (!in_array($attribute['type'], $enabledTypes, true)) { + throw new InvalidConfigurationException( + sprintf( + 'The value "%s" is not allowed for path "ibexa_fieldtype_richtext.custom_tags.%s.attributes.campaign.type". Allowed values: %s', + $attribute['type'], + $tagIdentifier, + implode(', ', array_map(static fn ($type): string => "\"$type\"", $enabledTypes)) + ) + ); + } + } + } + + return false; + }) + ->then(static fn (array $v): array => $v) + ->end(); + } + /** * Define RichText Custom Tags Semantic Configuration. * @@ -102,9 +150,8 @@ static function ($v) { ->thenInvalid('List of choices is supported by choices type only.') ->end() ->children() - ->enumNode('type') + ->scalarNode('type') ->isRequired() - ->values(static::CUSTOM_TAG_ATTRIBUTE_TYPES) ->end() ->booleanNode('required') ->defaultFalse() diff --git a/tests/bundle/DependencyInjection/Configuration/ConfigurationTest.php b/tests/bundle/DependencyInjection/Configuration/ConfigurationTest.php index edfb11b7..7c49c8a4 100644 --- a/tests/bundle/DependencyInjection/Configuration/ConfigurationTest.php +++ b/tests/bundle/DependencyInjection/Configuration/ConfigurationTest.php @@ -73,6 +73,7 @@ public function providerForTestProcessingConfiguration(): array [ 'custom_tags' => [], 'custom_styles' => [], + 'enabled_attribute_types' => ['number', 'string', 'boolean', 'choice', 'link'], ], ], 'Alloy editor configs from multiple sources' => [ @@ -111,6 +112,7 @@ public function providerForTestProcessingConfiguration(): array ], 'custom_tags' => [], 'custom_styles' => [], + 'enabled_attribute_types' => ['number', 'string', 'boolean', 'choice', 'link'], ], ], ]; diff --git a/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/000-attribute-type-number.yaml b/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/000-attribute-type-number.yaml index 84c07e39..6ae7f80a 100644 --- a/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/000-attribute-type-number.yaml +++ b/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/000-attribute-type-number.yaml @@ -9,3 +9,9 @@ custom_tags: default_value: 360 is_inline: false custom_styles: [] +enabled_attribute_types: + - 'number' + - 'string' + - 'boolean' + - 'choice' + - 'link' diff --git a/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/001-attribute-type-string.yaml b/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/001-attribute-type-string.yaml index 4c03964a..a6422979 100644 --- a/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/001-attribute-type-string.yaml +++ b/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/001-attribute-type-string.yaml @@ -9,3 +9,9 @@ custom_tags: default_value: 'abc' is_inline: false custom_styles: [] +enabled_attribute_types: + - 'number' + - 'string' + - 'boolean' + - 'choice' + - 'link' diff --git a/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/002-attribute-type-boolean.yaml b/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/002-attribute-type-boolean.yaml index 4a4507c8..ca1254f0 100644 --- a/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/002-attribute-type-boolean.yaml +++ b/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/002-attribute-type-boolean.yaml @@ -9,3 +9,9 @@ custom_tags: default_value: ~ is_inline: false custom_styles: [] +enabled_attribute_types: + - 'number' + - 'string' + - 'boolean' + - 'choice' + - 'link' diff --git a/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/003-attribute-type-choice.yaml b/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/003-attribute-type-choice.yaml index 366c7692..e41bfec1 100644 --- a/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/003-attribute-type-choice.yaml +++ b/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/003-attribute-type-choice.yaml @@ -10,3 +10,9 @@ custom_tags: choices: ['choice-1', 'choice-2'] is_inline: false custom_styles: [] +enabled_attribute_types: + - 'number' + - 'string' + - 'boolean' + - 'choice' + - 'link' diff --git a/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/004-attribute-type-link.yaml b/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/004-attribute-type-link.yaml index a1b1f904..9195a030 100644 --- a/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/004-attribute-type-link.yaml +++ b/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/004-attribute-type-link.yaml @@ -9,3 +9,9 @@ custom_tags: default_value: ~ is_inline: false custom_styles: [] +enabled_attribute_types: + - 'number' + - 'string' + - 'boolean' + - 'choice' + - 'link'