Skip to content

Commit a2d5113

Browse files
committed
Introduce observers to track deleted entities
1 parent c43d763 commit a2d5113

File tree

8 files changed

+518
-0
lines changed

8 files changed

+518
-0
lines changed
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\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 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+
*/
66+
public function __construct(
67+
ExtractAssetsFromContentInterface $extractAssetsFromContent,
68+
GetEntityContentsInterface $getContent,
69+
DeleteContentAssetLinksInterface $deleteContentAssetLinks,
70+
ContentIdentityInterfaceFactory $contentIdentityFactory,
71+
ContentAssetLinkInterfaceFactory $contentAssetLinkFactory,
72+
array $fields
73+
) {
74+
$this->extractAssetsFromContent = $extractAssetsFromContent;
75+
$this->getContent = $getContent;
76+
$this->deleteContentAssetLinks = $deleteContentAssetLinks;
77+
$this->contentAssetLinkFactory = $contentAssetLinkFactory;
78+
$this->contentIdentityFactory = $contentIdentityFactory;
79+
$this->fields = $fields;
80+
}
81+
82+
/**
83+
* Retrieve the deleted category and remove relation betwen category and asset
84+
*
85+
* @param Observer $observer
86+
* @throws \Exception
87+
*/
88+
public function execute(Observer $observer): void
89+
{
90+
$model = $observer->getEvent()->getData('category');
91+
$contentAssetsLinks = [];
92+
93+
if ($model instanceof CatalogCategory) {
94+
foreach ($this->fields as $field) {
95+
$contentIdentity = $this->contentIdentityFactory->create(
96+
[
97+
self::TYPE => self::CONTENT_TYPE,
98+
self::FIELD => $field,
99+
self::ENTITY_ID => (string) $model->getEntityId(),
100+
]
101+
);
102+
$content = implode(PHP_EOL, $this->getContent->execute($contentIdentity));
103+
$assets = $this->extractAssetsFromContent->execute($content);
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+
}
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\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+
*/
66+
public function __construct(
67+
ExtractAssetsFromContentInterface $extractAssetsFromContent,
68+
GetEntityContentsInterface $getContent,
69+
DeleteContentAssetLinksInterface $deleteContentAssetLinks,
70+
ContentIdentityInterfaceFactory $contentIdentityFactory,
71+
ContentAssetLinkInterfaceFactory $contentAssetLinkFactory,
72+
array $fields
73+
) {
74+
$this->extractAssetsFromContent = $extractAssetsFromContent;
75+
$this->getContent = $getContent;
76+
$this->deleteContentAssetLinks = $deleteContentAssetLinks;
77+
$this->contentAssetLinkFactory = $contentAssetLinkFactory;
78+
$this->contentIdentityFactory = $contentIdentityFactory;
79+
$this->fields = $fields;
80+
}
81+
82+
/**
83+
* Retrieve the deleted product and remove relation betwen product and asset
84+
*
85+
* @param Observer $observer
86+
* @throws \Exception
87+
*/
88+
public function execute(Observer $observer): void
89+
{
90+
$model = $observer->getEvent()->getData('product');
91+
$contentAssetsLinks = [];
92+
93+
if ($model instanceof CatalogProduct) {
94+
foreach ($this->fields as $field) {
95+
$contentIdentity = $this->contentIdentityFactory->create(
96+
[
97+
self::TYPE => self::CONTENT_TYPE,
98+
self::FIELD => $field,
99+
self::ENTITY_ID => (string) $model->getEntityId(),
100+
]
101+
);
102+
$content = implode(PHP_EOL, $this->getContent->execute($contentIdentity));
103+
$assets = $this->extractAssetsFromContent->execute($content);
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+
}

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+
*/
66+
public function __construct(
67+
ExtractAssetsFromContentInterface $extractAssetsFromContent,
68+
GetEntityContentsInterface $getContent,
69+
DeleteContentAssetLinksInterface $deleteContentAssetLinks,
70+
ContentIdentityInterfaceFactory $contentIdentityFactory,
71+
ContentAssetLinkInterfaceFactory $contentAssetLinkFactory,
72+
array $fields
73+
) {
74+
$this->extractAssetsFromContent = $extractAssetsFromContent;
75+
$this->getContent = $getContent;
76+
$this->deleteContentAssetLinks = $deleteContentAssetLinks;
77+
$this->contentAssetLinkFactory = $contentAssetLinkFactory;
78+
$this->contentIdentityFactory = $contentIdentityFactory;
79+
$this->fields = $fields;
80+
}
81+
82+
/**
83+
* Retrieve the deleted category and remove relation betwen category and asset
84+
*
85+
* @param Observer $observer
86+
* @throws \Exception
87+
*/
88+
public function execute(Observer $observer): void
89+
{
90+
$model = $observer->getEvent()->getData('object');
91+
$contentAssetsLinks = [];
92+
93+
if ($model instanceof CmsBlock) {
94+
foreach ($this->fields as $field) {
95+
$contentIdentity = $this->contentIdentityFactory->create(
96+
[
97+
self::TYPE => self::CONTENT_TYPE,
98+
self::FIELD => $field,
99+
self::ENTITY_ID => (string) $model->getId(),
100+
]
101+
);
102+
103+
$assets = $this->extractAssetsFromContent->execute((string) $model->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)