Skip to content

Commit a20c7c8

Browse files
created a custom factory for storing and displaying the media gallery entries
1 parent 51aa539 commit a20c7c8

File tree

1 file changed

+51
-3
lines changed

1 file changed

+51
-3
lines changed

app/code/Magento/ConfigurableProduct/Model/LinkManagement.php

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@
66

77
namespace Magento\ConfigurableProduct\Model;
88

9+
use Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterfaceFactory;
10+
use Magento\Catalog\Model\ProductRepository;
911
use Magento\Framework\Exception\InputException;
12+
use Magento\Framework\Exception\LocalizedException;
1013
use Magento\Framework\Exception\NoSuchEntityException;
1114
use Magento\Framework\Exception\StateException;
15+
use Magento\Catalog\Api\Data\ProductInterface;
16+
1217

1318
/**
1419
* Configurable product link management.
@@ -47,6 +52,11 @@ class LinkManagement implements \Magento\ConfigurableProduct\Api\LinkManagementI
4752
*/
4853
private $attributeFactory;
4954

55+
//private $mediaGalleryProcessor;
56+
public \Magento\Catalog\Model\ProductRepository $mediaGallery;
57+
public \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterfaceFactory $myModelFactory;
58+
private array $images;
59+
5060
/**
5161
* Constructor
5262
*
@@ -55,24 +65,47 @@ class LinkManagement implements \Magento\ConfigurableProduct\Api\LinkManagementI
5565
* @param \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $configurableType
5666
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
5767
* @param \Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory $attributeFactory
68+
* @param \Magento\Catalog\Model\ProductRepository $mediaGalleryProcessor
69+
* @param \Magento\Catalog\Api\Data\ProductInterface $productInterface
5870
*/
5971
public function __construct(
6072
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
6173
\Magento\Catalog\Api\Data\ProductInterfaceFactory $productFactory,
6274
\Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $configurableType,
6375
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
64-
\Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory $attributeFactory = null
76+
\Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory $attributeFactory = null,
77+
\Magento\Catalog\Model\ProductRepository $mediaGalleryProcessor,
78+
// \Magento\Catalog\Api\Data\ProductInterface $productInterface,
79+
\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterfaceFactory $myModelFactory
6580
) {
6681
$this->productRepository = $productRepository;
6782
$this->productFactory = $productFactory;
6883
$this->configurableType = $configurableType;
6984
$this->dataObjectHelper = $dataObjectHelper;
7085
$this->attributeFactory = $attributeFactory ?: \Magento\Framework\App\ObjectManager::getInstance()
7186
->get(\Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory::class);
87+
$this->mediaGallery = $mediaGalleryProcessor;
88+
// $this->productInterface=$productInterface;
89+
$this->myModelFactory = $myModelFactory;
7290
}
7391

92+
/**
93+
* Process Media gallery data before save product.
94+
*
95+
* Compare Media Gallery Entries Data with existing Media Gallery
96+
* * If Media entry has not value_id set it as new
97+
* * If Existing entry 'value_id' absent in Media Gallery set 'removed' flag
98+
* * Merge Existing and new media gallery
99+
*
100+
* @param ProductInterface $product contains only existing media gallery items
101+
* @param array $mediaGalleryEntries array which contains all media gallery items
102+
* @throws InputException
103+
* @throws StateException
104+
* @throws LocalizedException
105+
*/
74106
/**
75107
* @inheritdoc
108+
* @throws LocalizedException
76109
*/
77110
public function getChildren($sku)
78111
{
@@ -81,11 +114,9 @@ public function getChildren($sku)
81114
if ($product->getTypeId() != \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) {
82115
return [];
83116
}
84-
85117
/** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable $productTypeInstance */
86118
$productTypeInstance = $product->getTypeInstance();
87119
$productTypeInstance->setStoreFilter($product->getStoreId(), $product);
88-
89120
$childrenList = [];
90121
/** @var \Magento\Catalog\Model\Product $child */
91122
foreach ($productTypeInstance->getUsedProducts($product) as $child) {
@@ -97,7 +128,9 @@ public function getChildren($sku)
97128
$attributes[$attrCode] = $value;
98129
}
99130
}
131+
$images= (array)$child->getMediaGallery('images');
100132
$attributes['store_id'] = $child->getStoreId();
133+
$attributes['media_gallery_entries'] = $this->getMediaEntries($images);
101134
/** @var \Magento\Catalog\Api\Data\ProductInterface $productDataObject */
102135
$productDataObject = $this->productFactory->create();
103136
$this->dataObjectHelper->populateWithArray(
@@ -109,6 +142,21 @@ public function getChildren($sku)
109142
}
110143
return $childrenList;
111144
}
145+
public function getMediaEntries($images)
146+
{
147+
$media = $this->myModelFactory->create();
148+
$mediaGalleryEntries=[];
149+
foreach ($images as $image) {
150+
$media->setId($image["value_id"]);
151+
$media->setMediaType($image["media_type"]);
152+
$media->setLabel($image["label"]);
153+
$media->setPosition($image["position"]);
154+
$media->setDisabled($image["disabled"]);
155+
$media->setFile($image["file"]);
156+
$mediaGalleryEntries[]=$media->getData();
157+
}
158+
return $mediaGalleryEntries;
159+
}
112160

113161
/**
114162
* @inheritdoc

0 commit comments

Comments
 (0)