Skip to content

Commit c065bbd

Browse files
committed
Fix observers
1 parent 37ba576 commit c065bbd

File tree

4 files changed

+116
-48
lines changed

4 files changed

+116
-48
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaContentCatalog\Model\ResourceModel;
9+
10+
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
11+
use Magento\Catalog\Model\ResourceModel\Category;
12+
use Magento\Framework\App\ResourceConnection;
13+
14+
/**
15+
* Get concatenated content for all store views
16+
*/
17+
class GetCategoryContent
18+
{
19+
/**
20+
* @var Category
21+
*/
22+
private $categoryResource;
23+
24+
/**
25+
* @var ResourceConnection
26+
*/
27+
private $resourceConnection;
28+
29+
/**
30+
* @param ResourceConnection $resourceConnection
31+
* @param Category $categoryResource
32+
*/
33+
public function __construct(
34+
ResourceConnection $resourceConnection,
35+
Category $categoryResource
36+
) {
37+
$this->categoryResource = $categoryResource;
38+
$this->resourceConnection = $resourceConnection;
39+
}
40+
41+
/**
42+
* Get concatenated category content for all store views
43+
*
44+
* @param int $entityId
45+
* @param AbstractAttribute $attribute
46+
* @return string
47+
*/
48+
public function execute(int $entityId, AbstractAttribute $attribute): string
49+
{
50+
return implode(
51+
PHP_EOL,
52+
$this->getDistinctContent(
53+
$entityId,
54+
$attribute
55+
)
56+
);
57+
}
58+
59+
/**
60+
* Load values of an category attribute for all store views
61+
*
62+
* @param int $entityId
63+
* @param AbstractAttribute $attribute
64+
* @return array
65+
*/
66+
private function getDistinctContent(int $entityId, AbstractAttribute $attribute): array
67+
{
68+
$connection = $this->resourceConnection->getConnection();
69+
$select = $connection->select()->from(
70+
['abt' => $attribute->getBackendTable()],
71+
'abt.value'
72+
)->joinInner(
73+
['rt' => $this->categoryResource->getEntityTable()],
74+
'rt.' . $attribute->getEntityIdField() . ' = abt.' . $attribute->getEntityIdField()
75+
)->where(
76+
'rt.entity_id = ?',
77+
$entityId
78+
)->where(
79+
$connection->quoteIdentifier('abt.attribute_id') . ' = ?',
80+
(int) $attribute->getAttributeId()
81+
);
82+
return $connection->fetchCol($select);
83+
}
84+
}

app/code/Magento/MediaContentCatalog/Model/ResourceModel/GetContent.php renamed to app/code/Magento/MediaContentCatalog/Model/ResourceModel/GetProductContent.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,38 @@
88
namespace Magento\MediaContentCatalog\Model\ResourceModel;
99

1010
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
11+
use Magento\Catalog\Model\ResourceModel\Product;
1112
use Magento\Framework\App\ResourceConnection;
1213

