Skip to content

Commit 89c865b

Browse files
authored
ENGCOM-7492: ASI: Fix observers #27948
2 parents 94f5e88 + 636565e commit 89c865b

File tree

21 files changed

+618
-25
lines changed

21 files changed

+618
-25
lines changed

app/code/Magento/MediaContent/etc/di.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
<preference for="Magento\MediaContentApi\Api\Data\ContentIdentityInterface" type="Magento\MediaContent\Model\ContentIdentity"/>
1717
<preference for="Magento\MediaContentApi\Api\Data\ContentAssetLinkInterface" type="Magento\MediaContent\Model\ContentAssetLink"/>
1818
<preference for="Magento\MediaContentApi\Model\SearchPatternConfigInterface" type="Magento\MediaContent\Model\Content\SearchPatternConfig"/>
19-
<preference for="Magento\MediaContent\Model\Content\ConfigInterface" type="Magento\MediaContent\Model\Content\Config"/>
2019
<type name="Magento\MediaGalleryApi\Api\DeleteAssetsByPathsInterface">
2120
<plugin name="remove_media_content_after_asset_is_removed_by_path" type="Magento\MediaContent\Plugin\MediaGalleryAssetDeleteByPath" />
2221
</type>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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\MediaContentApi\Model\Composite;
9+
10+
use Magento\MediaContentApi\Api\Data\ContentIdentityInterface;
11+
use Magento\MediaContentApi\Model\GetEntityContentsInterface;
12+
13+
/**
14+
* Get concatenated content for all store views
15+
*/
16+
class GetEntityContents implements GetEntityContentsInterface
17+
{
18+
/**
19+
* @var GetEntityContentsInterface[]
20+
*/
21+
private $items;
22+
23+
/**
24+
* @param GetEntityContentsInterface[] $items
25+
*/
26+
public function __construct(
27+
$items = []
28+
) {
29+
foreach ($items as $item) {
30+
if (!$item instanceof GetEntityContentsInterface) {
31+
throw new \InvalidArgumentException(
32+
__('GetContent items must implement %1.', GetEntityContentsInterface::class)
33+
);
34+
}
35+
}
36+
37+
$this->items = $items;
38+
}
39+
40+
/**
41+
* Get concatenated content for the content identity
42+
*
43+
* @param ContentIdentityInterface $contentIdentity
44+
* @return string[]
45+
*/
46+
public function execute(ContentIdentityInterface $contentIdentity): array
47+
{
48+
if (isset($this->items[$contentIdentity->getEntityType()])) {
49+
return $this->items[$contentIdentity->getEntityType()]->execute($contentIdentity);
50+
}
51+
return [];
52+
}
53+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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\MediaContentApi\Model;
9+
10+
use Magento\MediaContentApi\Api\Data\ContentIdentityInterface;
11+
12+
/**
13+
* Get Entity Contents.
14+
* @api
15+
*/
16+
interface GetEntityContentsInterface
17+
{
18+
/**
19+
* Get concatenated content by the content identity
20+
*
21+
* @param ContentIdentityInterface $contentIdentity
22+
* @return string[]
23+
*/
24+
public function execute(ContentIdentityInterface $contentIdentity): array;
25+
}

app/code/Magento/MediaContentApi/Model/SearchPatternConfigInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\MediaContentApi\Model;
89

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<preference for="Magento\MediaContentApi\Model\GetEntityContentsInterface" type="Magento\MediaContentApi\Model\Composite\GetEntityContents"/>
10+
</config>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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\Catalog\Model\ResourceModel\Product;
11+
use Magento\Framework\App\ResourceConnection;
12+
use Magento\MediaContentApi\Model\GetEntityContentsInterface;
13+
use Magento\MediaContentApi\Api\Data\ContentIdentityInterface;
14+
use Magento\Eav\Model\Config;
15+
16+
/**
17+
* Get concatenated content for all store views
18+
*/
19+
class GetEntityContent implements GetEntityContentsInterface
20+
{
21+
/**
22+
* @var Config
23+
*/
24+
private $config;
25+
26+
/**
27+
* @var Product
28+
*/
29+
private $productResource;
30+
31+
/**
32+
* @var ResourceConnection
33+
*/
34+
private $resourceConnection;
35+
36+
/**
37+
* @param Config $config
38+
* @param ResourceConnection $resourceConnection
39+
* @param Product $productResource
40+
*/
41+
public function __construct(
42+
Config $config,
43+
ResourceConnection $resourceConnection,
44+
Product $productResource
45+
) {
46+
$this->config = $config;
47+
$this->productResource = $productResource;
48+
$this->resourceConnection = $resourceConnection;
49+
}
50+
51+
/**
52+
* Get product content for all store views
53+
*
54+
* @param ContentIdentityInterface $contentIdentity
55+
* @return array
56+
* @throws \Magento\Framework\Exception\LocalizedException
57+
*/
58+
public function execute(ContentIdentityInterface $contentIdentity): array
59+
{
60+
$attribute = $this->config->getAttribute($contentIdentity->getEntityType(), $contentIdentity->getField());
61+
$connection = $this->resourceConnection->getConnection();
62+
63+
$select = $connection->select()->from(
64+
['abt' => $attribute->getBackendTable()],
65+
'abt.value'
66+
)->where(
67+
$connection->quoteIdentifier('abt.attribute_id') . ' = ?',
68+
(int) $attribute->getAttributeId()
69+
)->where(
70+
$connection->quoteIdentifier('abt.' . $attribute->getEntityIdField()) . ' = ?',
71+
$contentIdentity->getEntityId()
72+
)->distinct(true);
73+
74+
return $connection->fetchCol($select);
75+
}
76+
}

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Framework\Event\ObserverInterface;
1313
use Magento\MediaContentApi\Api\UpdateContentAssetLinksInterface;
1414
use Magento\MediaContentApi\Api\Data\ContentIdentityInterfaceFactory;
15+
use Magento\MediaContentApi\Model\GetEntityContentsInterface;
1516

1617
/**
1718
* Observe the catalog_category_save_after event and run processing relation between category content and media asset.
@@ -39,16 +40,26 @@ class Category implements ObserverInterface
3940
private $contentIdentityFactory;
4041

4142
/**
43+
* @var GetEntityContentsInterface
44+
*/
45+
private $getContent;
46+
47+
/**
48+
* Create links for category content
49+
*
4250
* @param ContentIdentityInterfaceFactory $contentIdentityFactory
51+
* @param GetEntityContentsInterface $getContent
4352
* @param UpdateContentAssetLinksInterface $updateContentAssetLinks
4453
* @param array $fields
4554
*/
4655
public function __construct(
4756
ContentIdentityInterfaceFactory $contentIdentityFactory,
57+
GetEntityContentsInterface $getContent,
4858
UpdateContentAssetLinksInterface $updateContentAssetLinks,
4959
array $fields
5060
) {
5161
$this->contentIdentityFactory = $contentIdentityFactory;
62+
$this->getContent = $getContent;
5263
$this->updateContentAssetLinks = $updateContentAssetLinks;
5364
$this->fields = $fields;
5465
}
@@ -57,6 +68,7 @@ public function __construct(
5768
* Retrieve the saved category and pass it to the model processor to save content - asset relations
5869
*
5970
* @param Observer $observer
71+
* @throws \Exception
6072
*/
6173
public function execute(Observer $observer): void
6274
{
@@ -67,16 +79,15 @@ public function execute(Observer $observer): void
6779
if (!$model->dataHasChangedFor($field)) {
6880
continue;
6981
}
70-
$this->updateContentAssetLinks->execute(
71-
$this->contentIdentityFactory->create(
72-
[
73-
self::TYPE => self::CONTENT_TYPE,
74-
self::FIELD => $field,
75-
self::ENTITY_ID => (string) $model->getId(),
76-
]
77-
),
78-
(string) $model->getData($field)
82+
$contentIdentity = $this->contentIdentityFactory->create(
83+
[
84+
self::TYPE => self::CONTENT_TYPE,
85+
self::FIELD => $field,
86+
self::ENTITY_ID => (string) $model->getEntityId(),
87+
]
7988
);
89+
$concatenatedContent = implode(PHP_EOL, $this->getContent->execute($contentIdentity));
90+
$this->updateContentAssetLinks->execute($contentIdentity, $concatenatedContent);
8091
}
8192
}
8293
}

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

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Framework\Event\ObserverInterface;
1313
use Magento\MediaContentApi\Api\UpdateContentAssetLinksInterface;
1414
use Magento\MediaContentApi\Api\Data\ContentIdentityInterfaceFactory;
15+
use Magento\MediaContentApi\Model\GetEntityContentsInterface;
1516

1617
/**
1718
* Observe the catalog_product_save_after event and run processing relation between product content and media asset
@@ -26,7 +27,7 @@ class Product implements ObserverInterface
2627
/**
2728
* @var UpdateContentAssetLinksInterface
2829
*/
29-
private $processor;
30+
private $updateContentAssetLinks;
3031

3132
/**
3233
* @var array
@@ -39,45 +40,53 @@ class Product implements ObserverInterface
3940
private $contentIdentityFactory;
4041

4142
/**
43+
* @var GetEntityContentsInterface
44+
*/
45+
private $getContent;
46+
47+
/**
48+
* * Create links for product content
49+
*
4250
* @param ContentIdentityInterfaceFactory $contentIdentityFactory
43-
* @param UpdateContentAssetLinksInterface $processor
51+
* @param GetEntityContentsInterface $getContent
52+
* @param UpdateContentAssetLinksInterface $updateContentAssetLinks
4453
* @param array $fields
4554
*/
4655
public function __construct(
4756
ContentIdentityInterfaceFactory $contentIdentityFactory,
48-
UpdateContentAssetLinksInterface $processor,
57+
GetEntityContentsInterface $getContent,
58+
UpdateContentAssetLinksInterface $updateContentAssetLinks,
4959
array $fields
5060
) {
5161
$this->contentIdentityFactory = $contentIdentityFactory;
52-
$this->processor = $processor;
62+
$this->getContent = $getContent;
63+
$this->updateContentAssetLinks = $updateContentAssetLinks;
5364
$this->fields = $fields;
5465
}
5566

5667
/**
5768
* Retrieve the saved product and pass it to the model processor to save content - asset relations
5869
*
5970
* @param Observer $observer
71+
* @throws \Exception
6072
*/
6173
public function execute(Observer $observer): void
6274
{
6375
$model = $observer->getEvent()->getData('product');
64-
6576
if ($model instanceof CatalogProduct) {
6677
foreach ($this->fields as $field) {
6778
if (!$model->dataHasChangedFor($field)) {
6879
continue;
6980
}
70-
71-
$this->processor->execute(
72-
$this->contentIdentityFactory->create(
73-
[
74-
self::TYPE => self::CONTENT_TYPE,
75-
self::FIELD => $field,
76-
self::ENTITY_ID => (string) $model->getId(),
77-
]
78-
),
79-
(string) $model->getData($field)
81+
$contentIdentity = $this->contentIdentityFactory->create(
82+
[
83+
self::TYPE => self::CONTENT_TYPE,
84+
self::FIELD => $field,
85+
self::ENTITY_ID => (string) $model->getEntityId(),
86+
]
8087
);
88+
$concatenatedContent = implode(PHP_EOL, $this->getContent->execute($contentIdentity));
89+
$this->updateContentAssetLinks->execute($contentIdentity, $concatenatedContent);
8190
}
8291
}
8392
}

app/code/Magento/MediaContentCatalog/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"php": "~7.1.3||~7.2.0||~7.3.0",
66
"magento/module-media-content-api": "*",
77
"magento/module-catalog": "*",
8+
"magento/module-eav": "*",
89
"magento/framework": "*"
910
},
1011
"type": "magento2-module",

app/code/Magento/MediaContentCatalog/etc/di.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<type name="Magento\MediaContentApi\Model\Composite\GetEntityContents">
10+
<arguments>
11+
<argument name="items" xsi:type="array">
12+
<item name="catalog_product" xsi:type="object">Magento\MediaContentCatalog\Model\ResourceModel\GetEntityContent</item>
13+
<item name="catalog_category" xsi:type="object">Magento\MediaContentCatalog\Model\ResourceModel\GetEntityContent</item>
14+
</argument>
15+
</arguments>
16+
</type>
917
<type name="Magento\MediaContentCatalog\Observer\Category">
1018
<arguments>
1119
<argument name="fields" xsi:type="array">

0 commit comments

Comments
 (0)