Skip to content

Commit 6997871

Browse files
authored
Merge pull request #13 from magento-nord/NORD-PR
[NORD] P1, P2 bugfixes
2 parents 491ca43 + 7490c9d commit 6997871

File tree

32 files changed

+1489
-509
lines changed

32 files changed

+1489
-509
lines changed

app/code/Magento/Bundle/Block/Catalog/Product/View/Type/Bundle.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,14 @@ public function __construct(
7878
}
7979

8080
/**
81+
* Returns the bundle product options
82+
* Will return cached options data if the product options are already initialized
83+
* In a case when $stripSelection parameter is true will reload stored bundle selections collection from DB
84+
*
85+
* @param bool $stripSelection
8186
* @return array
8287
*/
83-
public function getOptions()
88+
public function getOptions($stripSelection = false)
8489
{
8590
if (!$this->options) {
8691
$product = $this->getProduct();
@@ -96,7 +101,7 @@ public function getOptions()
96101

97102
$this->options = $optionCollection->appendSelections(
98103
$selectionCollection,
99-
false,
104+
$stripSelection,
100105
$this->catalogProduct->getSkipSaleableCheck()
101106
);
102107
}

app/code/Magento/Bundle/Test/Unit/Block/Catalog/Product/View/Type/BundleTest.php

Lines changed: 159 additions & 143 deletions
Large diffs are not rendered by default.

app/code/Magento/Bundle/view/adminhtml/templates/product/composite/fieldset/options/bundle.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
?>
1010

1111
<?php /* @var $block \Magento\Bundle\Block\Adminhtml\Catalog\Product\Composite\Fieldset\Bundle */ ?>
12-
<?php $options = $block->decorateArray($block->getOptions()); ?>
12+
<?php $options = $block->decorateArray($block->getOptions(true)); ?>
1313
<?php if (count($options)): ?>
1414
<fieldset id="catalog_product_composite_configure_fields_bundle"
1515
class="fieldset admin__fieldset composite-bundle<?php echo $block->getIsLastFieldset() ? ' last-fieldset' : '' ?>">

app/code/Magento/Catalog/Controller/Adminhtml/Category/Image/Upload.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ protected function _isAllowed()
5050
*/
5151
public function execute()
5252
{
53+
$imageId = $this->_request->getParam('param_name', 'image');
54+
5355
try {
54-
$result = $this->imageUploader->saveFileToTmpDir('image');
56+
$result = $this->imageUploader->saveFileToTmpDir($imageId);
5557

5658
$result['cookie'] = [
5759
'name' => $this->_getSession()->getName(),

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

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Catalog\Controller\Adminhtml\Category;
77

88
use Magento\Store\Model\StoreManagerInterface;
9+
use Magento\Catalog\Api\Data\CategoryAttributeInterface;
910

1011
/**
1112
* Class Save
@@ -48,6 +49,11 @@ class Save extends \Magento\Catalog\Controller\Adminhtml\Category
4849
*/
4950
private $storeManager;
5051

52+
/**
53+
* @var \Magento\Eav\Model\Config
54+
*/
55+
private $eavConfig;
56+
5157
/**
5258
* Constructor
5359
*
@@ -56,31 +62,35 @@ class Save extends \Magento\Catalog\Controller\Adminhtml\Category
5662
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
5763
* @param \Magento\Framework\View\LayoutFactory $layoutFactory
5864
* @param StoreManagerInterface $storeManager
65+
* @param \Magento\Eav\Model\Config $eavConfig
5966
*/
6067
public function __construct(
6168
\Magento\Backend\App\Action\Context $context,
6269
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
6370
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
6471
\Magento\Framework\View\LayoutFactory $layoutFactory,
65-
StoreManagerInterface $storeManager
72+
StoreManagerInterface $storeManager,
73+
\Magento\Eav\Model\Config $eavConfig = null
6674
) {
6775
parent::__construct($context);
6876
$this->resultRawFactory = $resultRawFactory;
6977
$this->resultJsonFactory = $resultJsonFactory;
7078
$this->layoutFactory = $layoutFactory;
7179
$this->storeManager = $storeManager;
80+
$this->eavConfig = $eavConfig
81+
?: \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Eav\Model\Config::class);
7282
}
7383

7484
/**
7585
* Filter category data
7686
*
87+
* @deprecated
7788
* @param array $rawData
7889
* @return array
7990
*/
8091
protected function _filterCategoryPostData(array $rawData)
8192
{
8293
$data = $rawData;
83-
// @todo It is a workaround to prevent saving this data in category model and it has to be refactored in future
8494
if (isset($data['image']) && is_array($data['image'])) {
8595
if (!empty($data['image']['delete'])) {
8696
$data['image'] = null;
@@ -126,7 +136,7 @@ public function execute()
126136
$this->storeManager->setCurrentStore($store->getCode());
127137
$parentId = isset($categoryPostData['parent']) ? $categoryPostData['parent'] : null;
128138
if ($categoryPostData) {
129-
$category->addData($this->_filterCategoryPostData($categoryPostData));
139+
$category->addData($categoryPostData);
130140
if ($isNewCategory) {
131141
$parentCategory = $this->getParentCategory($parentId, $storeId);
132142
$category->setPath($parentCategory->getPath());
@@ -248,18 +258,30 @@ public function execute()
248258
}
249259

250260
/**
251-
* Image data preprocessing
261+
* Sets image attribute data to false if image was removed
252262
*
253263
* @param array $data
254-
*
255264
* @return array
256265
*/
257266
public function imagePreprocessing($data)
258267
{
259-
if (empty($data['image'])) {
260-
unset($data['image']);
261-
$data['image']['delete'] = true;
268+
$entityType = $this->eavConfig->getEntityType(CategoryAttributeInterface::ENTITY_TYPE_CODE);
269+
270+
foreach ($entityType->getAttributeCollection() as $attributeModel) {
271+
$attributeCode = $attributeModel->getAttributeCode();
272+
$backendModel = $attributeModel->getBackend();
273+
274+
if (isset($data[$attributeCode])) {
275+
continue;
276+
}
277+
278+
if (!$backendModel instanceof \Magento\Catalog\Model\Category\Attribute\Backend\Image) {
279+
continue;
280+
}
281+
282+
$data[$attributeCode] = false;
262283
}
284+
263285
return $data;
264286
}
265287

app/code/Magento/Catalog/Controller/Adminhtml/Product/Initialization/Helper.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,7 @@ public function initializeFromData(\Magento\Catalog\Model\Product $product, arra
131131
$productData[$field] = [];
132132
}
133133
}
134-
135-
foreach ($productData['website_ids'] as $websiteId => $checkboxValue) {
136-
if (!$checkboxValue) {
137-
unset($productData['website_ids'][$websiteId]);
138-
}
139-
}
134+
$productData['website_ids'] = $this->filterWebsiteIds($productData['website_ids']);
140135

141136
$wasLockedMedia = false;
142137
if ($product->isLockedAttribute('media')) {
@@ -422,4 +417,23 @@ private function getDateTimeFilter()
422417
}
423418
return $this->dateTimeFilter;
424419
}
420+
421+
/**
422+
* Remove ids of non selected websites from $websiteIds array and return filtered data
423+
* $websiteIds parameter expects array with website ids as keys and 1 (selected) or 0 (non selected) as values
424+
* Only one id (default website ID) will be set to $websiteIds array when the single store mode is turned on
425+
*
426+
* @param array $websiteIds
427+
* @return array
428+
*/
429+
private function filterWebsiteIds($websiteIds)
430+
{
431+
if (!$this->storeManager->isSingleStoreMode()) {
432+
$websiteIds = array_filter((array)$websiteIds);
433+
} else {
434+
$websiteIds[$this->storeManager->getWebsite(true)->getId()] = 1;
435+
}
436+
437+
return $websiteIds;
438+
}
425439
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -652,14 +652,14 @@ public function formatUrlKey($str)
652652
}
653653

654654
/**
655-
* Retrieve image URL
656-
*
657-
* @return string
655+
* @param string $attributeCode
656+
* @return bool|string
657+
* @throws \Magento\Framework\Exception\LocalizedException
658658
*/
659-
public function getImageUrl()
659+
public function getImageUrl($attributeCode = 'image')
660660
{
661661
$url = false;
662-
$image = $this->getImage();
662+
$image = $this->getData($attributeCode);
663663
if ($image) {
664664
if (is_string($image)) {
665665
$url = $this->_storeManager->getStore()->getBaseUrl(

app/code/Magento/Catalog/Model/Category/Attribute/Backend/Image.php

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,13 @@ class Image extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
2121
protected $_uploaderFactory;
2222

2323
/**
24-
* Filesystem facade
25-
*
2624
* @var \Magento\Framework\Filesystem
2725
*
2826
* @deprecated
2927
*/
3028
protected $_filesystem;
3129

3230
/**
33-
* File Uploader factory
34-
*
3531
* @var \Magento\MediaStorage\Model\File\UploaderFactory
3632
*
3733
* @deprecated
@@ -46,15 +42,16 @@ class Image extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
4642
protected $_logger;
4743

4844
/**
49-
* Image uploader
50-
*
5145
* @var \Magento\Catalog\Model\ImageUploader
5246
*/
5347
private $imageUploader;
5448

5549
/**
56-
* Image constructor.
57-
*
50+
* @var string
51+
*/
52+
private $additionalData = '_additional_data_';
53+
54+
/**
5855
* @param \Psr\Log\LoggerInterface $logger
5956
* @param \Magento\Framework\Filesystem $filesystem
6057
* @param \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory
@@ -70,19 +67,55 @@ public function __construct(
7067
}
7168

7269
/**
73-
* Get image uploader
70+
* Gets image name from $value array.
71+
* Will return empty string in a case when $value is not an array
72+
*
73+
* @param array $value Attribute value
74+
* @return string
75+
*/
76+
private function getUploadedImageName($value)
77+
{
78+
if (is_array($value) && isset($value[0]['name'])) {
79+
return $value[0]['name'];
80+
}
81+
82+
return '';
83+
}
84+
85+
/**
86+
* Avoiding saving potential upload data to DB
87+
* Will set empty image attribute value if image was not uploaded
7488
*
89+
* @param \Magento\Framework\DataObject $object
90+
* @return $this
91+
*/
92+
public function beforeSave($object)
93+
{
94+
$attributeName = $this->getAttribute()->getName();
95+
$value = $object->getData($attributeName);
96+
97+
if ($imageName = $this->getUploadedImageName($value)) {
98+
$object->setData($this->additionalData . $attributeName, $value);
99+
$object->setData($attributeName, $imageName);
100+
} else if (!is_string($value)) {
101+
$object->setData($attributeName, '');
102+
}
103+
104+
return parent::beforeSave($object);
105+
}
106+
107+
/**
75108
* @return \Magento\Catalog\Model\ImageUploader
76109
*
77110
* @deprecated
78111
*/
79112
private function getImageUploader()
80113
{
81114
if ($this->imageUploader === null) {
82-
$this->imageUploader = \Magento\Framework\App\ObjectManager::getInstance()->get(
83-
\Magento\Catalog\CategoryImageUpload::class
84-
);
115+
$this->imageUploader = \Magento\Framework\App\ObjectManager::getInstance()
116+
->get(\Magento\Catalog\CategoryImageUpload::class);
85117
}
118+
86119
return $this->imageUploader;
87120
}
88121

@@ -94,15 +127,16 @@ private function getImageUploader()
94127
*/
95128
public function afterSave($object)
96129
{
97-
$image = $object->getData($this->getAttribute()->getName(), null);
130+
$value = $object->getData($this->additionalData . $this->getAttribute()->getName());
98131

99-
if ($image !== null) {
132+
if ($imageName = $this->getUploadedImageName($value)) {
100133
try {
101-
$this->getImageUploader()->moveFileFromTmp($image);
134+
$this->getImageUploader()->moveFileFromTmp($imageName);
102135
} catch (\Exception $e) {
103136
$this->_logger->critical($e);
104137
}
105138
}
139+
106140
return $this;
107141
}
108142
}

app/code/Magento/Catalog/Model/Category/DataProvider.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Magento\Ui\DataProvider\EavValidationRules;
1818
use Magento\Catalog\Model\CategoryFactory;
1919
use Magento\Framework\Exception\NoSuchEntityException;
20+
use Magento\Catalog\Model\Category\Attribute\Backend\Image as ImageBackendModel;
2021

2122
/**
2223
* Class DataProvider
@@ -206,11 +207,8 @@ public function getData()
206207
$categoryData = $this->addUseDefaultSettings($category, $categoryData);
207208
$categoryData = $this->addUseConfigSettings($categoryData);
208209
$categoryData = $this->filterFields($categoryData);
209-
if (isset($categoryData['image'])) {
210-
unset($categoryData['image']);
211-
$categoryData['image'][0]['name'] = $category->getData('image');
212-
$categoryData['image'][0]['url'] = $category->getImageUrl();
213-
}
210+
$categoryData = $this->convertValues($category, $categoryData);
211+
214212
$this->loadedData[$category->getId()] = $categoryData;
215213
}
216214
return $this->loadedData;
@@ -371,6 +369,31 @@ protected function filterFields($categoryData)
371369
return array_diff_key($categoryData, array_flip($this->ignoreFields));
372370
}
373371

372+
/**
373+
* Converts category image data to acceptable for rendering format
374+
*
375+
* @param \Magento\Catalog\Model\Category $category
376+
* @param array $categoryData
377+
* @return array
378+
*/
379+
private function convertValues($category, $categoryData)
380+
{
381+
foreach ($category->getAttributes() as $attributeCode => $attribute) {
382+
if (!isset($categoryData[$attributeCode])) {
383+
continue;
384+
}
385+
386+
if ($attribute->getBackend() instanceof ImageBackendModel) {
387+
unset($categoryData[$attributeCode]);
388+
389+
$categoryData[$attributeCode][0]['name'] = $category->getData($attributeCode);
390+
$categoryData[$attributeCode][0]['url'] = $category->getImageUrl($attributeCode);
391+
}
392+
}
393+
394+
return $categoryData;
395+
}
396+
374397
/**
375398
* Category's fields default values
376399
*

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2182,12 +2182,17 @@ public function addMediaGalleryData()
21822182

21832183
$mediaGalleries = [];
21842184
$linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();
2185+
$items = $this->getItems();
2186+
2187+
$select->where('entity.' . $linkField . ' IN (?)', array_map(function ($item) {
2188+
return $item->getId();
2189+
}, $items));
21852190

21862191
foreach ($this->getConnection()->fetchAll($select) as $row) {
21872192
$mediaGalleries[$row[$linkField]][] = $row;
21882193
}
21892194

2190-
foreach ($this->getItems() as $item) {
2195+
foreach ($items as $item) {
21912196
$mediaEntries = isset($mediaGalleries[$item->getId()]) ? $mediaGalleries[$item->getId()] : [];
21922197
$this->getGalleryReadHandler()->addMediaDataToProduct($item, $mediaEntries);
21932198
}

0 commit comments

Comments
 (0)