Skip to content

Extract images from content only when new media gallery enabled #29492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions app/code/Magento/MediaContentApi/Model/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\MediaContentApi\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;

/**
* Class responsible to provide access to system configuration related to the Media Gallery
*/
class Config
{
/**
* Path to enable/disable media gallery in the system settings.
*/
private const XML_PATH_ENABLED = 'system/media_gallery/enabled';

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* Config constructor.
*
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(ScopeConfigInterface $scopeConfig)
{
$this->scopeConfig = $scopeConfig;
}

/**
* Check if new media gallery enabled
*
* @return bool
*/
public function isEnabled(): bool
{
return $this->scopeConfig->isSetFlag(self::XML_PATH_ENABLED);
}
}
13 changes: 13 additions & 0 deletions app/code/Magento/MediaContentCatalog/Observer/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Magento\MediaContentApi\Api\UpdateContentAssetLinksInterface;
use Magento\MediaContentApi\Api\Data\ContentIdentityInterfaceFactory;
use Magento\MediaContentApi\Model\GetEntityContentsInterface;
use Magento\MediaContentApi\Model\Config;

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

/**
* @var Config
*/
private $config;

/**
* @var array
*/
Expand All @@ -50,17 +56,20 @@ class Category implements ObserverInterface
* @param ContentIdentityInterfaceFactory $contentIdentityFactory
* @param GetEntityContentsInterface $getContent
* @param UpdateContentAssetLinksInterface $updateContentAssetLinks
* @param Config $config
* @param array $fields
*/
public function __construct(
ContentIdentityInterfaceFactory $contentIdentityFactory,
GetEntityContentsInterface $getContent,
UpdateContentAssetLinksInterface $updateContentAssetLinks,
Config $config,
array $fields
) {
$this->contentIdentityFactory = $contentIdentityFactory;
$this->getContent = $getContent;
$this->updateContentAssetLinks = $updateContentAssetLinks;
$this->config = $config;
$this->fields = $fields;
}

