Skip to content

Commit e55c352

Browse files
authored
ENGCOM-7709: Introduce observers to track deleted entities #28798
2 parents 02b87f6 + 3b5bab7 commit e55c352

File tree

8 files changed

+521
-0
lines changed

8 files changed

+521
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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\Observer;
9+
10+
use Magento\Catalog\Model\Category as CatalogCategory;
11+
use Magento\Framework\Event\Observer;
12+
use Magento\Framework\Event\ObserverInterface;
13+
use Magento\MediaContentApi\Api\Data\ContentIdentityInterfaceFactory;
14+
use Magento\MediaContentApi\Api\Data\ContentAssetLinkInterfaceFactory;
15+
use Magento\MediaContentApi\Api\DeleteContentAssetLinksInterface;
16+
use Magento\MediaContentApi\Model\GetEntityContentsInterface;
17+
use Magento\MediaContentApi\Api\ExtractAssetsFromContentInterface;
18+
19+
/**
20+
* Observe the catalog_category_delete_after event and deletes relation between category content and media asset.
21+
*/
22+
class CategoryDelete implements ObserverInterface
23+
{
24+
private const CONTENT_TYPE = 'catalog_category';
25+
private const TYPE = 'entityType';
26+
private const ENTITY_ID = 'entityId';
27+
private const FIELD = 'field';
28+
29+
/**
30+
* @var ContentIdentityInterfaceFactory
31+
*/
32+
private $contentIdentityFactory;
33+
34+
/**
35+
* @var ContentAssetLinkInterfaceFactory
36+
*/
37+
private $contentAssetLinkFactory;
38+
39+
/**
40+
* @var DeleteContentAssetLinksInterface
41+
*/
42+
private $deleteContentAssetLinks;
43+
44+
/**
45+
* @var array
46+
*/
47+
private $fields;
48+
49+
/**
50+
* @var GetEntityContentsInterface
51+
*/
52+
private $getContent;
53+
54+
/**
55+
* @var ExtractAssetsFromContentInterface
56+
*/
57+
private $extractAssetsFromContent;
58+
59+
/**
60+
* @param ExtractAssetsFromContentInterface $extractAssetsFromContent
61+
* @param GetEntityContentsInterface $getContent
62+
* @param DeleteContentAssetLinksInterface $deleteContentAssetLinks
63+
* @param ContentIdentityInterfaceFactory $contentIdentityFactory
64+
* @param ContentAssetLinkInterfaceFactory $contentAssetLinkFactory
65+
* @param array $fields
66+
*/
67+
public function __construct(
68+
ExtractAssetsFromContentInterface $extractAssetsFromContent,
69+
GetEntityContentsInterface $getContent,
70+
DeleteContentAssetLinksInterface $deleteContentAssetLinks,
71+
ContentIdentityInterfaceFactory $contentIdentityFactory,
72+
ContentAssetLinkInterfaceFactory $contentAssetLinkFactory,
73+
array $fields
74+
) {
75+
$this->extractAssetsFromContent = $extractAssetsFromContent;
76+
$this->getContent = $getContent;
77+
$this->deleteContentAssetLinks = $deleteContentAssetLinks;
78+
$this->contentAssetLinkFactory = $contentAssetLinkFactory;
79+
$this->contentIdentityFactory = $contentIdentityFactory;
80+
$this->fields = $fields;
81+
}
82+
83+
/**
84+
* Retrieve the deleted category and remove relation betwen category and asset
85+
*
86+
* @param Observer $observer
87+
* @throws \Exception
88+
*/
89+
public function execute(Observer $observer): void
90+
{
91+
$category = $observer->getEvent()->getData('category');
92+
$contentAssetLinks = [];
93+
94+
if ($category instanceof CatalogCategory) {
95+
foreach ($this->fields as $field) {
96+
$contentIdentity = $this->contentIdentityFactory->create(
97+
[
98+
self::TYPE => self::CONTENT_TYPE,
99+
self::FIELD => $field,
100+
self::ENTITY_ID => (string) $category->getEntityId(),
101+
]
102+
);
103+
$content = implode(PHP_EOL, $this->getContent->execute($contentIdentity));
104+
$assets = $this->extractAssetsFromContent->execute($content);
105+
106+
foreach ($assets as $asset) {
107+
$contentAssetLinks[] = $this->contentAssetLinkFactory->create(
108+
[
109+
'assetId' => $asset->getId(),
110+
'contentIdentity' => $contentIdentity
111+
]
112+
);
113+
}
114+
}
115+
if (!empty($contentAssetLinks)) {
116+
$this->deleteContentAssetLinks->execute($contentAssetLinks);
117+
}
118+
}
119+
}
120+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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\Observer;
9+
10+
use Magento\Catalog\Model\Product as CatalogProduct;
11+
use Magento\Framework\Event\Observer;
12+
use Magento\Framework\Event\ObserverInterface;
13+
use Magento\MediaContentApi\Api\Data\ContentIdentityInterfaceFactory;
14+
use Magento\MediaContentApi\Api\Data\ContentAssetLinkInterfaceFactory;
15+
use Magento\MediaContentApi\Api\DeleteContentAssetLinksInterface;
16+
use Magento\MediaContentApi\Model\GetEntityContentsInterface;
17+
use Magento\MediaContentApi\Api\ExtractAssetsFromContentInterface;
18+
19+
/**
20+
* Observe the catalog_product_delete_before event and deletes relation between category content and media asset.
21+
*/
22+
class ProductDelete implements ObserverInterface
23+
{
24+
private const CONTENT_TYPE = 'catalog_product';
25+
private const TYPE = 'entityType';
26+
private const ENTITY_ID = 'entityId';
27+
private const FIELD = 'field';
28+
29+
/**
30+
* @var ContentIdentityInterfaceFactory
31+
*/
32+
private $contentIdentityFactory;
33+
34+
/**
35+
* @var ContentAssetLinkInterfaceFactory
36+
*/
37+
private $contentAssetLinkFactory;
38+
39+
/**
40+
* @var DeleteContentAssetLinksInterface
41+
*/
42+
private $deleteContentAssetLinks;
43+
44+
/**
45+
* @var array
46+
*/
47+
private $fields;
48+
49+
/**
50+
* @var GetEntityContentsInterface
51+
*/
52+
private $getContent;
53+
54+
/**
55+
* @var ExtractAssetsFromContentInterface
56+
*/
57+
private $extractAssetsFromContent;
58+
59+
/**
60+
* @param ExtractAssetsFromContentInterface $extractAssetsFromContent
61+
* @param GetEntityContentsInterface $getContent
62+
* @param DeleteContentAssetLinksInterface $deleteContentAssetLinks
63+
* @param ContentIdentityInterfaceFactory $contentIdentityFactory
64+
* @param ContentAssetLinkInterfaceFactory $contentAssetLinkFactory
65+
* @param array $fields
66+
*/
67+
public function __construct(
68+
ExtractAssetsFromContentInterface $extractAssetsFromContent,
69+
GetEntityContentsInterface $getContent,
70+
DeleteContentAssetLinksInterface $deleteContentAssetLinks,
71+
ContentIdentityInterfaceFactory $contentIdentityFactory,
72+
ContentAssetLinkInterfaceFactory $contentAssetLinkFactory,
73+
array $fields
74+
) {
75+
$this->extractAssetsFromContent = $extractAssetsFromContent;
76+
$this->getContent = $getContent;
77+
$this->deleteContentAssetLinks = $deleteContentAssetLinks;
78+
$this->contentAssetLinkFactory = $contentAssetLinkFactory;
79+
$this->contentIdentityFactory = $contentIdentityFactory;
80+
$this->fields = $fields;
81+
}
82+
83+
/**
84+
* Retrieve the deleted product and remove relation betwen product and asset
85+
*
86+
* @param Observer $observer
87+
* @throws \Exception
88+
*/
89+
public function execute(Observer $observer): void
90+
{
91+
$product = $observer->getEvent()->getData('product');
92+
$contentAssetLinks = [];
93+
94+
if ($product instanceof CatalogProduct) {
95+
foreach ($this->fields as $field) {
96+
$contentIdentity = $this->contentIdentityFactory->create(
97+
[
98+
self::TYPE => self::CONTENT_TYPE,
99+
self::FIELD => $field,
100+
self::ENTITY_ID => (string) $product->getEntityId(),
101+
]
102+
);
103+
$productContent = implode(PHP_EOL, $this->getContent->execute($contentIdentity));
104+
$assets = $this->extractAssetsFromContent->execute($productContent);
105+
106+
foreach ($assets as $asset) {
107+
$contentAssetLinks[] = $this->contentAssetLinkFactory->create(
108+
[
109+
'assetId' => $asset->getId(),
110+
'contentIdentity' => $contentIdentity
111+
]
112+
);
113+
}
114+
}
115+
if (!empty($contentAssetLinks)) {
116+
$this->deleteContentAssetLinks->execute($contentAssetLinks);
117+
}
118+
}
119+
}
120+
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@
1414
</argument>
1515
</arguments>
1616
</type>
17+
<type name="Magento\MediaContentCatalog\Observer\ProductDelete">
18+
<arguments>
19+
<argument name="fields" xsi:type="array">
20+
<item name="description" xsi:type="string">description</item>
21+
<item name="short_description" xsi:type="string">short_description</item>
22+
</argument>
23+
</arguments>
24+
</type>
25+
<type name="Magento\MediaContentCatalog\Observer\CategoryDelete">
26+
<arguments>
27+
<argument name="fields" xsi:type="array">
28+
<item name="image" xsi:type="string">image</item>
29+
<item name="description" xsi:type="string">description</item>
30+
</argument>
31+
</arguments>
32+
</type>
1733
<type name="Magento\MediaContentCatalog\Observer\Category">
1834
<arguments>
1935
<argument name="fields" xsi:type="array">

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
<event name="catalog_category_save_after">
1010
<observer name="media_content_catalog_category_save_after" instance="Magento\MediaContentCatalog\Observer\Category" />
1111
</event>
12+
<event name="catalog_product_delete_before">
13+
<observer name="media_content_catalog_product_delete_before" instance="Magento\MediaContentCatalog\Observer\ProductDelete" />
14+
</event>
15+
<event name="catalog_category_delete_before">
16+
<observer name="media_content_catalog_category_delete_before" instance="Magento\MediaContentCatalog\Observer\CategoryDelete" />
17+
</event>
1218
<event name="catalog_product_save_after">
1319
<observer name="media_content_catalog_product_save_after" instance="Magento\MediaContentCatalog\Observer\Product" />
1420
</event>
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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\MediaContentCms\Observer;
9+
10+
use Magento\Cms\Model\Block as CmsBlock;
11+
use Magento\Framework\Event\Observer;
12+
use Magento\Framework\Event\ObserverInterface;
13+
use Magento\MediaContentApi\Api\Data\ContentIdentityInterfaceFactory;
14+
use Magento\MediaContentApi\Api\Data\ContentAssetLinkInterfaceFactory;
15+
use Magento\MediaContentApi\Api\DeleteContentAssetLinksInterface;
16+
use Magento\MediaContentApi\Model\GetEntityContentsInterface;
17+
use Magento\MediaContentApi\Api\ExtractAssetsFromContentInterface;
18+
19+
/**
20+
* Observe the adminhtml_cmspage_on_delete event and deletes relation between page content and media asset.
21+
*/
22+
class BlockDelete implements ObserverInterface
23+
{
24+
private const CONTENT_TYPE = 'cms_block';
25+
private const TYPE = 'entityType';
26+
private const ENTITY_ID = 'entityId';
27+
private const FIELD = 'field';
28+
29+
/**
30+
* @var ContentIdentityInterfaceFactory
31+
*/
32+
private $contentIdentityFactory;
33+
34+
/**
35+
* @var ContentAssetLinkInterfaceFactory
36+
*/
37+
private $contentAssetLinkFactory;
38+
39+
/**
40+
* @var DeleteContentAssetLinksInterface
41+
*/
42+
private $deleteContentAssetLinks;
43+
44+
/**
45+
* @var array
46+
*/
47+
private $fields;
48+
49+
/**
50+
* @var GetEntityContentsInterface
51+
*/
52+
private $getContent;
53+
54+
/**
55+
* @var ExtractAssetsFromContentInterface
56+
*/
57+
private $extractAssetsFromContent;
58+
59+
/**
60+
* @param ExtractAssetsFromContentInterface $extractAssetsFromContent
61+
* @param GetEntityContentsInterface $getContent
62+
* @param DeleteContentAssetLinksInterface $deleteContentAssetLinks
63+
* @param ContentIdentityInterfaceFactory $contentIdentityFactory
64+
* @param ContentAssetLinkInterfaceFactory $contentAssetLinkFactory
65+
* @param array $fields
66+
*/
67+
public function __construct(
68+
ExtractAssetsFromContentInterface $extractAssetsFromContent,
69+
GetEntityContentsInterface $getContent,
70+
DeleteContentAssetLinksInterface $deleteContentAssetLinks,
71+
ContentIdentityInterfaceFactory $contentIdentityFactory,
72+
ContentAssetLinkInterfaceFactory $contentAssetLinkFactory,
73+
array $fields
74+
) {
75+
$this->extractAssetsFromContent = $extractAssetsFromContent;
76+
$this->getContent = $getContent;
77+
$this->deleteContentAssetLinks = $deleteContentAssetLinks;
78+
$this->contentAssetLinkFactory = $contentAssetLinkFactory;
79+
$this->contentIdentityFactory = $contentIdentityFactory;
80+
$this->fields = $fields;
81+
}
82+
83+
/**
84+
* Retrieve the deleted category and remove relation betwen category and asset
85+
*
86+
* @param Observer $observer
87+
* @throws \Exception
88+
*/
89+
public function execute(Observer $observer): void
90+
{
91+
$block = $observer->getEvent()->getData('object');
92+
$contentAssetLinks = [];
93+
94+
if ($block instanceof CmsBlock) {
95+
foreach ($this->fields as $field) {
96+
$contentIdentity = $this->contentIdentityFactory->create(
97+
[
98+
self::TYPE => self::CONTENT_TYPE,
99+
self::FIELD => $field,
100+
self::ENTITY_ID => (string) $block->getId(),
101+
]
102+
);
103+
$assets = $this->extractAssetsFromContent->execute((string) $block->getData($field));
104+
105+
foreach ($assets as $asset) {
106+
$contentAssetLinks[] = $this->contentAssetLinkFactory->create(
107+
[
108+
'assetId' => $asset->getId(),
109+
'contentIdentity' => $contentIdentity
110+
]
111+
);
112+
}
113+
}
114+
if (!empty($contentAssetLinks)) {
115+
$this->deleteContentAssetLinks->execute($contentAssetLinks);
116+
}
117+
}
118+
}
119+
}

0 commit comments

Comments
 (0)