Skip to content

Commit 4cb56f5

Browse files
committed
Merge pull request #5 from magento-goinc/MAGETWO-45172
MAGETWO-45172
2 parents 19e2407 + e8c2cf0 commit 4cb56f5

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Product/Save.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,31 @@ class Save extends \Magento\Catalog\Controller\Adminhtml\Product
2626
*/
2727
protected $productTypeManager;
2828

29+
/**
30+
* @var \Magento\Catalog\Api\ProductRepositoryInterface
31+
*/
32+
protected $productRepository;
33+
2934
/**
3035
* @param Action\Context $context
3136
* @param Builder $productBuilder
3237
* @param Initialization\Helper $initializationHelper
3338
* @param \Magento\Catalog\Model\Product\Copier $productCopier
3439
* @param \Magento\Catalog\Model\Product\TypeTransitionManager $productTypeManager
40+
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
3541
*/
3642
public function __construct(
3743
\Magento\Backend\App\Action\Context $context,
3844
Product\Builder $productBuilder,
3945
Initialization\Helper $initializationHelper,
4046
\Magento\Catalog\Model\Product\Copier $productCopier,
41-
\Magento\Catalog\Model\Product\TypeTransitionManager $productTypeManager
47+
\Magento\Catalog\Model\Product\TypeTransitionManager $productTypeManager,
48+
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository
4249
) {
4350
$this->initializationHelper = $initializationHelper;
4451
$this->productCopier = $productCopier;
4552
$this->productTypeManager = $productTypeManager;
53+
$this->productRepository = $productRepository;
4654
parent::__construct($context, $productBuilder);
4755
}
4856

@@ -74,6 +82,7 @@ public function execute()
7482

7583
$originalSku = $product->getSku();
7684
$product->save();
85+
$this->handleImageRemoveError($data, $product->getId());
7786
$productId = $product->getId();
7887
$productAttributeSetId = $product->getAttributeSetId();
7988
$productTypeId = $product->getTypeId();
@@ -147,4 +156,33 @@ public function execute()
147156
}
148157
return $resultRedirect;
149158
}
159+
160+
/**
161+
* Notify customer when image was not deleted in specific case.
162+
* TODO: temporary workaround must be eliminated in MAGETWO-45306
163+
*
164+
* @param array $postData
165+
* @param int $productId
166+
* @return void
167+
*/
168+
private function handleImageRemoveError($postData, $productId)
169+
{
170+
if (isset($postData['product']['media_gallery']['images'])) {
171+
$removedImagesAmount = 0;
172+
foreach ($postData['product']['media_gallery']['images'] as $image) {
173+
if (!empty($image['removed'])) {
174+
$removedImagesAmount++;
175+
}
176+
}
177+
if ($removedImagesAmount) {
178+
$expectedImagesAmount = count($postData['product']['media_gallery']['images']) - $removedImagesAmount;
179+
$product = $this->productRepository->getById($productId);
180+
if ($expectedImagesAmount != count($product->getMediaGallery('images'))) {
181+
$this->messageManager->addNotice(
182+
__('The image cannot be removed as it has been assigned to the other image role')
183+
);
184+
}
185+
}
186+
}
187+
}
150188
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,11 +1505,11 @@ public function getMediaGalleryImages()
15051505
if (!$this->hasData('media_gallery_images') && is_array($this->getMediaGallery('images'))) {
15061506
$images = $this->_collectionFactory->create();
15071507
foreach ($this->getMediaGallery('images') as $image) {
1508-
if (isset($image['disabled']) && $image['disabled']) {
1508+
if ((isset($image['disabled']) && $image['disabled']) || empty($image['value_id'])) {
15091509
continue;
15101510
}
15111511
$image['url'] = $this->getMediaConfig()->getMediaUrl($image['file']);
1512-
$image['id'] = !empty($image['value_id']) ? $image['value_id'] : null;
1512+
$image['id'] = $image['value_id'];
15131513
$image['path'] = $directory->getAbsolutePath($this->getMediaConfig()->getMediaPath($image['file']));
15141514
$images->addItem(new \Magento\Framework\DataObject($image));
15151515
}

0 commit comments

Comments
 (0)