Skip to content

Commit 8420c37

Browse files
committed
Extract images from content only when new media gallery enabled magento/adobe-stock-integration#1750
1 parent 6d15bb1 commit 8420c37

File tree

9 files changed

+166
-16
lines changed

9 files changed

+166
-16
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\MediaContentApi\Model;
10+
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
13+
/**
14+
* Class responsible to provide access to system configuration related to the Media Gallery
15+
*/
16+
class Config
17+
{
18+
/**
19+
* Path to enable/disable media gallery in the system settings.
20+
*/
21+
private const XML_PATH_ENABLED = 'system/media_gallery/enabled';
22+
23+
/**
24+
* @var ScopeConfigInterface
25+
*/
26+
private $scopeConfig;
27+
28+
/**
29+
* Config constructor.
30+
*
31+
* @param ScopeConfigInterface $scopeConfig
32+
*/
33+
public function __construct(ScopeConfigInterface $scopeConfig)
34+
{
35+
$this->scopeConfig = $scopeConfig;
36+
}
37+
38+
/**
39+
* Check if new media gallery emabled
40+
*
41+
* @return bool
42+
*/
43+
public function isEnabled(): bool
44+
{
45+
return $this->scopeConfig->isSetFlag(self::XML_PATH_ENABLED);
46+
}
47+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\MediaContentApi\Api\UpdateContentAssetLinksInterface;
1414
use Magento\MediaContentApi\Api\Data\ContentIdentityInterfaceFactory;
1515
use Magento\MediaContentApi\Model\GetEntityContentsInterface;
16+
use Magento\MediaContentApi\Model\Config;
1617

1718
/**
1819
* Observe the catalog_category_save_after event and run processing relation between category content and media asset.
@@ -29,6 +30,11 @@ class Category implements ObserverInterface
2930
*/
3031
private $updateContentAssetLinks;
3132

33+
/**
34+
* @var Config
35+
*/
36+
private $config;
37+
3238
/**
3339
* @var array
3440
*/
@@ -50,17 +56,20 @@ class Category implements ObserverInterface
5056
* @param ContentIdentityInterfaceFactory $contentIdentityFactory
5157
* @param GetEntityContentsInterface $getContent
5258
* @param UpdateContentAssetLinksInterface $updateContentAssetLinks
59+
* @param Config $config
5360
* @param array $fields
5461
*/
5562
public function __construct(
5663
ContentIdentityInterfaceFactory $contentIdentityFactory,
5764
GetEntityContentsInterface $getContent,
5865
UpdateContentAssetLinksInterface $updateContentAssetLinks,
66+
Config $config,
5967
array $fields
6068
) {
6169
$this->contentIdentityFactory = $contentIdentityFactory;
6270
$this->getContent = $getContent;
6371
$this->updateContentAssetLinks = $updateContentAssetLinks;
72+
$this->config = $config;
6473
$this->fields = $fields;
6574
}
6675

@@ -72,6 +81,10 @@ public function __construct(
7281
*/
7382
public function execute(Observer $observer): void
7483
{
84+
if (!$this->config->isEnabled()) {
85+
return;
86+
}
87+
7588
$model = $observer->getEvent()->getData('category');
7689

7790
if ($model instanceof CatalogCategory) {

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Magento\MediaContentApi\Api\DeleteContentAssetLinksInterface;
1616
use Magento\MediaContentApi\Model\GetEntityContentsInterface;
1717
use Magento\MediaContentApi\Api\ExtractAssetsFromContentInterface;
18+
use Magento\MediaContentApi\Model\Config;
1819

1920
/**
2021
* Observe the catalog_category_delete_after event and deletes relation between category content and media asset.
@@ -25,7 +26,7 @@ class CategoryDelete implements ObserverInterface
2526
private const TYPE = 'entityType';
2627
private const ENTITY_ID = 'entityId';
2728
private const FIELD = 'field';
28-
29+
2930
/**
3031
* @var ContentIdentityInterfaceFactory
3132
*/
@@ -51,17 +52,23 @@ class CategoryDelete implements ObserverInterface
5152
*/
5253
private $getContent;
5354

55+
/**
56+
* @var Config
57+
*/
58+
private $config;
59+
5460
/**
5561
* @var ExtractAssetsFromContentInterface
5662
*/
5763
private $extractAssetsFromContent;
58-
64+
5965
/**
6066
* @param ExtractAssetsFromContentInterface $extractAssetsFromContent
6167
* @param GetEntityContentsInterface $getContent
6268
* @param DeleteContentAssetLinksInterface $deleteContentAssetLinks
6369
* @param ContentIdentityInterfaceFactory $contentIdentityFactory
6470
* @param ContentAssetLinkInterfaceFactory $contentAssetLinkFactory
71+
* @param Config $config
6572
* @param array $fields
6673
*/
6774
public function __construct(
@@ -70,13 +77,15 @@ public function __construct(
7077
DeleteContentAssetLinksInterface $deleteContentAssetLinks,
7178
ContentIdentityInterfaceFactory $contentIdentityFactory,
7279
ContentAssetLinkInterfaceFactory $contentAssetLinkFactory,
80+
Config $config,
7381
array $fields
7482
) {
7583
$this->extractAssetsFromContent = $extractAssetsFromContent;
7684
$this->getContent = $getContent;
7785
$this->deleteContentAssetLinks = $deleteContentAssetLinks;
7886
$this->contentAssetLinkFactory = $contentAssetLinkFactory;
7987
$this->contentIdentityFactory = $contentIdentityFactory;
88+
$this->config = $config;
8089
$this->fields = $fields;
8190
}
8291

@@ -88,9 +97,13 @@ public function __construct(
8897
*/
8998
public function execute(Observer $observer): void
9099
{
100+
if (!$this->config->isEnabled()) {
101+
return;
102+
}
103+
91104
$category = $observer->getEvent()->getData('category');
92105
$contentAssetLinks = [];
93-
106+
94107
if ($category instanceof CatalogCategory) {
95108
foreach ($this->fields as $field) {
96109
$contentIdentity = $this->contentIdentityFactory->create(

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\MediaContentApi\Api\UpdateContentAssetLinksInterface;
1414
use Magento\MediaContentApi\Api\Data\ContentIdentityInterfaceFactory;
1515
use Magento\MediaContentApi\Model\GetEntityContentsInterface;
16+
use Magento\MediaContentApi\Model\Config;
1617

1718
/**
1819
* Observe the catalog_product_save_after event and run processing relation between product content and media asset
@@ -34,6 +35,11 @@ class Product implements ObserverInterface
3435
*/
3536
private $fields;
3637

38+
/**
39+
* @var Config
40+
*/
41+
private $config;
42+
3743
/**
3844
* @var ContentIdentityInterfaceFactory
3945
*/
@@ -45,22 +51,25 @@ class Product implements ObserverInterface
4551
private $getContent;
4652

4753
/**
48-
* * Create links for product content
54+
* Product observer constructor
4955
*
5056
* @param ContentIdentityInterfaceFactory $contentIdentityFactory
5157
* @param GetEntityContentsInterface $getContent
5258
* @param UpdateContentAssetLinksInterface $updateContentAssetLinks
59+
* @param Config $config
5360
* @param array $fields
5461
*/
5562
public function __construct(
5663
ContentIdentityInterfaceFactory $contentIdentityFactory,
5764
GetEntityContentsInterface $getContent,
5865
UpdateContentAssetLinksInterface $updateContentAssetLinks,
66+
Config $config,
5967
array $fields
6068
) {
6169
$this->contentIdentityFactory = $contentIdentityFactory;
6270
$this->getContent = $getContent;
6371
$this->updateContentAssetLinks = $updateContentAssetLinks;
72+
$this->config = $config;
6473
$this->fields = $fields;
6574
}
6675

@@ -72,6 +81,10 @@ public function __construct(
7281
*/
7382
public function execute(Observer $observer): void
7483
{
84+
if (!$this->config->isEnabled()) {
85+
return;
86+
}
87+
7588
$model = $observer->getEvent()->getData('product');
7689
if ($model instanceof CatalogProduct) {
7790
foreach ($this->fields as $field) {

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Magento\MediaContentApi\Api\DeleteContentAssetLinksInterface;
1616
use Magento\MediaContentApi\Model\GetEntityContentsInterface;
1717
use Magento\MediaContentApi\Api\ExtractAssetsFromContentInterface;
18+
use Magento\MediaContentApi\Model\Config;
1819

1920
/**
2021
* Observe the catalog_product_delete_before event and deletes relation between category content and media asset.
@@ -25,7 +26,7 @@ class ProductDelete implements ObserverInterface
2526
private const TYPE = 'entityType';
2627
private const ENTITY_ID = 'entityId';
2728
private const FIELD = 'field';
28-
29+
2930
/**
3031
* @var ContentIdentityInterfaceFactory
3132
*/
@@ -51,17 +52,23 @@ class ProductDelete implements ObserverInterface
5152
*/
5253
private $getContent;
5354

55+
/**
56+
* @var Config
57+
*/
58+
private $config;
59+
5460
/**
5561
* @var ExtractAssetsFromContentInterface
5662
*/
5763
private $extractAssetsFromContent;
58-
64+
5965
/**
6066
* @param ExtractAssetsFromContentInterface $extractAssetsFromContent
6167
* @param GetEntityContentsInterface $getContent
6268
* @param DeleteContentAssetLinksInterface $deleteContentAssetLinks
6369
* @param ContentIdentityInterfaceFactory $contentIdentityFactory
6470
* @param ContentAssetLinkInterfaceFactory $contentAssetLinkFactory
71+
* @param Config $config
6572
* @param array $fields
6673
*/
6774
public function __construct(
@@ -70,13 +77,15 @@ public function __construct(
7077
DeleteContentAssetLinksInterface $deleteContentAssetLinks,
7178
ContentIdentityInterfaceFactory $contentIdentityFactory,
7279
ContentAssetLinkInterfaceFactory $contentAssetLinkFactory,
80+
Config $config,
7381
array $fields
7482
) {
7583
$this->extractAssetsFromContent = $extractAssetsFromContent;
7684
$this->getContent = $getContent;
7785
$this->deleteContentAssetLinks = $deleteContentAssetLinks;
7886
$this->contentAssetLinkFactory = $contentAssetLinkFactory;
7987
$this->contentIdentityFactory = $contentIdentityFactory;
88+
$this->config = $config;
8089
$this->fields = $fields;
8190
}
8291

@@ -88,9 +97,13 @@ public function __construct(
8897
*/
8998
public function execute(Observer $observer): void
9099
{
100+
if (!$this->config->isEnabled()) {
101+
return;
102+
}
103+
91104
$product = $observer->getEvent()->getData('product');
92105
$contentAssetLinks = [];
93-
106+
94107
if ($product instanceof CatalogProduct) {
95108
foreach ($this->fields as $field) {
96109
$contentIdentity = $this->contentIdentityFactory->create(

app/code/Magento/MediaContentCms/Observer/Block.php

Lines changed: 12 additions & 0 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\Config;
1516

1617
/**
1718
* Observe cms_block_save_after event and run processing relation between cms block content and media asset
@@ -28,6 +29,11 @@ class Block implements ObserverInterface
2829
*/
2930
private $updateContentAssetLinks;
3031

32+
/**
33+
* @var Config
34+
*/
35+
private $config;
36+
3137
/**
3238
* @var array
3339
*/
@@ -41,15 +47,18 @@ class Block implements ObserverInterface
4147
/**
4248
* @param ContentIdentityInterfaceFactory $contentIdentityFactory
4349
* @param UpdateContentAssetLinksInterface $updateContentAssetLinks
50+
* @param Config $config
4451
* @param array $fields
4552
*/
4653
public function __construct(
4754
ContentIdentityInterfaceFactory $contentIdentityFactory,
4855
UpdateContentAssetLinksInterface $updateContentAssetLinks,
56+
Config $config,
4957
array $fields
5058
) {
5159
$this->contentIdentityFactory = $contentIdentityFactory;
5260
$this->updateContentAssetLinks = $updateContentAssetLinks;
61+
$this->config = $config;
5362
$this->fields = $fields;
5463
}
5564

@@ -60,6 +69,9 @@ public function __construct(
6069
*/
6170
public function execute(Observer $observer): void
6271
{
72+
if (!$this->config->isEnabled()) {
73+
return;
74+
}
6375
$model = $observer->getEvent()->getData('object');
6476

6577
if ($model instanceof CmsBlock) {

0 commit comments

Comments
 (0)