Skip to content

Commit

Permalink
Merge pull request #2206 from magento-trigger/MAGETWO-87986-Media-Fol…
Browse files Browse the repository at this point in the history
…der-Structure-CMS-Images

Fix Media Folder Structure for CMS Images
  • Loading branch information
irenelagno authored Mar 20, 2018
2 parents 28916b5 + e6f3e6c commit 620d37d
Show file tree
Hide file tree
Showing 33 changed files with 145 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
</uploaderConfig>
<previewTmpl>Magento_Catalog/image-preview</previewTmpl>
<openDialogTitle>Media Gallery</openDialogTitle>
<initialMediaGalleryOpenSubpath>catalog/category</initialMediaGalleryOpenSubpath>
<allowedExtensions>jpg jpeg gif png</allowedExtensions>
<maxFileSize>4194304</maxFileSize>
</settings>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public function getContentsUrl()
{
return $this->getUrl('cms/*/contents', [
'type' => $this->getRequest()->getParam('type'),
'use_storage_root' => (int) $this->getRequest()->getParam('use_storage_root'),
]);
}

Expand Down Expand Up @@ -143,9 +142,7 @@ public function getNewfolderUrl()
*/
protected function getDeletefolderUrl()
{
return $this->getUrl('cms/*/deleteFolder', [
'use_storage_root' => (int) $this->getRequest()->getParam('use_storage_root'),
]);
return $this->getUrl('cms/*/deleteFolder');
}

/**
Expand All @@ -165,9 +162,7 @@ public function getDeleteFilesUrl()
*/
public function getOnInsertUrl()
{
return $this->getUrl('cms/*/onInsert', [
'use_storage_root' => (int) $this->getRequest()->getParam('use_storage_root'),
]);
return $this->getUrl('cms/*/onInsert');
}

/**
Expand Down
20 changes: 18 additions & 2 deletions app/code/Magento/Cms/Block/Adminhtml/Wysiwyg/Images/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,17 @@ public function getTreeJson()
*/
public function getTreeLoaderUrl()
{
$params = [];

$currentTreePath = $this->getRequest()->getParam('current_tree_path');

if (strlen($currentTreePath)) {
$params['current_tree_path'] = $currentTreePath;
}

return $this->getUrl(
'cms/*/treeJson',
['use_storage_root' => (int) $this->getRequest()->getParam('use_storage_root')]
$params
);
}

Expand All @@ -119,7 +127,14 @@ public function getRootNodeName()
public function getTreeCurrentPath()
{
$treePath = ['root'];
if ($path = $this->_coreRegistry->registry('storage')->getSession()->getCurrentPath()) {

if ($idEncodedPath = $this->getRequest()->getParam('current_tree_path')) {
$path = $this->_cmsWysiwygImages->idDecode($idEncodedPath);
} else {
$path = $this->_coreRegistry->registry('storage')->getSession()->getCurrentPath();
}

if (strlen($path)) {
$path = str_replace($this->_cmsWysiwygImages->getStorageRoot(), '', $path);
$relative = [];
foreach (explode('/', $path) as $dirName) {
Expand All @@ -129,6 +144,7 @@ public function getTreeCurrentPath()
}
}
}

return $treePath;
}