Expand All @@ -72,6 +81,10 @@ public function __construct(
*/
public function execute(Observer $observer): void
{
if (!$this->config->isEnabled()) {
return;
}

$model = $observer->getEvent()->getData('category');

if ($model instanceof CatalogCategory) {
Expand Down
19 changes: 16 additions & 3 deletions app/code/Magento/MediaContentCatalog/Observer/CategoryDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Magento\MediaContentApi\Api\DeleteContentAssetLinksInterface;
use Magento\MediaContentApi\Model\GetEntityContentsInterface;
use Magento\MediaContentApi\Api\ExtractAssetsFromContentInterface;
use Magento\MediaContentApi\Model\Config;

/**
* Observe the catalog_category_delete_after event and deletes relation between category content and media asset.
Expand All @@ -25,7 +26,7 @@ class CategoryDelete implements ObserverInterface
private const TYPE = 'entityType';
private const ENTITY_ID = 'entityId';
private const FIELD = 'field';

/**
* @var ContentIdentityInterfaceFactory
*/
Expand All @@ -51,17 +52,23 @@ class CategoryDelete implements ObserverInterface
*/
private $getContent;

/**
* @var Config
*/
private $config;

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

/**
* @param ExtractAssetsFromContentInterface $extractAssetsFromContent
* @param GetEntityContentsInterface $getContent
* @param DeleteContentAssetLinksInterface $deleteContentAssetLinks
* @param ContentIdentityInterfaceFactory $contentIdentityFactory
* @param ContentAssetLinkInterfaceFactory $contentAssetLinkFactory
* @param Config $config
* @param array $fields
*/
public function __construct(
Expand All @@ -70,10 +77,12 @@ public function __construct(
DeleteContentAssetLinksInterface $deleteContentAssetLinks,
ContentIdentityInterfaceFactory $contentIdentityFactory,
ContentAssetLinkInterfaceFactory $contentAssetLinkFactory,
Config $config,
array $fields
) {
$this->extractAssetsFromContent = $extractAssetsFromContent;
$this->getContent = $getContent;
$this->config = $config;
$this->deleteContentAssetLinks = $deleteContentAssetLinks;
$this->contentAssetLinkFactory = $contentAssetLinkFactory;
$this->contentIdentityFactory = $contentIdentityFactory;
Expand All @@ -88,9 +97,13 @@ public function __construct(
*/
public function execute(Observer $observer): void
{
if (!$this->config->isEnabled()) {
return;
}

$category = $observer->getEvent()->getData('category');
$contentAssetLinks = [];

if ($category instanceof CatalogCategory) {
foreach ($this->fields as $field) {
$contentIdentity = $this->contentIdentityFactory->create(
Expand Down
15 changes: 14 additions & 1 deletion app/code/Magento/MediaContentCatalog/Observer/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Magento\MediaContentApi\Api\UpdateContentAssetLinksInterface;
use Magento\MediaContentApi\Api\Data\ContentIdentityInterfaceFactory;
use Magento\MediaContentApi\Model\GetEntityContentsInterface;
use Magento\MediaContentApi\Model\Config;

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

/**
* @var Config
*/
private $config;

/**
* @var ContentIdentityInterfaceFactory
*/
Expand All @@ -45,22 +51,25 @@ class Product implements ObserverInterface
private $getContent;

/**
* * Create links for product content
* Product observer constructor
*
* @param ContentIdentityInterfaceFactory $contentIdentityFactory
* @param GetEntityContentsInterface $getContent
* @param UpdateContentAssetLinksInterface $updateContentAssetLinks
* @param Config $config
* @param array $fields
*/
public function __construct(
ContentIdentityInterfaceFactory $contentIdentityFactory,
GetEntityContentsInterface $getContent,
UpdateContentAssetLinksInterface $updateContentAssetLinks,
Config $config,
array $fields
) {
$this->contentIdentityFactory = $contentIdentityFactory;
$this->getContent = $getContent;
$this->updateContentAssetLinks = $updateContentAssetLinks;
$this->config = $config;
$this->fields = $fields;
}

Expand All @@ -72,6 +81,10 @@ public function __construct(
*/
public function execute(Observer $observer): void
{
if (!$this->config->isEnabled()) {
return;
}

$model = $observer->getEvent()->getData('product');
if ($model instanceof CatalogProduct) {
foreach ($this->fields as $field) {
Expand Down
19 changes: 16 additions & 3 deletions app/code/Magento/MediaContentCatalog/Observer/ProductDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Magento\MediaContentApi\Api\DeleteContentAssetLinksInterface;
use Magento\MediaContentApi\Model\GetEntityContentsInterface;
use Magento\MediaContentApi\Api\ExtractAssetsFromContentInterface;
use Magento\MediaContentApi\Model\Config;

/**
* Observe the catalog_product_delete_before event and deletes relation between category content and media asset.
Expand All @@ -25,7 +26,7 @@ class ProductDelete implements ObserverInterface
private const TYPE = 'entityType';
private const ENTITY_ID = 'entityId';
private const FIELD = 'field';

/**
* @var ContentIdentityInterfaceFactory
*/
Expand All @@ -51,17 +52,23 @@ class ProductDelete implements ObserverInterface
*/
private $getContent;

/**
* @var Config
*/
private $config;

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

/**
* @param ExtractAssetsFromContentInterface $extractAssetsFromContent
* @param GetEntityContentsInterface $getContent
* @param DeleteContentAssetLinksInterface $deleteContentAssetLinks
* @param ContentIdentityInterfaceFactory $contentIdentityFactory
* @param ContentAssetLinkInterfaceFactory $contentAssetLinkFactory
* @param Config $config
* @param array $fields
*/
public function __construct(
Expand All @@ -70,13 +77,15 @@ public function __construct(
DeleteContentAssetLinksInterface $deleteContentAssetLinks,
ContentIdentityInterfaceFactory $contentIdentityFactory,
ContentAssetLinkInterfaceFactory $contentAssetLinkFactory,
Config $config,
array $fields
) {
$this->extractAssetsFromContent = $extractAssetsFromContent;
$this->getContent = $getContent;
$this->deleteContentAssetLinks = $deleteContentAssetLinks;
$this->contentAssetLinkFactory = $contentAssetLinkFactory;
$this->contentIdentityFactory = $contentIdentityFactory;
$this->config = $config;
$this->fields = $fields;
}

Expand All @@ -88,9 +97,13 @@ public function __construct(
*/
public function execute(Observer $observer): void
{
if (!$this->config->isEnabled()) {
return;
}

$product = $observer->getEvent()->getData('product');
$contentAssetLinks = [];

if ($product instanceof CatalogProduct) {
foreach ($this->fields as $field) {
$contentIdentity = $this->contentIdentityFactory->create(
Expand Down
12 changes: 12 additions & 0 deletions app/code/Magento/MediaContentCms/Observer/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Magento\Framework\Event\ObserverInterface;
use Magento\MediaContentApi\Api\UpdateContentAssetLinksInterface;
use Magento\MediaContentApi\Api\Data\ContentIdentityInterfaceFactory;
use Magento\MediaContentApi\Model\Config;

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

/**
* @var Config
*/
private $config;

/**
* @var array
*/
Expand All @@ -41,14 +47,17 @@ class Block implements ObserverInterface
/**
* @param ContentIdentityInterfaceFactory $contentIdentityFactory
* @param UpdateContentAssetLinksInterface $updateContentAssetLinks
* @param Config $config
* @param array $fields
*/
public function __construct(
ContentIdentityInterfaceFactory $contentIdentityFactory,
UpdateContentAssetLinksInterface $updateContentAssetLinks,
Config $config,
array $fields
) {
$this->contentIdentityFactory = $contentIdentityFactory;
$this->config = $config;
$this->updateContentAssetLinks = $updateContentAssetLinks;
$this->fields = $fields;
}
Expand All @@ -60,6 +69,9 @@ public function __construct(
*/
public function execute(Observer $observer): void
{
if (!$this->config->isEnabled()) {
return;
}
$model = $observer->getEvent()->getData('object');

if ($model instanceof CmsBlock) {
Expand Down
Loading