Skip to content

Commit 3c3acd7

Browse files
danmooney2irenelagno
authored andcommitted
MAGETWO-86968: Add MediaGalleryImage Component to Category page
Make imageUploader component
1 parent a86fd3f commit 3c3acd7

File tree

19 files changed

+359
-21
lines changed

19 files changed

+359
-21
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -667,9 +667,22 @@ public function getImageUrl($attributeCode = 'image')
667667
$image = $this->getData($attributeCode);
668668
if ($image) {
669669
if (is_string($image)) {
670-
$url = $this->_storeManager->getStore()->getBaseUrl(
670+
$store = $this->_storeManager->getStore();
671+
672+
$isRelativeUrl = substr($image, 0, 1) === '/';
673+
674+
$mediaBaseUrl = $store->getBaseUrl(
671675
\Magento\Framework\UrlInterface::URL_TYPE_MEDIA
672-
) . 'catalog/category/' . $image;
676+
);
677+
678+
if ($isRelativeUrl) {
679+
$url = $image;
680+
} else {
681+
$url = $mediaBaseUrl
682+
. ltrim(\Magento\Catalog\Model\Category\FileInfo::ENTITY_MEDIA_PATH, '/')
683+
. '/'
684+
. $image;
685+
}
673686
} else {
674687
throw new \Magento\Framework\Exception\LocalizedException(
675688
__('Something went wrong while getting the image url.')

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ class Image extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
4141
*/
4242
protected $_logger;
4343

44+
/**
45+
* @var \Magento\Store\Model\StoreManagerInterface
46+
*/
47+
protected $storeManager;
48+
4449
/**
4550
* @var \Magento\Catalog\Model\ImageUploader
4651
*/
@@ -55,15 +60,19 @@ class Image extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
5560
* @param \Psr\Log\LoggerInterface $logger
5661
* @param \Magento\Framework\Filesystem $filesystem
5762
* @param \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory
63+
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
5864
*/
5965
public function __construct(
6066
\Psr\Log\LoggerInterface $logger,
6167
\Magento\Framework\Filesystem $filesystem,
62-
\Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory
68+
\Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory,
69+
\Magento\Store\Model\StoreManagerInterface $storeManager = null
6370
) {
6471
$this->_filesystem = $filesystem;
6572
$this->_fileUploaderFactory = $fileUploaderFactory;
6673
$this->_logger = $logger;
74+
$this->storeManager = $storeManager ?: \Magento\Framework\App\ObjectManager::getInstance()
75+
->get(\Magento\Store\Model\StoreManager::class);
6776
}
6877

6978
/**
@@ -95,6 +104,11 @@ public function beforeSave($object)
95104
$attributeName = $this->getAttribute()->getName();
96105
$value = $object->getData($attributeName);
97106

107+
if ($this->fileResidesOutsideCategoryDir($value)) {
108+
// use relative path for image attribute so we know it's outside of category dir when we fetch it
109+
$value[0]['name'] = $value[0]['url'];
110+
}
111+
98112
if ($imageName = $this->getUploadedImageName($value)) {
99113
$object->setData($this->additionalData . $attributeName, $value);
100114
$object->setData($attributeName, $imageName);
@@ -131,6 +145,26 @@ private function isTmpFileAvailable($value)
131145
return is_array($value) && isset($value[0]['tmp_name']);
132146
}
133147

148+
/**
149+
* Check for file path resides outside of category media dir. The URL will be a path including pub/media if true
150+
*
151+
* @param array|null $value
152+
* @return bool
153+
*/
154+
private function fileResidesOutsideCategoryDir($value)
155+
{
156+
if (!is_array($value) || !isset($value[0]['url'])) {
157+
return false;
158+
}
159+
160+
$fileUrl = ltrim($value[0]['url'], '/');
161+
$baseMediaDir = $this->storeManager->getStore()->getBaseMediaDir();
162+
163+
$usingPathRelativeToBase = strpos($fileUrl, $baseMediaDir) === 0;
164+
165+
return $usingPathRelativeToBase;
166+
}
167+
134168
/**
135169
* Save uploaded file and set its name to category
136170
*
@@ -148,6 +182,7 @@ public function afterSave($object)
148182
$this->_logger->critical($e);
149183
}
150184
}
185+
151186
return $this;
152187
}
153188
}

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->beginsWithMediaDirectoryPath($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
}

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

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Framework\File\Mime;
1010
use Magento\Framework\Filesystem;
1111
use Magento\Framework\Filesystem\Directory\WriteInterface;
12+
use Magento\Framework\Filesystem\Directory\ReadInterface;
1213

1314
/**
1415
* Class FileInfo
@@ -37,6 +38,11 @@ class FileInfo
3738
*/
3839
private $mediaDirectory;
3940

41+
/**
42+
* @var ReadInterface
43+
*/
44+
private $baseDirectory;
45+
4046
/**
4147
* @param Filesystem $filesystem
4248
* @param Mime $mime
@@ -62,6 +68,20 @@ private function getMediaDirectory()
6268
return $this->mediaDirectory;
6369
}
6470

71+
/**
72+
* Get Base Directory read instance
73+
*
74+
* @return ReadInterface
75+
*/
76+
private function getBaseDirectory()
77+
{
78+
if (!isset($this->baseDirectory)) {
79+
$this->baseDirectory = $this->filesystem->getDirectoryRead(DirectoryList::ROOT);
80+
}
81+
82+
return $this->baseDirectory;
83+
}
84+
6585
/**
6686
* Retrieve MIME type of requested file
6787
*
@@ -70,7 +90,7 @@ private function getMediaDirectory()
7090
*/
7191
public function getMimeType($fileName)
7292
{
73-
$filePath = self::ENTITY_MEDIA_PATH . '/' . ltrim($fileName, '/');
93+
$filePath = $this->getFilePath($fileName);
7494
$absoluteFilePath = $this->getMediaDirectory()->getAbsolutePath($filePath);
7595

7696
$result = $this->mime->getMimeType($absoluteFilePath);
@@ -85,7 +105,7 @@ public function getMimeType($fileName)
85105
*/
86106
public function getStat($fileName)
87107
{
88-
$filePath = self::ENTITY_MEDIA_PATH . '/' . ltrim($fileName, '/');
108+
$filePath = $this->getFilePath($fileName);
89109

90110
$result = $this->getMediaDirectory()->stat($filePath);
91111
return $result;
@@ -99,9 +119,65 @@ public function getStat($fileName)
99119
*/
100120
public function isExist($fileName)
101121
{
102-
$filePath = self::ENTITY_MEDIA_PATH . '/' . ltrim($fileName, '/');
122+
$filePath = $this->getFilePath($fileName);
103123

104124
$result = $this->getMediaDirectory()->isExist($filePath);
105125
return $result;
106126
}
127+
128+
/**
129+
* Construct and return file subpath based on filename relative to media directory
130+
*
131+
* @param string $fileName
132+
* @return string
133+
*/
134+
private function getFilePath($fileName)
135+
{
136+
$filePath = ltrim($fileName, '/');
137+
138+
$mediaDirectoryRelativeSubpath = $this->getMediaDirectoryPathRelativeToBaseDirectoryPath();
139+
$fileNameBeginsWithMediaDirectoryPath = $this->beginsWithMediaDirectoryPath($fileName);
140+
141+
// if the file is not using a relative path, it resides in the catalog/category media directory
142+
$fileIsInCategoryMediaDir = !$fileNameBeginsWithMediaDirectoryPath;
143+
144+
if ($fileIsInCategoryMediaDir) {
145+
$filePath = self::ENTITY_MEDIA_PATH . '/' . $filePath;
146+
} else {
147+
$filePath = substr($filePath, strlen($mediaDirectoryRelativeSubpath));
148+
}
149+
150+
return $filePath;
151+
}
152+
153+
/**
154+
* Checks for whether $fileName string begins with media directory path
155+
*
156+
* @param string $fileName
157+
* @return bool
158+
*/
159+
public function beginsWithMediaDirectoryPath($fileName)
160+
{
161+
$filePath = ltrim($fileName, '/');
162+
163+
$mediaDirectoryRelativeSubpath = $this->getMediaDirectoryPathRelativeToBaseDirectoryPath();
164+
$fileNameBeginsWithMediaDirectoryPath = strpos($filePath, $mediaDirectoryRelativeSubpath) === 0;
165+
166+
return $fileNameBeginsWithMediaDirectoryPath;
167+
}
168+
169+
/**
170+
* Get media directory subpath relative to base directory path
171+
*
172+
* @return string
173+
*/
174+
private function getMediaDirectoryPathRelativeToBaseDirectoryPath()
175+
{
176+
$baseDirectoryPath = $this->getBaseDirectory()->getAbsolutePath();
177+
$mediaDirectoryPath = $this->getMediaDirectory()->getAbsolutePath();
178+
179+
$mediaDirectoryRelativeSubpath = substr($mediaDirectoryPath, strlen($baseDirectoryPath));
180+
181+
return $mediaDirectoryRelativeSubpath;
182+
}
107183
}

app/code/Magento/Catalog/etc/adminhtml/di.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,9 @@
193193
<type name="Magento\Eav\Api\AttributeSetRepositoryInterface">
194194
<plugin name="remove_products" type="Magento\Catalog\Plugin\Model\AttributeSetRepository\RemoveProducts"/>
195195
</type>
196+
<type name="Magento\Catalog\Model\Category\Attribute\Backend\Image">
197+
<arguments>
198+
<argument name="storeManager" xsi:type="object">Magento\Store\Model\StoreManager</argument>
199+
</arguments>
200+
</type>
196201
</config>

app/code/Magento/Catalog/view/adminhtml/ui_component/category_form.xml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,29 +151,30 @@
151151
<collapsible>true</collapsible>
152152
<label translate="true">Content</label>
153153
</settings>
154-
<field name="image" sortOrder="40" formElement="fileUploader">
154+
<field name="image" sortOrder="40" formElement="imageUploader">
155155
<argument name="data" xsi:type="array">
156156
<item name="config" xsi:type="array">
157157
<item name="source" xsi:type="string">category</item>
158158
</item>
159159
</argument>
160160
<settings>
161-
<elementTmpl>ui/form/element/uploader/uploader</elementTmpl>
161+
<elementTmpl>ui/form/element/uploader/image</elementTmpl>
162162
<dataType>string</dataType>
163163
<label translate="true">Category Image</label>
164164
<visible>true</visible>
165165
<required>false</required>
166166
</settings>
167167
<formElements>
168-
<fileUploader>
168+
<imageUploader>
169169
<settings>
170170
<required>false</required>
171171
<uploaderConfig>
172172
<param xsi:type="url" name="url" path="catalog/category_image/upload"/>
173173
</uploaderConfig>
174174
<previewTmpl>Magento_Catalog/image-preview</previewTmpl>
175+
<openDialogTitle>Insert Images...</openDialogTitle> <!-- TODO - not appearing in $_data in \Magento\Ui\Component\Form\Element\DataType\Media\Image::prepare -->
175176
</settings>
176-
</fileUploader>
177+
</imageUploader>
177178
</formElements>
178179
</field>
179180
<field name="description" template="ui/form/field" sortOrder="50" formElement="wysiwyg">

app/code/Magento/Catalog/view/adminhtml/ui_component/design_config_form.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@
2222
<settings>
2323
<notice translate="true">Allowed file types: jpeg, gif, png.</notice>
2424
<label translate="true">Image</label>
25-
<componentType>fileUploader</componentType>
25+
<componentType>imageUploader</componentType>
2626
</settings>
2727
<formElements>
28-
<fileUploader>
28+
<imageUploader>
2929
<settings>
3030
<allowedExtensions>jpg jpeg gif png</allowedExtensions>
3131
<maxFileSize>2097152</maxFileSize>
3232
<uploaderConfig>
3333
<param xsi:type="string" name="url">theme/design_config_fileUploader/save</param>
3434
</uploaderConfig>
3535
</settings>
36-
</fileUploader>
36+
</imageUploader>
3737
</formElements>
3838
</field>
3939
<field name="watermark_image_imageOpacity" formElement="input">

app/code/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images/OnInsert.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,16 @@ public function execute()
4040
$filename = $this->getRequest()->getParam('filename');
4141
$filename = $helper->idDecode($filename);
4242
$asIs = $this->getRequest()->getParam('as_is');
43+
$forceStaticPath = $this->getRequest()->getParam('force_static_path');
4344

4445
$this->_objectManager->get(\Magento\Catalog\Helper\Data::class)->setStoreId($storeId);
4546
$helper->setStoreId($storeId);
4647

47-
$image = $helper->getImageHtmlDeclaration($filename, $asIs);
48+
if ($forceStaticPath) {
49+
$image = parse_url($helper->getCurrentUrl() . $filename, PHP_URL_PATH);
50+
} else {
51+
$image = $helper->getImageHtmlDeclaration($filename, $asIs);
52+
}
4853

4954
/** @var \Magento\Framework\Controller\Result\Raw $resultRaw */
5055
$resultRaw = $this->resultRawFactory->create();

app/code/Magento/Cms/Model/Wysiwyg/Images/Storage.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,8 @@ public function getFilesCollection($path, $type = null)
318318
$item->setName($item->getBasename());
319319
$item->setShortName($this->_cmsWysiwygImages->getShortFilename($item->getBasename()));
320320
$item->setUrl($this->_cmsWysiwygImages->getCurrentUrl() . $item->getBasename());
321+
$item->setSize(filesize($item->getFilename()));
322+
$item->setMimeType(\mime_content_type($item->getFilename()));
321323

322324
if ($this->isImage($item->getBasename())) {
323325
$thumbUrl = $this->getThumbnailUrl($item->getFilename(), true);

app/code/Magento/Cms/view/adminhtml/templates/browser/content/files.phtml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ $_height = $block->getImagesHeight();
1414
?>
1515
<?php if ($block->getFilesCount() > 0): ?>
1616
<?php foreach ($block->getFiles() as $file): ?>
17-
<div data-row="file" class="filecnt" id="<?= $block->escapeHtmlAttr($block->getFileId($file)) ?>">
17+
<div
18+
data-row="file"
19+
class="filecnt"
20+
id="<?= $block->escapeHtmlAttr($block->getFileId($file)) ?>"
21+
data-size="<?= $file->getSize() ?>"
22+
data-mime-type="<?= $file->getMimeType() ?>"
23+
>
1824
<p class="nm" style="height:<?= $block->escapeHtmlAttr($_height) ?>px;width:<?= $block->escapeHtmlAttr($_width) ?>px;">
1925
<?php if ($block->getFileThumbUrl($file)):?>
2026
<img src="<?= $block->escapeHtmlAttr($block->getFileThumbUrl($file)) ?>" alt="<?= $block->escapeHtmlAttr($block->getFileName($file)) ?>"/>

0 commit comments

Comments
 (0)