1314
/**
1415
* Get concatenated content for all store views
1516
*/
16-
class GetContent
17+
class GetProductContent
1718
{
19+
/**
20+
* @var Product
21+
*/
22+
private $productResource;
23+
1824
/**
1925
* @var ResourceConnection
2026
*/
2127
private $resourceConnection;
2228

2329
/**
2430
* @param ResourceConnection $resourceConnection
31+
* @param Product $productResource
2532
*/
2633
public function __construct(
27-
ResourceConnection $resourceConnection
34+
ResourceConnection $resourceConnection,
35+
Product $productResource
2836
) {
37+
$this->productResource = $productResource;
2938
$this->resourceConnection = $resourceConnection;
3039
}
3140

3241
/**
33-
* Get concatenated content for all store views
42+
* Get concatenated product content for all store views
3443
*
3544
* @param int $entityId
3645
* @param AbstractAttribute $attribute
@@ -48,7 +57,7 @@ public function execute(int $entityId, AbstractAttribute $attribute): string
4857
}
4958

5059
/**
51-
* Load values of an attribute for all store views
60+
* Load values of an product attribute for all store views
5261
*
5362
* @param int $entityId
5463
* @param AbstractAttribute $attribute
@@ -57,18 +66,19 @@ public function execute(int $entityId, AbstractAttribute $attribute): string
5766
private function getDistinctContent(int $entityId, AbstractAttribute $attribute): array
5867
{
5968
$connection = $this->resourceConnection->getConnection();
60-
6169
$select = $connection->select()->from(
6270
['abt' => $attribute->getBackendTable()],
6371
'abt.value'
72+
)->joinInner(
73+
['rt' => $this->productResource->getEntityTable()],
74+
'rt.' . $attribute->getEntityIdField() . ' = abt.' . $attribute->getEntityIdField()
75+
)->where(
76+
'rt.entity_id = ?',
77+
$entityId
6478
)->where(
6579
$connection->quoteIdentifier('abt.attribute_id') . ' = ?',
6680
(int) $attribute->getAttributeId()
67-
)->where(
68-
$connection->quoteIdentifier('abt.' . $attribute->getEntityIdField()) . ' = ?',
69-
$entityId
70-
)->distinct(true);
71-
81+
);
7282
return $connection->fetchCol($select);
7383
}
7484
}

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

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@
77

88
namespace Magento\MediaContentCatalog\Observer;
99

10-
use Magento\Catalog\Api\Data\CategoryInterface;
1110
use Magento\Catalog\Model\Category as CatalogCategory;
12-
use Magento\Framework\EntityManager\MetadataPool;
1311
use Magento\Framework\Event\Observer;
1412
use Magento\Framework\Event\ObserverInterface;
1513
use Magento\MediaContentApi\Api\UpdateContentAssetLinksInterface;
1614
use Magento\MediaContentApi\Api\Data\ContentIdentityInterfaceFactory;
17-
use Magento\MediaContentCatalog\Model\ResourceModel\GetContent;
15+
use Magento\MediaContentCatalog\Model\ResourceModel\GetCategoryContent;
1816
use Magento\Eav\Model\Config;
1917

2018
/**
@@ -43,15 +41,10 @@ class Category implements ObserverInterface
4341
private $contentIdentityFactory;
4442

4543
/**
46-
* @var GetContent
44+
* @var GetCategoryContent
4745
*/
4846
private $getContent;
4947

50-
/**
51-
* @var MetadataPool
52-
*/
53-
private $metadataPool;
54-
5548
/**
5649
*
5750
* @var Config
@@ -62,24 +55,21 @@ class Category implements ObserverInterface
6255
* Create links for category content
6356
*
6457
* @param ContentIdentityInterfaceFactory $contentIdentityFactory
65-
* @param GetContent $getContent
66-
* @param MetadataPool $metadataPool
58+
* @param GetCategoryContent $getContent
6759
* @param UpdateContentAssetLinksInterface $updateContentAssetLinks
6860
* @param Config $config
6961
* @param array $fields
7062
*/
7163
public function __construct(
7264
ContentIdentityInterfaceFactory $contentIdentityFactory,
73-
GetContent $getContent,
74-
MetadataPool $metadataPool,
65+
GetCategoryContent $getContent,
7566
UpdateContentAssetLinksInterface $updateContentAssetLinks,
7667
Config $config,
7768
array $fields
7869
) {
7970
$this->contentIdentityFactory = $contentIdentityFactory;
8071
$this->getContent = $getContent;
8172
$this->updateContentAssetLinks = $updateContentAssetLinks;
82-
$this->metadataPool = $metadataPool;
8373
$this->fields = $fields;
8474
$this->config = $config;
8575
}
@@ -95,9 +85,6 @@ public function execute(Observer $observer): void
9585
$model = $observer->getEvent()->getData('category');
9686

9787
if ($model instanceof CatalogCategory) {
98-
$id = (int) $model->getData(
99-
$this->metadataPool->getMetadata(CategoryInterface::class)->getLinkField()
100-
);
10188
foreach ($this->fields as $field) {
10289
if (!$model->dataHasChangedFor($field)) {
10390
continue;
@@ -108,10 +95,10 @@ public function execute(Observer $observer): void
10895
[
10996
self::TYPE => self::CONTENT_TYPE,
11097
self::FIELD => $field,
111-
self::ENTITY_ID => (string) $id,
98+
self::ENTITY_ID => (string) $model->getEntityId(),
11299
]
113100
),
114-
$this->getContent->execute($id, $attribute)
101+
$this->getContent->execute((int) $model->getEntityId(), $attribute)
115102
);
116103
}
117104
}

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

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@
77

