Skip to content

Commit

Permalink
add property_normalizer.default_type_mapping config (#216)
Browse files Browse the repository at this point in the history
* add property_normalizer.default_type_mapping config
* remove invalid normalizer from accordion config
* respect thumbnail config in normalizer
  • Loading branch information
solverat authored May 30, 2024
1 parent 4019d69 commit 13c758f
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 10 deletions.
5 changes: 5 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Upgrade Notes

## 5.1.0
- [NEW FEATURE] Add `property_normalizer.default_type_mapping` feature
- [ENHANCEMENT] Respect thumbnail config in normalizer
- [BUGFIX] Remove invalid normalizer from accordion config

## 5.0.5
- Add `caption`, `marker` and `hotspots` to image normalizer

Expand Down
1 change: 0 additions & 1 deletion config/core_areas/accordion_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ toolbox:
ac_name:
type: input
title: 'Name'
property_normalizer: ToolboxBundle\Normalizer\DownloadRelationsNormalizer
ac_data:
type: areablock
title: 'Content'
Expand Down
19 changes: 16 additions & 3 deletions docs/90_Headless.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ This normalizer will transform gallery relations to thumbnail data arrays
### ImageEditableNormalizer
This normalizer will transform the inline image editable to a thumbnail data array

## Custom normalizer
### Custom normalizer
First, you need to add your normalizer:

```yaml
Expand All @@ -179,8 +179,8 @@ class MyNormalizer implements PropertyNormalizerInterface
}
}
```
Then, assign them:

Then, assign them:
```yaml
toolbox:
areas:
Expand All @@ -198,4 +198,17 @@ toolbox:
config: ~
additional_property_normalizer:
myAdditionalProperty: App\Normalizer\MyNormalizer # normalize your config property, added in your "headlessAction() method
```
```

### Default normalizer definition
If you want to apply a normalizer to every element of type `checkbox`, you're able to define it globally:

```yaml
toolbox:
property_normalizer:
default_type_mapping:
checkbox: App\Normalizer\MyNormalizer
```

> It is still possible, to override the default mapping by using the `property_normalizer` in your element config
> since those will be processed with the highest priority!
19 changes: 19 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function addContextNode(ArrayNodeDefinition $rootNode): void
->append($this->buildAreaBlockConfiguration())
->append($this->buildThemeConfiguration())
->append($this->buildDataAttributeConfiguration())
->append($this->buildPropertyNormalizerDefaultsConfiguration())
->end()
->end()
->end()
Expand All @@ -68,6 +69,7 @@ public function addRootNode(ArrayNodeDefinition $rootNode): void
->append($this->buildAreaBlockConfiguration())
->append($this->buildThemeConfiguration())
->append($this->buildDataAttributeConfiguration())
->append($this->buildPropertyNormalizerDefaultsConfiguration())
->end();
}

Expand Down Expand Up @@ -316,6 +318,23 @@ protected function buildDataAttributeConfiguration(): ArrayNodeDefinition
return $treeBuilder;
}

protected function buildPropertyNormalizerDefaultsConfiguration(): ArrayNodeDefinition
{
$treeBuilder = new ArrayNodeDefinition('property_normalizer');

$treeBuilder
->addDefaultsIfNotSet()
->children()
->arrayNode('default_type_mapping')
->useAttributeAsKey('name')
->prototype('scalar')
->end()
->end()
->end();

return $treeBuilder;
}

protected function buildAreasSection(): ArrayNodeDefinition
{
$treeBuilder = new ArrayNodeDefinition('areas');
Expand Down
19 changes: 17 additions & 2 deletions src/Document/Editable/EditableWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,15 @@ private function processSimpleEditableData(HeadlessResponse $data): array
$normalizedData = [];

$config = $data->getEditableConfiguration();
$editableType = $data->getEditableType();
$elementData = $data->getInlineConfigElementData();

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

if (array_key_exists('property_normalizer', $config) && $config['property_normalizer'] !== null) {
$configData = $this->applyNormalizer($config['property_normalizer'], $configData);
} elseif (null !== $defaultNormalizer = $this->getDefaultNormalizer($editableType)) {
$configData = $this->applyNormalizer($defaultNormalizer, $configData);
} elseif ($configData instanceof Editable) {
$configData = $configData->render();
} else {
Expand Down Expand Up @@ -144,8 +147,12 @@ private function processElementData(HeadlessResponse $data, string $areaName): a
$configData = $this->applyNormalizer($configElements[$configName], $configData);
} elseif ($configBlockName !== 'additional_property_normalizer') {
$configNode = $this->findBrickConfigNode($configName, $configElements);
if ($configNode !== null && $configNode['property_normalizer'] !== null) {
$configData = $this->applyNormalizer($configNode['property_normalizer'], $configData);
if ($configNode !== null) {
if ($configNode['property_normalizer'] !== null) {
$configData = $this->applyNormalizer($configNode['property_normalizer'], $configData);
} elseif (null !== $defaultNormalizer = $this->getDefaultNormalizer($configNode['type'])) {
$configData = $this->applyNormalizer($defaultNormalizer, $configData);
}
}
}

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

private function getDefaultNormalizer(string $type): ?string
{
$propertyNormalizerConfig = $this->configManager->getConfig('property_normalizer');
$defaultMapping = $propertyNormalizerConfig['default_type_mapping'];

return $defaultMapping[$type] ?? null;
}

private function getBlockState(): BlockState
{
return $this->blockStateStack->getCurrentState();
Expand Down
3 changes: 2 additions & 1 deletion src/Document/Editable/HeadlessEditableRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,9 @@ private function processEditable(HeadlessEditableInfo $headlessEditableInfo, boo

$simpleHeadlessResponse = new HeadlessResponse(
HeadlessResponse::TYPE_EDITABLE,
$type,
$headlessEditableInfo->getBrickParent(),
$headlessEditableInfo->getEditableConfiguration()
$headlessEditableInfo->getEditableConfiguration(),
);

$simpleHeadlessResponse->setInlineConfigElementData([$editable->getRealName() => $editable]);
Expand Down
8 changes: 7 additions & 1 deletion src/Document/Response/HeadlessResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ class HeadlessResponse

public function __construct(
protected string $type,
protected ?string $editableType = null,
protected ?string $brickParent = null,
protected ?array $editableConfiguration = null
protected ?array $editableConfiguration = null,
) {
}

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

public function getEditableType(): ?string
{
return $this->editableType;
}

public function hasBrickParent(): bool
{
return $this->brickParent !== null;
Expand Down
11 changes: 9 additions & 2 deletions src/Normalizer/ImageEditableNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@ public function normalize(mixed $value, ?string $toolboxContextId = null): mixed
return $value;
}

$imageLightbox = $this->configManager->getImageThumbnailFromConfig('image_lightbox');
$imageElement = $this->configManager->getImageThumbnailFromConfig('image_element');
$config = $value->getConfig();

$imageLightbox = array_key_exists('lightbox_thumbnail', $config)
? $config['lightbox_thumbnail']
: $this->configManager->getImageThumbnailFromConfig('image_lightbox');

$imageElement = array_key_exists('thumbnail', $config)
? $config['thumbnail']
: $this->configManager->getImageThumbnailFromConfig('image_element');

if ($value->getThumbnailConfig()) {
$imageElement = $value->getThumbnailConfig();
Expand Down

0 comments on commit 13c758f

Please sign in to comment.