15
15
use Magento \MediaContentApi \Api \UpdateContentAssetLinksInterface ;
16
16
use Magento \MediaContentApi \Api \Data \ContentIdentityInterfaceFactory ;
17
17
use Magento \MediaContentCatalog \Model \ResourceModel \GetContent ;
18
+ use Magento \Eav \Model \Config ;
18
19
19
20
/**
20
21
* Observe the catalog_product_save_after event and run processing relation between product content and media asset
@@ -51,24 +52,32 @@ class Product implements ObserverInterface
51
52
*/
52
53
private $ metadataPool ;
53
54
55
+ /**
56
+ * @var Config
57
+ */
58
+ private $ config ;
59
+
54
60
/**
55
61
* @param ContentIdentityInterfaceFactory $contentIdentityFactory
56
62
* @param GetContent $getContent
57
63
* @param UpdateContentAssetLinksInterface $updateContentAssetLinks
58
64
* @param MetadataPool $metadataPool
65
+ * @param Config $config
59
66
* @param array $fields
60
67
*/
61
68
public function __construct (
62
69
ContentIdentityInterfaceFactory $ contentIdentityFactory ,
63
70
GetContent $ getContent ,
64
71
UpdateContentAssetLinksInterface $ updateContentAssetLinks ,
65
72
MetadataPool $ metadataPool ,
73
+ Config $ config ,
66
74
array $ fields
67
75
) {
68
76
$ this ->contentIdentityFactory = $ contentIdentityFactory ;
69
77
$ this ->getContent = $ getContent ;
70
78
$ this ->updateContentAssetLinks = $ updateContentAssetLinks ;
71
79
$ this ->metadataPool = $ metadataPool ;
80
+ $ this ->config = $ config ;
72
81
$ this ->fields = $ fields ;
73
82
}
74
83
@@ -81,22 +90,24 @@ public function __construct(
81
90
public function execute (Observer $ observer ): void
82
91
{
83
92
$ model = $ observer ->getEvent ()->getData ('product ' );
84
-
85
93
if ($ model instanceof CatalogProduct) {
86
- $ id = (int ) $ model ->getData ($ this ->metadataPool ->getMetadata (ProductInterface::class)->getLinkField ());
94
+ $ id = (int ) $ model ->getData (
95
+ $ this ->metadataPool ->getMetadata (ProductInterface::class)->getLinkField ()
96
+ );
87
97
foreach ($ this ->fields as $ field ) {
88
98
if (!$ model ->dataHasChangedFor ($ field )) {
89
99
continue ;
90
100
}
101
+ $ attribute = $ this ->config ->getAttribute (self ::CONTENT_TYPE , $ field );
91
102
$ this ->updateContentAssetLinks ->execute (
92
103
$ this ->contentIdentityFactory ->create (
93
104
[
94
105
self ::TYPE => self ::CONTENT_TYPE ,
95
106
self ::FIELD => $ field ,
96
- self ::ENTITY_ID => (string ) $ model -> getId () ,
107
+ self ::ENTITY_ID => (string ) $ id ,
97
108
]
98
109
),
99
- $ this ->getContent ->execute ($ id , $ model -> getAttributes ()[ $ field ] )
110
+ $ this ->getContent ->execute ($ id , $ attribute )
100
111
);
101
112
}
102
113
}
0 commit comments