Skip to content

Commit 4d464c4

Browse files
committed
Fix observers
1 parent 0d0c7b9 commit 4d464c4

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

app/code/Magento/MediaContentCatalog/Observer/Category.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Magento\MediaContentApi\Api\UpdateContentAssetLinksInterface;
1616
use Magento\MediaContentApi\Api\Data\ContentIdentityInterfaceFactory;
1717
use Magento\MediaContentCatalog\Model\ResourceModel\GetContent;
18+
use Magento\Eav\Model\Config;
1819

1920
/**
2021
* Observe the catalog_category_save_after event and run processing relation between category content and media asset.
@@ -51,24 +52,32 @@ class Category implements ObserverInterface
5152
*/
5253
private $metadataPool;
5354

55+
/**
56+
* @var Config
57+
*/
58+
private $config;
59+
5460
/**
5561
* @param ContentIdentityInterfaceFactory $contentIdentityFactory
5662
* @param GetContent $getContent
5763
* @param UpdateContentAssetLinksInterface $updateContentAssetLinks
5864
* @param MetadataPool $metadataPool
65+
* @param Config $config
5966
* @param array $fields
6067
*/
6168
public function __construct(
6269
ContentIdentityInterfaceFactory $contentIdentityFactory,
6370
GetContent $getContent,
6471
UpdateContentAssetLinksInterface $updateContentAssetLinks,
6572
MetadataPool $metadataPool,
73+
Config $config,
6674
array $fields
6775
) {
6876
$this->contentIdentityFactory = $contentIdentityFactory;
6977
$this->getContent = $getContent;
7078
$this->updateContentAssetLinks = $updateContentAssetLinks;
7179
$this->metadataPool = $metadataPool;
80+
$this->config = $config;
7281
$this->fields = $fields;
7382
}
7483

@@ -83,11 +92,14 @@ public function execute(Observer $observer): void
8392
$model = $observer->getEvent()->getData('category');
8493

8594
if ($model instanceof CatalogCategory) {
86-
$id = (int) $model->getData($this->metadataPool->getMetadata(CategoryInterface::class)->getLinkField());
95+
$id = (int) $model->getData(
96+
$this->metadataPool->getMetadata(CategoryInterface::class)->getLinkField()
97+
);
8798
foreach ($this->fields as $field) {
8899
if (!$model->dataHasChangedFor($field)) {
89100
continue;
90101
}
102+
$attribute = $this->config->getAttribute(self::CONTENT_TYPE, $field);
91103
$this->updateContentAssetLinks->execute(
92104
$this->contentIdentityFactory->create(
93105
[
@@ -96,7 +108,7 @@ public function execute(Observer $observer): void
96108
self::ENTITY_ID => (string) $model->getId(),
97109
]
98110
),
99-
$this->getContent->execute($id, $model->getAttributes()[$field])
111+
$this->getContent->execute($id, $attribute)
100112
);
101113
}
102114
}

app/code/Magento/MediaContentCatalog/Observer/Product.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Magento\MediaContentApi\Api\UpdateContentAssetLinksInterface;
1616
use Magento\MediaContentApi\Api\Data\ContentIdentityInterfaceFactory;
1717
use Magento\MediaContentCatalog\Model\ResourceModel\GetContent;
18+
use Magento\Eav\Model\Config;
1819

1920
/**
2021
* 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
5152
*/
5253
private $metadataPool;
5354

55+
/**
56+
* @var Config
57+
*/
58+
private $config;
59+
5460
/**
5561
* @param ContentIdentityInterfaceFactory $contentIdentityFactory
5662
* @param GetContent $getContent
5763
* @param UpdateContentAssetLinksInterface $updateContentAssetLinks
5864
* @param MetadataPool $metadataPool
65+
* @param Config $config
5966
* @param array $fields
6067
*/
6168
public function __construct(
6269
ContentIdentityInterfaceFactory $contentIdentityFactory,
6370
GetContent $getContent,
6471
UpdateContentAssetLinksInterface $updateContentAssetLinks,
6572
MetadataPool $metadataPool,
73+
Config $config,
6674
array $fields
6775
) {
6876
$this->contentIdentityFactory = $contentIdentityFactory;
6977
$this->getContent = $getContent;
7078
$this->updateContentAssetLinks = $updateContentAssetLinks;
7179
$this->metadataPool = $metadataPool;
80+
$this->config = $config;
7281
$this->fields = $fields;
7382
}
7483

@@ -81,22 +90,24 @@ public function __construct(
8190
public function execute(Observer $observer): void
8291
{
8392
$model = $observer->getEvent()->getData('product');
84-
8593
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+
);
8797
foreach ($this->fields as $field) {
8898
if (!$model->dataHasChangedFor($field)) {
8999
continue;
90100
}
101+
$attribute = $this->config->getAttribute(self::CONTENT_TYPE, $field);
91102
$this->updateContentAssetLinks->execute(
92103
$this->contentIdentityFactory->create(
93104
[
94105
self::TYPE => self::CONTENT_TYPE,
95106
self::FIELD => $field,
96-
self::ENTITY_ID => (string) $model->getId(),
107+
self::ENTITY_ID => (string) $id,
97108
]
98109
),
99-
$this->getContent->execute($id, $model->getAttributes()[$field])
110+
$this->getContent->execute($id, $attribute)
100111
);
101112
}
102113
}

0 commit comments

Comments
 (0)