1818
1919use Pimcore \Bundle \StudioBackendBundle \Exception \Api \InvalidArgumentException ;
2020use Pimcore \Bundle \StudioBackendBundle \Metadata \Repository \MetadataRepositoryInterface ;
21+ use Pimcore \Bundle \StudioBackendBundle \Metadata \Service \DataResolverServiceInterface ;
2122use Pimcore \Bundle \StudioBackendBundle \Metadata \Service \MetadataServiceInterface ;
2223use Pimcore \Bundle \StudioBackendBundle \Patcher \Adapter \PatchAdapterInterface ;
2324use Pimcore \Bundle \StudioBackendBundle \Patcher \Service \Loader \TaggedIteratorAdapter ;
2829use Symfony \Component \DependencyInjection \Attribute \AutoconfigureTag ;
2930use function array_key_exists ;
3031use function in_array ;
32+ use function sprintf ;
3133
3234/**
3335 * @internal
3436 */
3537#[AutoconfigureTag(TaggedIteratorAdapter::ADAPTER_TAG )]
3638final class CustomMetadataAdapter implements PatchAdapterInterface
3739{
38- private const INDEX_KEY = 'metadata ' ;
40+ private const string INDEX_KEY = 'metadata ' ;
3941
40- private const PATCHABLE_KEYS = [
42+ private const array PATCHABLE_KEYS = [
4143 'language ' ,
4244 'data ' ,
4345 ];
4446
45- public function __construct (private readonly MetadataRepositoryInterface $ metadataRepository )
46- {
47+ public function __construct (
48+ private readonly DataResolverServiceInterface $ dataResolverService ,
49+ private readonly MetadataRepositoryInterface $ metadataRepository
50+ ) {
4751 }
4852
4953 /**
@@ -67,9 +71,9 @@ public function patch(ElementInterface $element, array $data, UserInterface $use
6771 continue ;
6872 }
6973
70- foreach (self ::PATCHABLE_KEYS as $ patchKeys ) {
71- if (array_key_exists ($ patchKeys , $ metadataForPatch [$ index ])) {
72- $ metadata [$ patchKeys ] = $ metadataForPatch [$ index ][ $ patchKeys ] ;
74+ foreach (self ::PATCHABLE_KEYS as $ patchKey ) {
75+ if (array_key_exists ($ patchKey , $ metadataForPatch [$ index ])) {
76+ $ metadata [$ patchKey ] = $ this -> getExistingEntryValue ( $ metadataForPatch [$ index ], $ patchKey , $ user ) ;
7377 }
7478 }
7579 $ patchedMetadata [] = $ metadata ;
@@ -81,7 +85,7 @@ public function patch(ElementInterface $element, array $data, UserInterface $use
8185 $ patchedMetadata = [
8286 ...$ patchedMetadata ,
8387 ...array_map (
84- fn (array $ metaData ) => $ this ->processNewMetadataEntry ($ metaData ),
88+ fn (array $ metaData ) => $ this ->processNewMetadataEntry ($ metaData, $ user ),
8589 $ metadataForPatch
8690 ),
8791 ];
@@ -103,10 +107,19 @@ public function supportedElementTypes(): array
103107 ];
104108 }
105109
110+ private function getExistingEntryValue (array $ metadata , string $ key , UserInterface $ user ): mixed
111+ {
112+ if ($ key !== 'data ' ) {
113+ return $ metadata [$ key ];
114+ }
115+
116+ return $ this ->dataResolverService ->denormalizeData ($ metadata , $ user );
117+ }
118+
106119 /**
107120 * @throws InvalidArgumentException
108121 */
109- private function processNewMetadataEntry (array $ metadata ): array
122+ private function processNewMetadataEntry (array $ metadata, UserInterface $ user ): array
110123 {
111124 if (!isset ($ metadata ['name ' ])) {
112125 throw new InvalidArgumentException ('Metadata name is required ' );
@@ -119,14 +132,14 @@ private function processNewMetadataEntry(array $metadata): array
119132 $ predefined = $ this ->metadataRepository ->getPredefinedMetadataByName ($ metadata ['name ' ]);
120133
121134 if (!$ predefined ) {
122- throw new InvalidArgumentException ('Predefined metadata not found ' );
135+ throw new InvalidArgumentException (sprintf ( 'Predefined metadata %s not found ' , $ metadata [ ' name ' ]) );
123136 }
124137
125138 return [
126139 'name ' => $ predefined ->getName (),
127140 'language ' => $ metadata ['language ' ] ?? '' ,
128141 'type ' => $ predefined ->getType (),
129- 'data ' => $ metadata ['data ' ] ?? null ,
142+ 'data ' => $ metadata ['data ' ] ? $ this -> dataResolverService -> denormalizeData ( $ metadata , $ user ) : null ,
130143 ];
131144 }
132145
0 commit comments