Skip to content

Commit 24ad802

Browse files
committed
Merge branch '2.3-develop' of github.com:magento/magento2ce into MAGETWO-88054
2 parents 0d7ed5c + 57f4ad2 commit 24ad802

File tree

108 files changed

+3800
-479
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+3800
-479
lines changed

app/code/Magento/Catalog/Api/Data/CategoryInterface.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,37 @@
1414
*/
1515
interface CategoryInterface extends \Magento\Framework\Api\CustomAttributesDataInterface
1616
{
17+
/**#@+
18+
* Constants defined for keys of data array
19+
*/
20+
const KEY_PARENT_ID = 'parent_id';
21+
const KEY_NAME = 'name';
22+
const KEY_IS_ACTIVE = 'is_active';
23+
const KEY_POSITION = 'position';
24+
const KEY_LEVEL = 'level';
25+
const KEY_UPDATED_AT = 'updated_at';
26+
const KEY_CREATED_AT = 'created_at';
27+
const KEY_PATH = 'path';
28+
const KEY_AVAILABLE_SORT_BY = 'available_sort_by';
29+
const KEY_INCLUDE_IN_MENU = 'include_in_menu';
30+
const KEY_PRODUCT_COUNT = 'product_count';
31+
const KEY_CHILDREN_DATA = 'children_data';
32+
33+
const ATTRIBUTES = [
34+
'id',
35+
self::KEY_PARENT_ID,
36+
self::KEY_NAME,
37+
self::KEY_IS_ACTIVE,
38+
self::KEY_POSITION,
39+
self::KEY_LEVEL,
40+
self::KEY_UPDATED_AT,
41+
self::KEY_CREATED_AT,
42+
self::KEY_AVAILABLE_SORT_BY,
43+
self::KEY_INCLUDE_IN_MENU,
44+
self::KEY_CHILDREN_DATA,
45+
];
46+
/**#@-*/
47+
1748
/**
1849
* @return int|null
1950
*/

app/code/Magento/Catalog/Api/Data/ProductInterface.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@ interface ProductInterface extends \Magento\Framework\Api\CustomAttributesDataIn
3636

3737
const UPDATED_AT = 'updated_at';
3838

39+
const MEDIA_GALLERY = 'media_gallery';
40+
41+
const TIER_PRICE = 'tier_price';
42+
43+
const ATTRIBUTES = [
44+
self::SKU,
45+
self::NAME,
46+
self::PRICE,
47+
self::WEIGHT,
48+
self::STATUS,
49+
self::VISIBILITY,
50+
self::ATTRIBUTE_SET_ID,
51+
self::TYPE_ID,
52+
self::CREATED_AT,
53+
self::UPDATED_AT,
54+
self::MEDIA_GALLERY,
55+
self::TIER_PRICE,
56+
];
3957
/**#@-*/
4058

4159
/**

app/code/Magento/Catalog/Block/Product/ImageBuilder.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Catalog\Block\Product;
77

88
use Magento\Catalog\Helper\ImageFactory as HelperFactory;
9+
use Magento\Catalog\Model\Product;
910
use Magento\Catalog\Model\Product\Image\NotLoadInfoImageException;
1011

1112
class ImageBuilder
@@ -21,7 +22,7 @@ class ImageBuilder
2122
protected $helperFactory;
2223

2324
/**
24-
* @var \Magento\Catalog\Model\Product
25+
* @var Product
2526
*/
2627
protected $product;
2728

@@ -50,10 +51,10 @@ public function __construct(
5051
/**
5152
* Set product
5253
*
53-
* @param \Magento\Catalog\Model\Product $product
54+
* @param Product $product
5455
* @return $this
5556
*/
56-
public function setProduct(\Magento\Catalog\Model\Product $product)
57+
public function setProduct(Product $product)
5758
{
5859
$this->product = $product;
5960
return $this;
@@ -79,9 +80,7 @@ public function setImageId($imageId)
7980
*/
8081
public function setAttributes(array $attributes)
8182
{
82-
if ($attributes) {
83-
$this->attributes = $attributes;
84-
}
83+
$this->attributes = $attributes;
8584
return $this;
8685
}
8786

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

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
namespace Magento\Catalog\Model;
77

88
use Magento\Catalog\Api\CategoryRepositoryInterface;
9+
use Magento\Catalog\Api\Data\CategoryInterface;
10+
use Magento\Catalog\Model\Entity\GetCategoryCustomAttributeCodes;
911
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
12+
use Magento\Eav\Model\Entity\GetCustomAttributeCodesInterface;
1013
use Magento\Framework\Api\AttributeValueFactory;
14+
use Magento\Framework\App\ObjectManager;
1115
use Magento\Framework\Convert\ConvertArray;
1216
use Magento\Framework\Exception\NoSuchEntityException;
1317
use Magento\Framework\Profiler;
@@ -69,23 +73,6 @@ class Category extends \Magento\Catalog\Model\AbstractModel implements
6973

7074
const CACHE_TAG = 'cat_c';
7175

72-
/**#@+
73-
* Constants
74-
*/
75-
const KEY_PARENT_ID = 'parent_id';
76-
const KEY_NAME = 'name';
77-
const KEY_IS_ACTIVE = 'is_active';
78-
const KEY_POSITION = 'position';
79-
const KEY_LEVEL = 'level';
80-
const KEY_UPDATED_AT = 'updated_at';
81-
const KEY_CREATED_AT = 'created_at';
82-
const KEY_PATH = 'path';
83-
const KEY_AVAILABLE_SORT_BY = 'available_sort_by';
84-
const KEY_INCLUDE_IN_MENU = 'include_in_menu';
85-
const KEY_PRODUCT_COUNT = 'product_count';
86-
const KEY_CHILDREN_DATA = 'children_data';
87-
/**#@-*/
88-
8976
/**#@-*/
9077
protected $_eventPrefix = 'catalog_category';
9178

@@ -142,21 +129,11 @@ class Category extends \Magento\Catalog\Model\AbstractModel implements
142129
/**
143130
* Attributes are that part of interface
144131
*
132+
* @deprecated
133+
* @see CategoryInterface::ATTRIBUTES
145134
* @var array
146135
*/
147-
protected $interfaceAttributes = [
148-
'id',
149-
self::KEY_PARENT_ID,
150-
self::KEY_NAME,
151-
self::KEY_IS_ACTIVE,
152-
self::KEY_POSITION,
153-
self::KEY_LEVEL,
154-
self::KEY_UPDATED_AT,
155-
self::KEY_CREATED_AT,
156-
self::KEY_AVAILABLE_SORT_BY,
157-
self::KEY_INCLUDE_IN_MENU,
158-
self::KEY_CHILDREN_DATA,
159-
];
136+
protected $interfaceAttributes = CategoryInterface::ATTRIBUTES;
160137

161138
/**
162139
* Category tree model
@@ -230,6 +207,11 @@ class Category extends \Magento\Catalog\Model\AbstractModel implements
230207
*/
231208
protected $metadataService;
232209

210+
/**
211+
* @var GetCustomAttributeCodesInterface
212+
*/
213+
private $getCustomAttributeCodes;
214+
233215
/**
234216
* @param \Magento\Framework\Model\Context $context
235217
* @param \Magento\Framework\Registry $registry
@@ -252,6 +234,7 @@ class Category extends \Magento\Catalog\Model\AbstractModel implements
252234
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
253235
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
254236
* @param array $data
237+
* @param GetCustomAttributeCodesInterface|null $getCustomAttributeCodes
255238
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
256239
*/
257240
public function __construct(
@@ -275,7 +258,8 @@ public function __construct(
275258
CategoryRepositoryInterface $categoryRepository,
276259
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
277260
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
278-
array $data = []
261+
array $data = [],
262+
GetCustomAttributeCodesInterface $getCustomAttributeCodes = null
279263
) {
280264
$this->metadataService = $metadataService;
281265
$this->_treeModel = $categoryTreeResource;
@@ -290,6 +274,9 @@ public function __construct(
290274
$this->urlFinder = $urlFinder;
291275
$this->indexerRegistry = $indexerRegistry;
292276
$this->categoryRepository = $categoryRepository;
277+
$this->getCustomAttributeCodes = $getCustomAttributeCodes ?? ObjectManager::getInstance()->get(
278+
GetCategoryCustomAttributeCodes::class
279+
);
293280
parent::__construct(
294281
$context,
295282
$registry,
@@ -323,11 +310,7 @@ protected function _construct()
323310
*/
324311
protected function getCustomAttributesCodes()
325312
{
326-
if ($this->customAttributesCodes === null) {
327-
$this->customAttributesCodes = $this->getEavAttributesCodes($this->metadataService);
328-
$this->customAttributesCodes = array_diff($this->customAttributesCodes, $this->interfaceAttributes);
329-
}
330-
return $this->customAttributesCodes;
313+
return $this->getCustomAttributeCodes->execute($this->metadataService);
331314
}
332315

333316
/**
@@ -667,9 +650,22 @@ public function getImageUrl($attributeCode = 'image')
667650
$image = $this->getData($attributeCode);
668651
if ($image) {
669652
if (is_string($image)) {
670-
$url = $this->_storeManager->getStore()->getBaseUrl(
653+
$store = $this->_storeManager->getStore();
654+
655+
$isRelativeUrl = substr($image, 0, 1) === '/';
656+
657+
$mediaBaseUrl = $store->getBaseUrl(
671658
\Magento\Framework\UrlInterface::URL_TYPE_MEDIA
672-
) . 'catalog/category/' . $image;
659+
);
660+
661+
if ($isRelativeUrl) {
662+
$url = $image;
663+
} else {
664+
$url = $mediaBaseUrl
665+
. ltrim(\Magento\Catalog\Model\Category\FileInfo::ENTITY_MEDIA_PATH, '/')
666+
. '/'
667+
. $image;
668+
}
673669
} else {
674670
throw new \Magento\Framework\Exception\LocalizedException(
675671
__('Something went wrong while getting the image url.')

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\Catalog\Model\Category\Attribute\Backend;
77

8+
use Magento\Framework\App\Filesystem\DirectoryList;
9+
810
/**
911
* Catalog category image attribute backend model
1012
*
@@ -95,6 +97,11 @@ public function beforeSave($object)
9597
$attributeName = $this->getAttribute()->getName();
9698
$value = $object->getData($attributeName);
9799

100+
if ($this->fileResidesOutsideCategoryDir($value)) {
101+
// use relative path for image attribute so we know it's outside of category dir when we fetch it
102+
$value[0]['name'] = $value[0]['url'];
103+
}
104+
98105
if ($imageName = $this->getUploadedImageName($value)) {
99106
$object->setData($this->additionalData . $attributeName, $value);
100107
$object->setData($attributeName, $imageName);
@@ -131,6 +138,26 @@ private function isTmpFileAvailable($value)
131138
return is_array($value) && isset($value[0]['tmp_name']);
132139
}
133140

141+
/**
142+
* Check for file path resides outside of category media dir. The URL will be a path including pub/media if true
143+
*
144+
* @param array|null $value
145+
* @return bool
146+
*/
147+
private function fileResidesOutsideCategoryDir($value)
148+
{
149+
if (!is_array($value) || !isset($value[0]['url'])) {
150+
return false;
151+
}
152+
153+
$fileUrl = ltrim($value[0]['url'], '/');
154+
$baseMediaDir = $this->_filesystem->getUri(DirectoryList::MEDIA);
155+
156+
$usingPathRelativeToBase = strpos($fileUrl, $baseMediaDir) === 0;
157+
158+
return $usingPathRelativeToBase;
159+
}
160+
134161
/**
135162
* Save uploaded file and set its name to category
136163
*
@@ -148,6 +175,7 @@ public function afterSave($object)
148175
$this->_logger->critical($e);
149176
}
150177
}
178+
151179
return $this;
152180
}
153181
}

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -492,12 +492,20 @@ private function convertValues($category, $categoryData)
492492
unset($categoryData[$attributeCode]);
493493

494494
$fileName = $category->getData($attributeCode);
495-
if ($this->getFileInfo()->isExist($fileName)) {
496-
$stat = $this->getFileInfo()->getStat($fileName);
497-
$mime = $this->getFileInfo()->getMimeType($fileName);
495+
$fileInfo = $this->getFileInfo();
496+
497+
if ($fileInfo->isExist($fileName)) {
498+
$stat = $fileInfo->getStat($fileName);
499+
$mime = $fileInfo->getMimeType($fileName);
500+
501+
$categoryData[$attributeCode][0]['name'] = basename($fileName);
502+
503+
if ($fileInfo->isBeginsWithMediaDirectoryPath($fileName)) {
504+
$categoryData[$attributeCode][0]['url'] = $fileName;
505+
} else {
506+
$categoryData[$attributeCode][0]['url'] = $category->getImageUrl($attributeCode);
507+
}
498508

499-
$categoryData[$attributeCode][0]['name'] = $fileName;
500-
$categoryData[$attributeCode][0]['url'] = $category->getImageUrl($attributeCode);
501509
$categoryData[$attributeCode][0]['size'] = isset($stat) ? $stat['size'] : 0;
502510
$categoryData[$attributeCode][0]['type'] = $mime;
503511
}

0 commit comments

Comments
 (0)