Skip to content

Commit 7b87d80

Browse files
committed
add property_normalizer.default_type_mapping config
1 parent 4019d69 commit 7b87d80

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

docs/90_Headless.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ This normalizer will transform gallery relations to thumbnail data arrays
152152
### ImageEditableNormalizer
153153
This normalizer will transform the inline image editable to a thumbnail data array
154154

155-
## Custom normalizer
155+
### Custom normalizer
156156
First, you need to add your normalizer:
157157

158158
```yaml
@@ -179,8 +179,8 @@ class MyNormalizer implements PropertyNormalizerInterface
179179
}
180180
}
181181
```
182-
Then, assign them:
183182

183+
Then, assign them:
184184
```yaml
185185
toolbox:
186186
areas:
@@ -198,4 +198,17 @@ toolbox:
198198
config: ~
199199
additional_property_normalizer:
200200
myAdditionalProperty: App\Normalizer\MyNormalizer # normalize your config property, added in your "headlessAction() method
201-
```
201+
```
202+
203+
### Default normalizer definition
204+
If you want to apply a normalizer to every element of type `checkbox`, you're able to define it globally:
205+
206+
```yaml
207+
toolbox:
208+
property_normalizer:
209+
default_type_mapping:
210+
checkbox: App\Normalizer\MyNormalizer
211+
```
212+
213+
> It is still possible, to override the default mapping by using the `property_normalizer` in your element config
214+
> since those will be processed with the highest priority!

src/DependencyInjection/Configuration.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function addContextNode(ArrayNodeDefinition $rootNode): void
4848
->append($this->buildAreaBlockConfiguration())
4949
->append($this->buildThemeConfiguration())
5050
->append($this->buildDataAttributeConfiguration())
51+
->append($this->buildPropertyNormalizerDefaultsConfiguration())
5152
->end()
5253
->end()
5354
->end()
@@ -68,6 +69,7 @@ public function addRootNode(ArrayNodeDefinition $rootNode): void
6869
->append($this->buildAreaBlockConfiguration())
6970
->append($this->buildThemeConfiguration())
7071
->append($this->buildDataAttributeConfiguration())
72+
->append($this->buildPropertyNormalizerDefaultsConfiguration())
7173
->end();
7274
}
7375

@@ -316,6 +318,23 @@ protected function buildDataAttributeConfiguration(): ArrayNodeDefinition
316318
return $treeBuilder;
317319
}
318320

321+
protected function buildPropertyNormalizerDefaultsConfiguration(): ArrayNodeDefinition
322+
{
323+
$treeBuilder = new ArrayNodeDefinition('property_normalizer');
324+
325+
$treeBuilder
326+
->addDefaultsIfNotSet()
327+
->children()
328+
->arrayNode('default_type_mapping')
329+
->useAttributeAsKey('name')
330+
->prototype('scalar')
331+
->end()
332+
->end()
333+
->end();
334+
335+
return $treeBuilder;
336+
}
337+
319338
protected function buildAreasSection(): ArrayNodeDefinition
320339
{
321340
$treeBuilder = new ArrayNodeDefinition('areas');

src/Document/Editable/EditableWorker.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,12 @@ private function processElementData(HeadlessResponse $data, string $areaName): a
144144
$configData = $this->applyNormalizer($configElements[$configName], $configData);
145145
} elseif ($configBlockName !== 'additional_property_normalizer') {
146146
$configNode = $this->findBrickConfigNode($configName, $configElements);
147-
if ($configNode !== null && $configNode['property_normalizer'] !== null) {
148-
$configData = $this->applyNormalizer($configNode['property_normalizer'], $configData);
147+
if ($configNode !== null) {
148+
if ($configNode['property_normalizer'] !== null) {
149+
$configData = $this->applyNormalizer($configNode['property_normalizer'], $configData);
150+
} elseif (null !== $defaultNormalizer = $this->getDefaultNormalizer($configNode['type'])) {
151+
$configData = $this->applyNormalizer($defaultNormalizer, $configData);
152+
}
149153
}
150154
}
151155

@@ -181,6 +185,14 @@ private function applyNormalizer(string $normalizerName, mixed $value)
181185
return $this->normalizerRegistry->get($normalizerName)->normalize($value, $this->configManager->getContextIdentifier());
182186
}
183187

188+
private function getDefaultNormalizer(string $type): ?string
189+
{
190+
$propertyNormalizerConfig = $this->configManager->getConfig('property_normalizer');
191+
$defaultMapping = $propertyNormalizerConfig['default_type_mapping'];
192+
193+
return $defaultMapping[$type] ?? null;
194+
}
195+
184196
private function getBlockState(): BlockState
185197
{
186198
return $this->blockStateStack->getCurrentState();

0 commit comments

Comments
 (0)