From 85fa0038c938cd5b1023c63318dec414ca2a8ac8 Mon Sep 17 00:00:00 2001 From: mattamon Date: Mon, 13 Jan 2025 08:38:15 +0100 Subject: [PATCH 1/7] Add normalizer interface to video --- src/DataObject/Data/Adapter/VideoAdapter.php | 24 ++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/DataObject/Data/Adapter/VideoAdapter.php b/src/DataObject/Data/Adapter/VideoAdapter.php index fc020d11..b80b3248 100644 --- a/src/DataObject/Data/Adapter/VideoAdapter.php +++ b/src/DataObject/Data/Adapter/VideoAdapter.php @@ -17,6 +17,7 @@ namespace Pimcore\Bundle\StudioBackendBundle\DataObject\Data\Adapter; use Pimcore\Bundle\StaticResolverBundle\Models\Asset\AssetResolverInterface; +use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\DataNormalizerInterface; use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\Model\FieldContextData; use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\SetterDataInterface; use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterLoaderInterface; @@ -33,10 +34,11 @@ * @internal */ #[AutoconfigureTag(DataAdapterLoaderInterface::ADAPTER_TAG)] -final readonly class VideoAdapter implements SetterDataInterface +final readonly class VideoAdapter implements SetterDataInterface, DataNormalizerInterface { use ElementProviderTrait; + public function __construct(private AssetResolverInterface $assetResolver) { } @@ -49,7 +51,6 @@ public function getDataForSetter( ?FieldContextData $contextData = null ): ?Video { $adapterData = $data[$key] ?? null; - if (!is_array($adapterData)) { return null; } @@ -85,4 +86,23 @@ private function getAssetByPath(?string $path): ?Asset { return $path ? $this->assetResolver->getByPath($path) : null; } + + public function normalize(mixed $value, Data $fieldDefinition): mixed + { + if (!$value instanceof Video) { + return null; + } + + $data = $fieldDefinition->normalize($value); + + if (isset($data['poster'])) { + $data['poster']['path'] = $value->getPoster()?->getRealFullPath(); + } + + if (isset($data['data'])) { + $data['data']['path'] = $value->getData()?->getRealFullPath(); + } + + return $data; + } } From ec56eb93594cc7b5f934b4063b7756c940362043 Mon Sep 17 00:00:00 2001 From: mattamon <38959016+mattamon@users.noreply.github.com> Date: Mon, 13 Jan 2025 07:39:36 +0000 Subject: [PATCH 2/7] Apply php-cs-fixer changes --- src/DataObject/Data/Adapter/VideoAdapter.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/DataObject/Data/Adapter/VideoAdapter.php b/src/DataObject/Data/Adapter/VideoAdapter.php index b80b3248..9930e193 100644 --- a/src/DataObject/Data/Adapter/VideoAdapter.php +++ b/src/DataObject/Data/Adapter/VideoAdapter.php @@ -38,7 +38,6 @@ { use ElementProviderTrait; - public function __construct(private AssetResolverInterface $assetResolver) { } From 4621f294a3f3d7e98a2ffe18552a01f44d2cdd9f Mon Sep 17 00:00:00 2001 From: mattamon Date: Mon, 13 Jan 2025 09:03:18 +0100 Subject: [PATCH 3/7] Add subtype and fullpath to manytoone relation --- .../Data/Adapter/ManyToOneRelationAdapter.php | 14 +++++++++++++- src/DataObject/Data/Adapter/VideoAdapter.php | 5 +++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/DataObject/Data/Adapter/ManyToOneRelationAdapter.php b/src/DataObject/Data/Adapter/ManyToOneRelationAdapter.php index 5a93423b..89d740f2 100644 --- a/src/DataObject/Data/Adapter/ManyToOneRelationAdapter.php +++ b/src/DataObject/Data/Adapter/ManyToOneRelationAdapter.php @@ -17,6 +17,7 @@ namespace Pimcore\Bundle\StudioBackendBundle\DataObject\Data\Adapter; use Pimcore\Bundle\StaticResolverBundle\Models\Element\ServiceResolverInterface; +use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\DataNormalizerInterface; use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\Model\FieldContextData; use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\SetterDataInterface; use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterLoaderInterface; @@ -32,7 +33,7 @@ * @internal */ #[AutoconfigureTag(DataAdapterLoaderInterface::ADAPTER_TAG)] -final readonly class ManyToOneRelationAdapter implements SetterDataInterface +final readonly class ManyToOneRelationAdapter implements SetterDataInterface, DataNormalizerInterface { use ElementProviderTrait; @@ -59,4 +60,15 @@ public function getDataForSetter( return null; } } + + public function normalize(mixed $value, Data $fieldDefinition): mixed + { + $data = $fieldDefinition->normalize($value); + + if (!empty($data)) { + $data['fullPath'] = $value->getRealFullPath(); + $data['subtype'] = $value->getType(); + } + return $data; + } } diff --git a/src/DataObject/Data/Adapter/VideoAdapter.php b/src/DataObject/Data/Adapter/VideoAdapter.php index 9930e193..b68b5651 100644 --- a/src/DataObject/Data/Adapter/VideoAdapter.php +++ b/src/DataObject/Data/Adapter/VideoAdapter.php @@ -93,13 +93,14 @@ public function normalize(mixed $value, Data $fieldDefinition): mixed } $data = $fieldDefinition->normalize($value); + $data['subtype'] = $value->getData()?->getType(); if (isset($data['poster'])) { - $data['poster']['path'] = $value->getPoster()?->getRealFullPath(); + $data['poster']['fullPath'] = $value->getPoster()?->getRealFullPath(); } if (isset($data['data'])) { - $data['data']['path'] = $value->getData()?->getRealFullPath(); + $data['data']['fullPath'] = $value->getData()?->getRealFullPath(); } return $data; From d45024aa8c20278697ff0912eb81f92065043ccd Mon Sep 17 00:00:00 2001 From: mattamon <38959016+mattamon@users.noreply.github.com> Date: Mon, 13 Jan 2025 08:04:11 +0000 Subject: [PATCH 4/7] Apply php-cs-fixer changes --- src/DataObject/Data/Adapter/ManyToOneRelationAdapter.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/DataObject/Data/Adapter/ManyToOneRelationAdapter.php b/src/DataObject/Data/Adapter/ManyToOneRelationAdapter.php index 89d740f2..89c51831 100644 --- a/src/DataObject/Data/Adapter/ManyToOneRelationAdapter.php +++ b/src/DataObject/Data/Adapter/ManyToOneRelationAdapter.php @@ -66,9 +66,10 @@ public function normalize(mixed $value, Data $fieldDefinition): mixed $data = $fieldDefinition->normalize($value); if (!empty($data)) { - $data['fullPath'] = $value->getRealFullPath(); - $data['subtype'] = $value->getType(); + $data['fullPath'] = $value->getRealFullPath(); + $data['subtype'] = $value->getType(); } + return $data; } } From 0f26fbbb017ff71178727915f7f784697a937fc3 Mon Sep 17 00:00:00 2001 From: mattamon Date: Mon, 13 Jan 2025 09:35:43 +0100 Subject: [PATCH 5/7] Add normalizer check --- src/DataObject/Data/Adapter/ManyToOneRelationAdapter.php | 5 +++++ src/DataObject/Data/Adapter/VideoAdapter.php | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/DataObject/Data/Adapter/ManyToOneRelationAdapter.php b/src/DataObject/Data/Adapter/ManyToOneRelationAdapter.php index 89c51831..61fe3fb9 100644 --- a/src/DataObject/Data/Adapter/ManyToOneRelationAdapter.php +++ b/src/DataObject/Data/Adapter/ManyToOneRelationAdapter.php @@ -26,6 +26,7 @@ use Pimcore\Model\DataObject\ClassDefinition\Data; use Pimcore\Model\DataObject\Concrete; use Pimcore\Model\Element\ElementInterface; +use Pimcore\Normalizer\NormalizerInterface; use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag; use function is_array; @@ -63,6 +64,10 @@ public function getDataForSetter( public function normalize(mixed $value, Data $fieldDefinition): mixed { + if (!$fieldDefinition instanceof NormalizerInterface) { + return null; + } + $data = $fieldDefinition->normalize($value); if (!empty($data)) { diff --git a/src/DataObject/Data/Adapter/VideoAdapter.php b/src/DataObject/Data/Adapter/VideoAdapter.php index b68b5651..a9650336 100644 --- a/src/DataObject/Data/Adapter/VideoAdapter.php +++ b/src/DataObject/Data/Adapter/VideoAdapter.php @@ -27,6 +27,7 @@ use Pimcore\Model\DataObject\ClassDefinition\Data; use Pimcore\Model\DataObject\Concrete; use Pimcore\Model\DataObject\Data\Video; +use Pimcore\Normalizer\NormalizerInterface; use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag; use function is_array; @@ -88,7 +89,8 @@ private function getAssetByPath(?string $path): ?Asset public function normalize(mixed $value, Data $fieldDefinition): mixed { - if (!$value instanceof Video) { + + if (!$value instanceof Video || !$fieldDefinition instanceof NormalizerInterface) { return null; } From d46e1472c3901b31ca78b2fb809ef0487135db0e Mon Sep 17 00:00:00 2001 From: mattamon Date: Mon, 13 Jan 2025 11:56:47 +0100 Subject: [PATCH 6/7] Move subtype to data --- src/DataObject/Data/Adapter/VideoAdapter.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/DataObject/Data/Adapter/VideoAdapter.php b/src/DataObject/Data/Adapter/VideoAdapter.php index a9650336..77e75fd0 100644 --- a/src/DataObject/Data/Adapter/VideoAdapter.php +++ b/src/DataObject/Data/Adapter/VideoAdapter.php @@ -95,7 +95,7 @@ public function normalize(mixed $value, Data $fieldDefinition): mixed } $data = $fieldDefinition->normalize($value); - $data['subtype'] = $value->getData()?->getType(); + if (isset($data['poster'])) { $data['poster']['fullPath'] = $value->getPoster()?->getRealFullPath(); @@ -103,6 +103,7 @@ public function normalize(mixed $value, Data $fieldDefinition): mixed if (isset($data['data'])) { $data['data']['fullPath'] = $value->getData()?->getRealFullPath(); + $data['data']['subtype'] = $value->getData()?->getType(); } return $data; From 33096721f0cc06a35a09012f053866f1689404d8 Mon Sep 17 00:00:00 2001 From: mattamon <38959016+mattamon@users.noreply.github.com> Date: Mon, 13 Jan 2025 10:57:27 +0000 Subject: [PATCH 7/7] Apply php-cs-fixer changes --- src/DataObject/Data/Adapter/VideoAdapter.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/DataObject/Data/Adapter/VideoAdapter.php b/src/DataObject/Data/Adapter/VideoAdapter.php index 77e75fd0..535c8ba5 100644 --- a/src/DataObject/Data/Adapter/VideoAdapter.php +++ b/src/DataObject/Data/Adapter/VideoAdapter.php @@ -96,7 +96,6 @@ public function normalize(mixed $value, Data $fieldDefinition): mixed $data = $fieldDefinition->normalize($value); - if (isset($data['poster'])) { $data['poster']['fullPath'] = $value->getPoster()?->getRealFullPath(); }