88
namespace Magento\MediaContentCatalog\Observer;
99

10-
use Magento\Catalog\Api\Data\ProductInterface;
1110
use Magento\Catalog\Model\Product as CatalogProduct;
12-
use Magento\Framework\EntityManager\MetadataPool;
1311
use Magento\Framework\Event\Observer;
1412
use Magento\Framework\Event\ObserverInterface;
1513
use Magento\MediaContentApi\Api\UpdateContentAssetLinksInterface;
1614
use Magento\MediaContentApi\Api\Data\ContentIdentityInterfaceFactory;
17-
use Magento\MediaContentCatalog\Model\ResourceModel\GetContent;
15+
use Magento\MediaContentCatalog\Model\ResourceModel\GetProductContent;
1816
use Magento\Eav\Model\Config;
1917

2018
/**
@@ -43,15 +41,10 @@ class Product implements ObserverInterface
4341
private $contentIdentityFactory;
4442

4543
/**
46-
* @var GetContent
44+
* @var GetProductContent
4745
*/
4846
private $getContent;
4947

50-
/**
51-
* @var MetadataPool
52-
*/
53-
private $metadataPool;
54-
5548
/**
5649
* @var Config
5750
*/
@@ -61,24 +54,21 @@ class Product implements ObserverInterface
6154
* * Create links for product content
6255
*
6356
* @param ContentIdentityInterfaceFactory $contentIdentityFactory
64-
* @param GetContent $getContent
57+
* @param GetProductContent $getContent
6558
* @param UpdateContentAssetLinksInterface $updateContentAssetLinks
66-
* @param MetadataPool $metadataPool
6759
* @param Config $config
6860
* @param array $fields
6961
*/
7062
public function __construct(
7163
ContentIdentityInterfaceFactory $contentIdentityFactory,
72-
GetContent $getContent,
64+
GetProductContent $getContent,
7365
UpdateContentAssetLinksInterface $updateContentAssetLinks,
74-
MetadataPool $metadataPool,
7566
Config $config,
7667
array $fields
7768
) {
7869
$this->contentIdentityFactory = $contentIdentityFactory;
7970
$this->getContent = $getContent;
8071
$this->updateContentAssetLinks = $updateContentAssetLinks;
81-
$this->metadataPool = $metadataPool;
8272
$this->config = $config;
8373
$this->fields = $fields;
8474
}
@@ -93,9 +83,6 @@ public function execute(Observer $observer): void
9383
{
9484
$model = $observer->getEvent()->getData('product');
9585
if ($model instanceof CatalogProduct) {
96-
$id = (int) $model->getData(
97-
$this->metadataPool->getMetadata(ProductInterface::class)->getLinkField()
98-
);
9986
foreach ($this->fields as $field) {
10087
if (!$model->dataHasChangedFor($field)) {
10188
continue;
@@ -106,10 +93,10 @@ public function execute(Observer $observer): void
10693
[
10794
self::TYPE => self::CONTENT_TYPE,
10895
self::FIELD => $field,
109-
self::ENTITY_ID => (string) $id,
96+
self::ENTITY_ID => (string) $model->getEntityId(),
11097
]
11198
),
112-
$this->getContent->execute($id, $attribute)
99+
$this->getContent->execute((int) $model->getEntityId(), $attribute)
113100
);
114101
}
115102
}

0 commit comments

Comments
 (0)