Expand Down
4 changes: 1 addition & 3 deletions app/code/Magento/Cms/Helper/Wysiwyg/Images.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ public function getStorageRoot()
*/
public function getStorageRootSubpath()
{
return $this->_getRequest()->getParam('use_storage_root')
? ''
: \Magento\Cms\Model\Wysiwyg\Config::IMAGE_DIRECTORY;
return '';
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,37 @@ class DefaultConfigProvider implements \Magento\Framework\Data\Wysiwyg\ConfigPro
*/
private $backendUrl;

/**
* @var \Magento\Cms\Helper\Wysiwyg\Images
*/
private $imagesHelper;

/**
* @var array
*/
private $windowSize;

/**
* @var string|null
*/
private $currentTreePath;

/**
* @param \Magento\Backend\Model\UrlInterface $backendUrl
* @param \Magento\Cms\Helper\Wysiwyg\Images $imagesHelper
* @param array $windowSize
* @param string|null $currentTreePath
*/
public function __construct(\Magento\Backend\Model\UrlInterface $backendUrl, array $windowSize = [])
{
public function __construct(
\Magento\Backend\Model\UrlInterface $backendUrl,
\Magento\Cms\Helper\Wysiwyg\Images $imagesHelper,
array $windowSize = [],
$currentTreePath = null
) {
$this->backendUrl = $backendUrl;
$this->imagesHelper = $imagesHelper;
$this->windowSize = $windowSize;
$this->currentTreePath = $currentTreePath;
}

/**
Expand All @@ -39,10 +57,22 @@ public function getConfig($config)
'name' => 'image',
]
];

$fileBrowserUrlParams = [];

if (is_string($this->currentTreePath)) {
$fileBrowserUrlParams = [
'current_tree_path' => $this->imagesHelper->idEncode($this->currentTreePath),
];
}

return $config->addData(
[
'add_images' => true,
'files_browser_window_url' => $this->backendUrl->getUrl('cms/wysiwyg_images/index'),
'files_browser_window_url' => $this->backendUrl->getUrl(
'cms/wysiwyg_images/index',
$fileBrowserUrlParams
),
'files_browser_window_width' => $this->windowSize['width'],
'files_browser_window_height' => $this->windowSize['height'],
'plugins' => array_merge($pluginData, $imageData)
Expand Down
32 changes: 16 additions & 16 deletions app/code/Magento/Cms/Test/Unit/Helper/Wysiwyg/ImagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class ImagesTest extends \PHPUnit\Framework\TestCase

protected function setUp()
{
$this->path = 'PATH/';
$this->path = 'PATH';
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);

$this->eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
Expand Down Expand Up @@ -111,6 +111,7 @@ protected function setUp()
[
[WysiwygConfig::IMAGE_DIRECTORY, null, $this->getAbsolutePath(WysiwygConfig::IMAGE_DIRECTORY)],
[null, null, $this->getAbsolutePath(null)],
['', null, $this->getAbsolutePath('')],
]
);

Expand Down Expand Up @@ -179,7 +180,7 @@ public function testSetStoreId()
public function testGetStorageRoot()
{
$this->assertEquals(
$this->getAbsolutePath(WysiwygConfig::IMAGE_DIRECTORY),
$this->getAbsolutePath(''),
$this->imagesHelper->getStorageRoot()
);
}
Expand All @@ -203,7 +204,7 @@ public function testGetTreeNodeName()
public function testConvertPathToId()
{
$pathOne = '/test_path';
$pathTwo = $this->getAbsolutePath(WysiwygConfig::IMAGE_DIRECTORY) . '/test_path';
$pathTwo = $this->getAbsolutePath('') . '/test_path';
$this->assertEquals(
$this->imagesHelper->convertPathToId($pathOne),
$this->imagesHelper->convertPathToId($pathTwo)
Expand Down Expand Up @@ -345,26 +346,25 @@ public function testGetCurrentPath($pathId, $expectedPath, $isExist)
->willReturnMap(
[
['node', null, $pathId],
['use_storage_root', null, false],
]
);

$this->directoryWriteMock->expects($this->any())
->method('isDirectory')
->willReturnMap(
[
['/../wysiwyg/test_path', true],
['/../wysiwyg/my.jpg', false],
['/../wysiwyg', true],
['/../test_path', true],
['/../my.jpg', false],
['.', true],
]
);
$this->directoryWriteMock->expects($this->any())
->method('getRelativePath')
->willReturnMap(
[
['PATH/wysiwyg/test_path', '/../wysiwyg/test_path'],
['PATH/wysiwyg/my.jpg', '/../wysiwyg/my.jpg'],
['PATH/wysiwyg', '/../wysiwyg'],
['PATH/test_path', '/../test_path'],
['PATH/my.jpg', '/../my.jpg'],
['PATH', '.'],
]
);
$this->directoryWriteMock->expects($this->once())
Expand Down Expand Up @@ -401,12 +401,12 @@ public function testGetCurrentPathThrowException()
public function providerGetCurrentPath()
{
return [
['L3Rlc3RfcGF0aA--', 'PATH/wysiwyg/test_path', true],
['L215LmpwZw--', 'PATH/wysiwyg', true],
[null, 'PATH/wysiwyg', true],
['L3Rlc3RfcGF0aA--', 'PATH/wysiwyg/test_path', false],
['L215LmpwZw--', 'PATH/wysiwyg', false],
[null, 'PATH/wysiwyg', false],
['L3Rlc3RfcGF0aA--', 'PATH/test_path', true],
['L215LmpwZw--', 'PATH', true],
[null, 'PATH', true],
['L3Rlc3RfcGF0aA--', 'PATH/test_path', false],
['L215LmpwZw--', 'PATH', false],
[null, 'PATH', false],
];
}

Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/Cms/etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
</type>
<type name="Magento\Cms\Model\Wysiwyg\Gallery\DefaultConfigProvider">
<arguments>
<argument name="currentTreePath" xsi:type="const">\Magento\Cms\Model\Wysiwyg\Config::IMAGE_DIRECTORY</argument>
<argument name="windowSize" xsi:type="array">
<item name="height" xsi:type="number">600</item>
<item name="width" xsi:type="number">1000</item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Ui\Component\Form\Element\DataType\Media;

use Magento\Store\Model\StoreManagerInterface;
Expand Down Expand Up @@ -71,6 +74,7 @@ public function prepare()
'mediaGallery' => [
'openDialogUrl' => $this->getContext()->getUrl('cms/wysiwyg_images/index'),
'openDialogTitle' => $this->getConfiguration()['openDialogTitle'] ?? __('Insert Images...'),
'initialOpenSubpath' => $this->getConfiguration()['initialMediaGalleryOpenSubpath'],
'storeId' => $this->storeManager->getStore()->getId(),
],
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,9 @@
<item name="openDialogTitle" type="string" xsi:type="xpath">
formElements/*[name(.)=../../@formElement]/settings/openDialogTitle
</item>
<item name="initialMediaGalleryOpenSubpath" type="string" xsi:type="xpath">
formElements/*[name(.)=../../@formElement]/settings/initialMediaGalleryOpenSubpath
</item>
<item name="customEntry" type="string" xsi:type="xpath">
formElements/*[name(.)=../../@formElement]/settings/customEntry
</item>
Expand Down Expand Up @@ -427,6 +430,7 @@
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="openDialogTitle" type="string" translate="true" xsi:type="xpath">settings/openDialogTitle</item>
<item name="initialMediaGalleryOpenSubpath" type="string" xsi:type="xpath">settings/initialMediaGalleryOpenSubpath</item>
</item>
</argument>
</schema>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@
<multiline class="Magento\Ui\Component\Form\Element\Multiline" component="Magento_Ui/js/form/components/group"/>
<range class="Magento\Ui\Component\Form\Element\Range" component="Magento_Ui/js/grid/filters/range"/>
<fileUploader class="Magento\Ui\Component\Form\Element\DataType\Media" component="Magento_Ui/js/form/element/file-uploader" template="ui/form/element/uploader/uploader"/>
<imageUploader class="Magento\Ui\Component\Form\Element\DataType\Media\Image" component="Magento_Ui/js/form/element/image-uploader" template="ui/form/element/uploader/image"/>
<imageUploader class="Magento\Ui\Component\Form\Element\DataType\Media\Image" component="Magento_Ui/js/form/element/image-uploader" template="ui/form/element/uploader/image">
<settings>
<initialMediaGalleryOpenSubpath>wysiwyg</initialMediaGalleryOpenSubpath>
</settings>
</imageUploader>
<!-- Form elements -->

<!-- Form element data types -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="initialMediaGalleryOpenSubpath" type="xs:string">
<xs:annotation>
<xs:documentation>
Defines the initial subpath relative to root that will be open when opening media gallery
</xs:documentation>
</xs:annotation>
</xs:element>
</xs:choice>
</xs:group>
</xs:schema>
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
* See COPYING.txt for license details.
*/

/* global Base64 */
define([
'jquery',
'underscore',
'mageUtils',
'Magento_Ui/js/modal/alert',
'Magento_Ui/js/lib/validation/validator',
'Magento_Ui/js/form/element/file-uploader',
'mage/adminhtml/browser'
'mage/adminhtml/browser',
'mage/adminhtml/tools'
], function ($, _, utils, uiAlert, validator, Element, browser) {
'use strict';

Expand Down Expand Up @@ -55,7 +57,7 @@ define([
},

/**
* Open the media browser dialog using the
* Open the media browser dialog
*
* @param {ImageUploader} imageUploader - UI Class
* @param {Event} e
Expand All @@ -65,7 +67,11 @@ define([
openDialogUrl = this.mediaGallery.openDialogUrl +
'target_element_id/' + $buttonEl.attr('id') +
'/store/' + this.mediaGallery.storeId +
'/type/image/use_storage_root/1?isAjax=true';
'/type/image/?isAjax=true';

if (this.mediaGallery.initialOpenSubpath) {
openDialogUrl += '&current_tree_path=' + Base64.mageEncode(this.mediaGallery.initialOpenSubpath);
}

browser.openDialog(openDialogUrl, null, null, this.mediaGallery.openDialogTitle);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
xsi:noNamespaceSchemaLocation="../../../../../../vendor/magento/magento2-functional-testing-framework/src/Magento/FunctionalTestingFramework/Test/etc/testSchema.xsd">
<test name="EndToEndB2CAdminTest">
<!--Create Bundle Product-->
<amOnPage url="{{AdminProductIndexPage}}" stepKey="visitAdminProductPageBundle" after="seeSimpleProductInGrid"/>
<amOnPage url="{{AdminProductIndexPage.url}}" stepKey="visitAdminProductPageBundle" after="seeSimpleProductInGrid"/>
<waitForPageLoad stepKey="waitForProductPageLoadBundle" after="visitAdminProductPageBundle"/>
<actionGroup ref="goToCreateProductPage" stepKey="goToCreateBundleProduct" after="waitForProductPageLoadBundle">
<argument name="product" value="BundleProduct"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<arguments>
<argument name="categoryEntity" defaultValue="_defaultCategory"/>
</arguments>
<seeInCurrentUrl url="{{AdminCategoryPage}}" stepKey="seeOnCategoryPage"/>
<seeInCurrentUrl url="{{AdminCategoryPage.url}}" stepKey="seeOnCategoryPage"/>
<click selector="{{AdminCategorySidebarActionSection.AddSubcategoryButton}}" stepKey="clickOnAddSubCategory"/>
<see selector="{{AdminHeaderSection.pageTitle}}" userInput="New Category" stepKey="seeCategoryPageTitle"/>
<fillField selector="{{AdminCategoryBasicFieldSection.CategoryNameInput}}" userInput="{{categoryEntity.name}}" stepKey="enterCategoryName"/>
Expand Down Expand Up @@ -41,7 +41,7 @@
<arguments>
<argument name="categoryEntity" defaultValue="_defaultCategory"/>
</arguments>
<amOnPage url="{{AdminCategoryPage}}" stepKey="goToCategoryPage"/>
<amOnPage url="{{AdminCategoryPage.url}}" stepKey="goToCategoryPage"/>
<waitForPageLoad time="60" stepKey="waitForCategoryPageLoad"/>
<click selector="{{AdminCategorySidebarTreeSection.categoryInTree(categoryEntity.name)}}" stepKey="clickCategoryLink"/>
<click selector="{{AdminCategoryMainActionsSection.DeleteButton}}" stepKey="clickDelete"/>
Expand All @@ -56,7 +56,7 @@

<!--Check that name field is required-->
<actionGroup name="CheckCategoryNameIsRequiredField">
<seeInCurrentUrl url="{{AdminCategoryPage}}" stepKey="seeOnCategoryPage"/>
<seeInCurrentUrl url="{{AdminCategoryPage.url}}" stepKey="seeOnCategoryPage"/>
<click selector="{{AdminCategorySidebarActionSection.AddSubcategoryButton}}" stepKey="clickOnAddSubCategory"/>
<clearField selector="{{AdminCategoryBasicFieldSection.CategoryNameInput}}" stepKey="makeNameFieldEmpty"/>
<click selector="{{AdminCategoryMainActionsSection.SaveButton}}" stepKey="saveCategory"/>
Expand Down
Loading

0 comments on commit 620d37d

Please sign in to comment.