Skip to content

Commit 46e8da2

Browse files
committed
Merge branch '2.4.1-develop' of https://github.com/magento/magento2ce into PR-08-10-2020
2 parents f2bde5a + 5173b44 commit 46e8da2

File tree

13 files changed

+950
-140
lines changed

13 files changed

+950
-140
lines changed

app/code/Magento/Catalog/Model/Product/Gallery/CreateHandler.php

Lines changed: 94 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
namespace Magento\Catalog\Model\Product\Gallery;
99

1010
use Magento\Catalog\Api\Data\ProductInterface;
11+
use Magento\Catalog\Model\Product;
1112
use Magento\Framework\App\Filesystem\DirectoryList;
1213
use Magento\Framework\App\ObjectManager;
1314
use Magento\Framework\EntityManager\Operation\ExtensionInterface;
1415
use Magento\MediaStorage\Model\File\Uploader as FileUploader;
16+
use Magento\Store\Model\Store;
1517
use Magento\Store\Model\StoreManagerInterface;
1618

1719
/**
@@ -89,6 +91,15 @@ class CreateHandler implements ExtensionInterface
8991
*/
9092
private $storeManager;
9193

94+
/**
95+
* @var string[]
96+
*/
97+
private $mediaAttributesWithLabels = [
98+
'image',
99+
'small_image',
100+
'thumbnail'
101+
];
102+
92103
/**
93104
* @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
94105
* @param \Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository
@@ -190,27 +201,8 @@ public function execute($product, $arguments = [])
190201
$value['duplicate'] = $duplicate;
191202
}
192203

193-
/* @var $mediaAttribute \Magento\Catalog\Api\Data\ProductAttributeInterface */
194-
foreach ($this->getMediaAttributeCodes() as $mediaAttrCode) {
195-
$attrData = $product->getData($mediaAttrCode);
196-
if (empty($attrData) && empty($clearImages) && empty($newImages) && empty($existImages)) {
197-
continue;
198-
}
199-
$this->processMediaAttribute(
200-
$product,
201-
$mediaAttrCode,
202-
$clearImages,
203-
$newImages
204-
);
205-
if (in_array($mediaAttrCode, ['image', 'small_image', 'thumbnail'])) {
206-
$this->processMediaAttributeLabel(
207-
$product,
208-
$mediaAttrCode,
209-
$clearImages,
210-
$newImages,
211-
$existImages
212-
);
213-
}
204+
if (!empty($value['images'])) {
205+
$this->processMediaAttributes($product, $existImages, $newImages, $clearImages);
214206
}
215207

216208
$product->setData($attrCode, $value);
@@ -492,50 +484,59 @@ private function getMediaAttributeCodes()
492484
/**
493485
* Process media attribute
494486
*
495-
* @param \Magento\Catalog\Model\Product $product
487+
* @param Product $product
496488
* @param string $mediaAttrCode
497489
* @param array $clearImages
498490
* @param array $newImages
499491
*/
500492
private function processMediaAttribute(
501-
\Magento\Catalog\Model\Product $product,
502-
$mediaAttrCode,
493+
Product $product,
494+
string $mediaAttrCode,
503495
array $clearImages,
504496
array $newImages
505-
) {
506-
$attrData = $product->getData($mediaAttrCode);
507-
if (in_array($attrData, $clearImages)) {
508-
$product->setData($mediaAttrCode, 'no_selection');
509-
}
510-
511-
if (in_array($attrData, array_keys($newImages))) {
512-
$product->setData($mediaAttrCode, $newImages[$attrData]['new_file']);
513-
}
514-
if (!empty($product->getData($mediaAttrCode))) {
497+
): void {
498+
$storeId = $product->isObjectNew() ? Store::DEFAULT_STORE_ID : (int) $product->getStoreId();
499+
/***
500+
* Attributes values are saved as default value in single store mode
501+
* @see \Magento\Catalog\Model\ResourceModel\AbstractResource::_saveAttributeValue
502+
*/
503+
if ($storeId === Store::DEFAULT_STORE_ID
504+
|| $this->storeManager->hasSingleStore()
505+
|| $this->getMediaAttributeStoreValue($product, $mediaAttrCode, $storeId) !== null
506+
) {
507+
$value = $product->getData($mediaAttrCode);
508+
$newValue = $value;
509+
if (in_array($value, $clearImages)) {
510+
$newValue = 'no_selection';
511+
}
512+
if (in_array($value, array_keys($newImages))) {
513+
$newValue = $newImages[$value]['new_file'];
514+
}
515+
$product->setData($mediaAttrCode, $newValue);
515516
$product->addAttributeUpdate(
516517
$mediaAttrCode,
517-
$product->getData($mediaAttrCode),
518-
$product->getStoreId()
518+
$newValue,
519+
$storeId
519520
);
520521
}
521522
}
522523

523524
/**
524525
* Process media attribute label
525526
*
526-
* @param \Magento\Catalog\Model\Product $product
527+
* @param Product $product
527528
* @param string $mediaAttrCode
528529
* @param array $clearImages
529530
* @param array $newImages
530531
* @param array $existImages
531532
*/
532533
private function processMediaAttributeLabel(
533-
\Magento\Catalog\Model\Product $product,
534-
$mediaAttrCode,
534+
Product $product,
535+
string $mediaAttrCode,
535536
array $clearImages,
536537
array $newImages,
537538
array $existImages
538-
) {
539+
): void {
539540
$resetLabel = false;
540541
$attrData = $product->getData($mediaAttrCode);
541542
if (in_array($attrData, $clearImages)) {
@@ -607,4 +608,57 @@ private function canRemoveImage(ProductInterface $product, string $imageFile) :b
607608

608609
return $canRemoveImage;
609610
}
611+
612+
/**
613+
* Get media attribute value for store view
614+
*
615+
* @param Product $product
616+
* @param string $attributeCode
617+
* @param int|null $storeId
618+
* @return string|null
619+
*/
620+
private function getMediaAttributeStoreValue(Product $product, string $attributeCode, int $storeId = null): ?string
621+
{
622+
$gallery = $this->getImagesForAllStores($product);
623+
$storeId = $storeId === null ? (int) $product->getStoreId() : $storeId;
624+
foreach ($gallery as $image) {
625+
if ($image['attribute_code'] === $attributeCode && ((int)$image['store_id']) === $storeId) {
626+
return $image['filepath'];
627+
}
628+
}
629+
return null;
630+
}
631+
632+
/**
633+
* Update media attributes
634+
*
635+
* @param Product $product
636+
* @param array $existImages
637+
* @param array $newImages
638+
* @param array $clearImages
639+
*/
640+
private function processMediaAttributes(
641+
Product $product,
642+
array $existImages,
643+
array $newImages,
644+
array $clearImages
645+
): void {
646+
foreach ($this->getMediaAttributeCodes() as $mediaAttrCode) {
647+
$this->processMediaAttribute(
648+
$product,
649+
$mediaAttrCode,
650+
$clearImages,
651+
$newImages
652+
);
653+
if (in_array($mediaAttrCode, $this->mediaAttributesWithLabels)) {
654+
$this->processMediaAttributeLabel(
655+
$product,
656+
$mediaAttrCode,
657+
$clearImages,
658+
$newImages,
659+
$existImages
660+
);
661+
}
662+
}
663+
}
610664
}

app/code/Magento/Catalog/Model/ResourceModel/Product/Gallery.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace Magento\Catalog\Model\ResourceModel\Product;
88

9+
use Magento\Catalog\Model\Product\Media\Config;
10+
use Magento\Framework\App\ObjectManager;
911
use Magento\Store\Model\Store;
1012

1113
/**
@@ -31,22 +33,30 @@ class Gallery extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
3133
* @since 101.0.0
3234
*/
3335
protected $metadata;
36+
/**
37+
* @var Config|null
38+
*/
39+
private $mediaConfig;
3440

3541
/**
3642
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
3743
* @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
3844
* @param string $connectionName
45+
* @param Config|null $mediaConfig
46+
* @throws \Exception
3947
*/
4048
public function __construct(
4149
\Magento\Framework\Model\ResourceModel\Db\Context $context,
4250
\Magento\Framework\EntityManager\MetadataPool $metadataPool,
43-
$connectionName = null
51+
$connectionName = null,
52+
?Config $mediaConfig = null
4453
) {
4554
$this->metadata = $metadataPool->getMetadata(
4655
\Magento\Catalog\Api\Data\ProductInterface::class
4756
);
4857

4958
parent::__construct($context, $connectionName);
59+
$this->mediaConfig = $mediaConfig ?? ObjectManager::getInstance()->get(Config::class);
5060
}
5161

5262
/**
@@ -490,7 +500,7 @@ public function getProductImages($product, $storeIds)
490500
$storeIds
491501
)->where(
492502
'attribute_code IN (?)',
493-
['small_image', 'thumbnail', 'image']
503+
$this->mediaConfig->getMediaAttributeCodes()
494504
);
495505

496506
return $this->getConnection()->fetchAll($select);

0 commit comments

Comments
 (0)