Commit c5482a8d by halweg

feat : 后台图片上传

parent 4bf54fe8
<?php
namespace Joshine\Category\Controller\Adminhtml\Category\Image;
use Magento\Framework\Controller\ResultFactory;
/**
* Class Upload
*/
class Upload extends \Magento\Backend\App\Action
{
protected $baseTmpPath;
protected $imageUploader;
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Catalog\Model\ImageUploader $imageUploader
) {
$this->imageUploader = $imageUploader;
parent::__construct($context);
}
public function execute() {
try {
$result = $this->imageUploader->saveFileToTmpDir('menu_image');
$result['cookie'] = [
'name' => $this->_getSession()->getName(),
'value' => $this->_getSession()->getSessionId(),
'lifetime' => $this->_getSession()->getCookieLifetime(),
'path' => $this->_getSession()->getCookiePath(),
'domain' => $this->_getSession()->getCookieDomain(),
];
} catch (\Exception $e) {
$result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
}
return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($result);
}
}
\ No newline at end of file
<?php
namespace Joshine\Category\Model\Category;
class DataProvider extends \Magento\Catalog\Model\Category\DataProvider
{
protected function getFieldsMap()
{
$fields = parent::getFieldsMap();
$fields['content'][] = 'menu_image'; // custom image field
return $fields;
}
}
\ No newline at end of file
...@@ -42,11 +42,29 @@ class UpgradeData implements UpgradeDataInterface ...@@ -42,11 +42,29 @@ class UpgradeData implements UpgradeDataInterface
$idg = $categorySetup->getAttributeGroupId($entityTypeId, $attributeSetId, 'Custom Menu'); $idg = $categorySetup->getAttributeGroupId($entityTypeId, $attributeSetId, 'Custom Menu');
$categorySetup->addAttribute(
\Magento\Catalog\Model\Category::ENTITY,
'menu_image',
[
'type' => 'varchar',
'label' => 'badge',
'visible_on_front' => true,
'user_defined' => false,
'default' => null,
'group' => 'Custom Menu',
'input' => 'image',
'backend' => 'Magento\Catalog\Model\Category\Attribute\Backend\Image',
'required' => false,
'sort_order' => 70,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
]
);
$categorySetup->addAttributeToGroup( $categorySetup->addAttributeToGroup(
$entityTypeId, $entityTypeId,
$attributeSetId, $attributeSetId,
$idg, $idg,
'badge', 'menu_image',
1 1
); );
} }
......
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Joshine\Category\Controller\Adminhtml\Category\Image\Upload">
<arguments>
<argument name="imageUploader" xsi:type="object">Magento\Catalog\CategoryImageUpload</argument>
</arguments>
</type>
<virtualType name="Magento\Catalog\CategoryImageUpload" type="Magento\Catalog\Model\ImageUploader">
<arguments>
<argument name="baseTmpPath" xsi:type="string">catalog/tmp/category</argument>
<argument name="basePath" xsi:type="string">catalog/category</argument>
<argument name="allowedExtensions" xsi:type="array">
<item name="jpg" xsi:type="string">jpg</item>
<item name="jpeg" xsi:type="string">jpeg</item>
<item name="gif" xsi:type="string">gif</item>
<item name="png" xsi:type="string">png</item>
</argument>
</arguments>
</virtualType>
</config>
\ No newline at end of file
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="admin">
<route id="categorylist" frontName="categorylist">
<module name="Joshine_Category" before="Magento_Backend" />
</route>
</router>
</config>
\ No newline at end of file
...@@ -6,5 +6,5 @@ ...@@ -6,5 +6,5 @@
*/ */
--> -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Joshine_Category" setup_version="1.0.0"/> <module name="Joshine_Category" setup_version="1.0.3"/>
</config> </config>
...@@ -19,6 +19,25 @@ ...@@ -19,6 +19,25 @@
</item> </item>
</argument> </argument>
</field> </field>
<field name="menu_image">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="dataType" xsi:type="string">string</item>
<item name="source" xsi:type="string">category</item>
<item name="label" xsi:type="string" translate="true">Menu Image</item>
<item name="visible" xsi:type="boolean">true</item>
<item name="formElement" xsi:type="string">imageUploader</item>
<item name="elementTmpl" xsi:type="string">ui/form/element/uploader/uploader</item>
<item name="previewTmpl" xsi:type="string">Magento_Catalog/image-preview</item>
<item name="required" xsi:type="boolean">false</item>
<item name="sortOrder" xsi:type="number">40</item>
<item name="uploaderConfig" xsi:type="array">
<item name="url" xsi:type="url" path="categorylist/category_image/upload"/>
</item>
</item>
</argument>
</field>
</fieldset> </fieldset>
</form> </form>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment