Skip to content

Commit

Permalink
ENGCOM-7709: Introduce observers to track deleted entities #28798
Browse files Browse the repository at this point in the history
 - Merge Pull Request #28798 from Nazar65/magento2:ASI-delete-observers
 - Merged commits:
   1. a2d5113
   2. 3bc8239
   3. 34bff12
  • Loading branch information
magento-engcom-team committed Jun 23, 2020
2 parents 02b87f6 + 34bff12 commit 3b5bab7
Show file tree
Hide file tree
Showing 8 changed files with 521 additions and 0 deletions.
120 changes: 120 additions & 0 deletions app/code/Magento/MediaContentCatalog/Observer/CategoryDelete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaContentCatalog\Observer;

use Magento\Catalog\Model\Category as CatalogCategory;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\MediaContentApi\Api\Data\ContentIdentityInterfaceFactory;
use Magento\MediaContentApi\Api\Data\ContentAssetLinkInterfaceFactory;
use Magento\MediaContentApi\Api\DeleteContentAssetLinksInterface;
use Magento\MediaContentApi\Model\GetEntityContentsInterface;
use Magento\MediaContentApi\Api\ExtractAssetsFromContentInterface;

/**
* Observe the catalog_category_delete_after event and deletes relation between category content and media asset.
*/
class CategoryDelete implements ObserverInterface
{
private const CONTENT_TYPE = 'catalog_category';
private const TYPE = 'entityType';
private const ENTITY_ID = 'entityId';
private const FIELD = 'field';

/**
* @var ContentIdentityInterfaceFactory
*/
private $contentIdentityFactory;

/**
* @var ContentAssetLinkInterfaceFactory
*/
private $contentAssetLinkFactory;

/**
* @var DeleteContentAssetLinksInterface
*/
private $deleteContentAssetLinks;

/**
* @var array
*/
private $fields;

/**
* @var GetEntityContentsInterface
*/
private $getContent;

/**
* @var ExtractAssetsFromContentInterface
*/
private $extractAssetsFromContent;

/**
* @param ExtractAssetsFromContentInterface $extractAssetsFromContent
* @param GetEntityContentsInterface $getContent
* @param DeleteContentAssetLinksInterface $deleteContentAssetLinks
* @param ContentIdentityInterfaceFactory $contentIdentityFactory
* @param ContentAssetLinkInterfaceFactory $contentAssetLinkFactory
* @param array $fields
*/
public function __construct(
ExtractAssetsFromContentInterface $extractAssetsFromContent,
GetEntityContentsInterface $getContent,
DeleteContentAssetLinksInterface $deleteContentAssetLinks,
ContentIdentityInterfaceFactory $contentIdentityFactory,
ContentAssetLinkInterfaceFactory $contentAssetLinkFactory,
array $fields
) {
$this->extractAssetsFromContent = $extractAssetsFromContent;
$this->getContent = $getContent;
$this->deleteContentAssetLinks = $deleteContentAssetLinks;
$this->contentAssetLinkFactory = $contentAssetLinkFactory;
$this->contentIdentityFactory = $contentIdentityFactory;
$this->fields = $fields;
}

/**
* Retrieve the deleted category and remove relation betwen category and asset
*
* @param Observer $observer
* @throws \Exception
*/
public function execute(Observer $observer): void
{
$category = $observer->getEvent()->getData('category');
$contentAssetLinks = [];

if ($category instanceof CatalogCategory) {
foreach ($this->fields as $field) {
$contentIdentity = $this->contentIdentityFactory->create(
[
self::TYPE => self::CONTENT_TYPE,
self::FIELD => $field,
self::ENTITY_ID => (string) $category->getEntityId(),
]
);
$content = implode(PHP_EOL, $this->getContent->execute($contentIdentity));
$assets = $this->extractAssetsFromContent->execute($content);

foreach ($assets as $asset) {
$contentAssetLinks[] = $this->contentAssetLinkFactory->create(
[
'assetId' => $asset->getId(),
'contentIdentity' => $contentIdentity
]
);
}
}
if (!empty($contentAssetLinks)) {
$this->deleteContentAssetLinks->execute($contentAssetLinks);
}
}
}
}
120 changes: 120 additions & 0 deletions app/code/Magento/MediaContentCatalog/Observer/ProductDelete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaContentCatalog\Observer;

use Magento\Catalog\Model\Product as CatalogProduct;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\MediaContentApi\Api\Data\ContentIdentityInterfaceFactory;
use Magento\MediaContentApi\Api\Data\ContentAssetLinkInterfaceFactory;
use Magento\MediaContentApi\Api\DeleteContentAssetLinksInterface;
use Magento\MediaContentApi\Model\GetEntityContentsInterface;
use Magento\MediaContentApi\Api\ExtractAssetsFromContentInterface;

/**
* Observe the catalog_product_delete_before event and deletes relation between category content and media asset.
*/
class ProductDelete implements ObserverInterface
{
private const CONTENT_TYPE = 'catalog_product';
private const TYPE = 'entityType';
private const ENTITY_ID = 'entityId';
private const FIELD = 'field';

/**
* @var ContentIdentityInterfaceFactory
*/
private $contentIdentityFactory;

/**
* @var ContentAssetLinkInterfaceFactory
*/
private $contentAssetLinkFactory;

/**
* @var DeleteContentAssetLinksInterface
*/
private $deleteContentAssetLinks;

/**
* @var array
*/
private $fields;

/**
* @var GetEntityContentsInterface
*/
private $getContent;

/**
* @var ExtractAssetsFromContentInterface
*/
private $extractAssetsFromContent;

/**
* @param ExtractAssetsFromContentInterface $extractAssetsFromContent
* @param GetEntityContentsInterface $getContent
* @param DeleteContentAssetLinksInterface $deleteContentAssetLinks
* @param ContentIdentityInterfaceFactory $contentIdentityFactory
* @param ContentAssetLinkInterfaceFactory $contentAssetLinkFactory
* @param array $fields
*/
public function __construct(
ExtractAssetsFromContentInterface $extractAssetsFromContent,
GetEntityContentsInterface $getContent,
DeleteContentAssetLinksInterface $deleteContentAssetLinks,
ContentIdentityInterfaceFactory $contentIdentityFactory,
ContentAssetLinkInterfaceFactory $contentAssetLinkFactory,
array $fields
) {
$this->extractAssetsFromContent = $extractAssetsFromContent;
$this->getContent = $getContent;
$this->deleteContentAssetLinks = $deleteContentAssetLinks;
$this->contentAssetLinkFactory = $contentAssetLinkFactory;
$this->contentIdentityFactory = $contentIdentityFactory;
$this->fields = $fields;
}

/**
* Retrieve the deleted product and remove relation betwen product and asset
*
* @param Observer $observer
* @throws \Exception
*/
public function execute(Observer $observer): void
{
$product = $observer->getEvent()->getData('product');
$contentAssetLinks = [];

if ($product instanceof CatalogProduct) {
foreach ($this->fields as $field) {
$contentIdentity = $this->contentIdentityFactory->create(
[
self::TYPE => self::CONTENT_TYPE,
self::FIELD => $field,
self::ENTITY_ID => (string) $product->getEntityId(),
]
);
$productContent = implode(PHP_EOL, $this->getContent->execute($contentIdentity));
$assets = $this->extractAssetsFromContent->execute($productContent);

foreach ($assets as $asset) {
$contentAssetLinks[] = $this->contentAssetLinkFactory->create(
[
'assetId' => $asset->getId(),
'contentIdentity' => $contentIdentity
]
);
}
}
if (!empty($contentAssetLinks)) {
$this->deleteContentAssetLinks->execute($contentAssetLinks);
}
}
}
}
16 changes: 16 additions & 0 deletions app/code/Magento/MediaContentCatalog/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@
</argument>
</arguments>
</type>
<type name="Magento\MediaContentCatalog\Observer\ProductDelete">
<arguments>
<argument name="fields" xsi:type="array">
<item name="description" xsi:type="string">description</item>
<item name="short_description" xsi:type="string">short_description</item>
</argument>
</arguments>
</type>
<type name="Magento\MediaContentCatalog\Observer\CategoryDelete">
<arguments>
<argument name="fields" xsi:type="array">
<item name="image" xsi:type="string">image</item>
<item name="description" xsi:type="string">description</item>
</argument>
</arguments>
</type>
<type name="Magento\MediaContentCatalog\Observer\Category">
<arguments>
<argument name="fields" xsi:type="array">
Expand Down
6 changes: 6 additions & 0 deletions app/code/Magento/MediaContentCatalog/etc/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
<event name="catalog_category_save_after">
<observer name="media_content_catalog_category_save_after" instance="Magento\MediaContentCatalog\Observer\Category" />
</event>
<event name="catalog_product_delete_before">
<observer name="media_content_catalog_product_delete_before" instance="Magento\MediaContentCatalog\Observer\ProductDelete" />
</event>
<event name="catalog_category_delete_before">
<observer name="media_content_catalog_category_delete_before" instance="Magento\MediaContentCatalog\Observer\CategoryDelete" />
</event>
<event name="catalog_product_save_after">
<observer name="media_content_catalog_product_save_after" instance="Magento\MediaContentCatalog\Observer\Product" />
</event>
Expand Down
119 changes: 119 additions & 0 deletions app/code/Magento/MediaContentCms/Observer/BlockDelete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaContentCms\Observer;

use Magento\Cms\Model\Block as CmsBlock;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\MediaContentApi\Api\Data\ContentIdentityInterfaceFactory;
use Magento\MediaContentApi\Api\Data\ContentAssetLinkInterfaceFactory;
use Magento\MediaContentApi\Api\DeleteContentAssetLinksInterface;
use Magento\MediaContentApi\Model\GetEntityContentsInterface;
use Magento\MediaContentApi\Api\ExtractAssetsFromContentInterface;

/**
* Observe the adminhtml_cmspage_on_delete event and deletes relation between page content and media asset.
*/
class BlockDelete implements ObserverInterface
{
private const CONTENT_TYPE = 'cms_block';
private const TYPE = 'entityType';
private const ENTITY_ID = 'entityId';
private const FIELD = 'field';

/**
* @var ContentIdentityInterfaceFactory
*/
private $contentIdentityFactory;

/**
* @var ContentAssetLinkInterfaceFactory
*/
private $contentAssetLinkFactory;

/**
* @var DeleteContentAssetLinksInterface
*/
private $deleteContentAssetLinks;

/**
* @var array
*/
private $fields;

/**
* @var GetEntityContentsInterface
*/
private $getContent;

/**
* @var ExtractAssetsFromContentInterface
*/
private $extractAssetsFromContent;

/**
* @param ExtractAssetsFromContentInterface $extractAssetsFromContent
* @param GetEntityContentsInterface $getContent
* @param DeleteContentAssetLinksInterface $deleteContentAssetLinks
* @param ContentIdentityInterfaceFactory $contentIdentityFactory
* @param ContentAssetLinkInterfaceFactory $contentAssetLinkFactory
* @param array $fields
*/
public function __construct(
ExtractAssetsFromContentInterface $extractAssetsFromContent,
GetEntityContentsInterface $getContent,
DeleteContentAssetLinksInterface $deleteContentAssetLinks,
ContentIdentityInterfaceFactory $contentIdentityFactory,
ContentAssetLinkInterfaceFactory $contentAssetLinkFactory,
array $fields
) {
$this->extractAssetsFromContent = $extractAssetsFromContent;
$this->getContent = $getContent;
$this->deleteContentAssetLinks = $deleteContentAssetLinks;
$this->contentAssetLinkFactory = $contentAssetLinkFactory;
$this->contentIdentityFactory = $contentIdentityFactory;
$this->fields = $fields;
}

/**
* Retrieve the deleted category and remove relation betwen category and asset
*
* @param Observer $observer
* @throws \Exception
*/
public function execute(Observer $observer): void
{
$block = $observer->getEvent()->getData('object');
$contentAssetLinks = [];

if ($block instanceof CmsBlock) {
foreach ($this->fields as $field) {
$contentIdentity = $this->contentIdentityFactory->create(
[
self::TYPE => self::CONTENT_TYPE,
self::FIELD => $field,
self::ENTITY_ID => (string) $block->getId(),
]
);
$assets = $this->extractAssetsFromContent->execute((string) $block->getData($field));

foreach ($assets as $asset) {
$contentAssetLinks[] = $this->contentAssetLinkFactory->create(
[
'assetId' => $asset->getId(),
'contentIdentity' => $contentIdentity
]
);
}
}
if (!empty($contentAssetLinks)) {
$this->deleteContentAssetLinks->execute($contentAssetLinks);
}
}
}
}
Loading

0 comments on commit 3b5bab7

Please sign in to comment.