Skip to content

Commit 13c758f

Browse files
authored
add property_normalizer.default_type_mapping config (#216)
* add property_normalizer.default_type_mapping config * remove invalid normalizer from accordion config * respect thumbnail config in normalizer
1 parent 4019d69 commit 13c758f

File tree

8 files changed

+75
-10
lines changed

8 files changed

+75
-10
lines changed

UPGRADE.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Upgrade Notes
22

3+
## 5.1.0
4+
- [NEW FEATURE] Add `property_normalizer.default_type_mapping` feature
5+
- [ENHANCEMENT] Respect thumbnail config in normalizer
6+
- [BUGFIX] Remove invalid normalizer from accordion config
7+
38
## 5.0.5
49
- Add `caption`, `marker` and `hotspots` to image normalizer
510

config/core_areas/accordion_config.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ toolbox:
3232
ac_name:
3333
type: input
3434
title: 'Name'
35-
property_normalizer: ToolboxBundle\Normalizer\DownloadRelationsNormalizer
3635
ac_data:
3736
type: areablock
3837
title: 'Content'

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: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,15 @@ private function processSimpleEditableData(HeadlessResponse $data): array
106106
$normalizedData = [];
107107

108108
$config = $data->getEditableConfiguration();
109+
$editableType = $data->getEditableType();
109110
$elementData = $data->getInlineConfigElementData();
110111

111112
foreach ($elementData as $configName => $configData) {
112113

113114
if (array_key_exists('property_normalizer', $config) && $config['property_normalizer'] !== null) {
114115
$configData = $this->applyNormalizer($config['property_normalizer'], $configData);
116+
} elseif (null !== $defaultNormalizer = $this->getDefaultNormalizer($editableType)) {
117+
$configData = $this->applyNormalizer($defaultNormalizer, $configData);
115118
} elseif ($configData instanceof Editable) {
116119
$configData = $configData->render();
117120
} else {
@@ -144,8 +147,12 @@ private function processElementData(HeadlessResponse $data, string $areaName): a
144147
$configData = $this->applyNormalizer($configElements[$configName], $configData);
145148
} elseif ($configBlockName !== 'additional_property_normalizer') {
146149
$configNode = $this->findBrickConfigNode($configName, $configElements);
147-
if ($configNode !== null && $configNode['property_normalizer'] !== null) {
148-
$configData = $this->applyNormalizer($configNode['property_normalizer'], $configData);
150+
if ($configNode !== null) {
151+
if ($configNode['property_normalizer'] !== null) {
152+
$configData = $this->applyNormalizer($configNode['property_normalizer'], $configData);
153+
} elseif (null !== $defaultNormalizer = $this->getDefaultNormalizer($configNode['type'])) {
154+
$configData = $this->applyNormalizer($defaultNormalizer, $configData);
155+
}
149156
}
150157
}
151158

@@ -181,6 +188,14 @@ private function applyNormalizer(string $normalizerName, mixed $value)
181188
return $this->normalizerRegistry->get($normalizerName)->normalize($value, $this->configManager->getContextIdentifier());
182189
}
183190

191+
private function getDefaultNormalizer(string $type): ?string
192+
{
193+
$propertyNormalizerConfig = $this->configManager->getConfig('property_normalizer');
194+
$defaultMapping = $propertyNormalizerConfig['default_type_mapping'];
195+
196+
return $defaultMapping[$type] ?? null;
197+
}
198+
184199
private function getBlockState(): BlockState
185200
{
186201
return $this->blockStateStack->getCurrentState();

src/Document/Editable/HeadlessEditableRenderer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,9 @@ private function processEditable(HeadlessEditableInfo $headlessEditableInfo, boo
190190

191191
$simpleHeadlessResponse = new HeadlessResponse(
192192
HeadlessResponse::TYPE_EDITABLE,
193+
$type,
193194
$headlessEditableInfo->getBrickParent(),
194-
$headlessEditableInfo->getEditableConfiguration()
195+
$headlessEditableInfo->getEditableConfiguration(),
195196
);
196197

197198
$simpleHeadlessResponse->setInlineConfigElementData([$editable->getRealName() => $editable]);

src/Document/Response/HeadlessResponse.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ class HeadlessResponse
1616

1717
public function __construct(
1818
protected string $type,
19+
protected ?string $editableType = null,
1920
protected ?string $brickParent = null,
20-
protected ?array $editableConfiguration = null
21+
protected ?array $editableConfiguration = null,
2122
) {
2223
}
2324

@@ -26,6 +27,11 @@ public function getType(): string
2627
return $this->type;
2728
}
2829

30+
public function getEditableType(): ?string
31+
{
32+
return $this->editableType;
33+
}
34+
2935
public function hasBrickParent(): bool
3036
{
3137
return $this->brickParent !== null;

src/Normalizer/ImageEditableNormalizer.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,15 @@ public function normalize(mixed $value, ?string $toolboxContextId = null): mixed
2020
return $value;
2121
}
2222

23-
$imageLightbox = $this->configManager->getImageThumbnailFromConfig('image_lightbox');
24-
$imageElement = $this->configManager->getImageThumbnailFromConfig('image_element');
23+
$config = $value->getConfig();
24+
25+
$imageLightbox = array_key_exists('lightbox_thumbnail', $config)
26+
? $config['lightbox_thumbnail']
27+
: $this->configManager->getImageThumbnailFromConfig('image_lightbox');
28+
29+
$imageElement = array_key_exists('thumbnail', $config)
30+
? $config['thumbnail']
31+
: $this->configManager->getImageThumbnailFromConfig('image_element');
2532

2633
if ($value->getThumbnailConfig()) {
2734
$imageElement = $value->getThumbnailConfig();

0 commit comments

Comments
 (0)