magento 2 how to upload images and videos in custom module
Finally i got the answer
I refer this module and implement in my custom module
Find below useful code :
following below steps
1) create layout.xml
[vendor]/[module]/operation/view/adminhtml/layout/layout_edit.xml
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<uiComponent name="sample_form"/>
</referenceContainer>
<referenceContainer name="sample_form">
<block name="gallery" class="[vendor]\[module]\Block\Adminhtml\Grid\Helper\Form\Gallery">
<arguments>
<argument name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Images</item>
<item name="collapsible" xsi:type="boolean">true</item>
<item name="opened" xsi:type="boolean">false</item>
<item name="sortOrder" xsi:type="string">22</item>
<item name="canShow" xsi:type="boolean">true</item>
<item name="componentType" xsi:type="string">fieldset</item>
</argument>
</arguments>
<block class="[vendor]\[module]\Block\Adminhtml\Grid\Helper\Form\Gallery\Content" as="content" template="[vendor]_[[module]::helper/gallery.phtml">
<arguments>
<argument name="config" xsi:type="array">
<item name="parentComponent" xsi:type="string">sample_form.sample_form.block_gallery.block_gallery</item>
</argument>
</arguments>
</block>
</block>
</referenceContainer>
</body>
</page>
2) Create helper block
[vendor]/[module]/Block/Adminhtml/Grid/Helper/Form/Gallery.php
namespace [vendor]\[module]\Block\Adminhtml\Grid\Helper\Form;
use Magento\Framework\Registry;
use Magento\Catalog\Model\Product;
use Magento\Eav\Model\Entity\Attribute;
use Magento\Catalog\Api\Data\ProductInterface;
class Gallery extends \Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Gallery
{
/**
* @var here you set your ui form
*/
protected $formName = 'sample_form';
}
3) Create helper form block
[vendor]\[module]\Block\Adminhtml\Grid\Helper\Form\Gallery\Content.php
namespace [vendor]\[module]\Block\Adminhtml\Grid\Helper\Form\Gallery;
use Magento\Framework\Data\Form\Element\AbstractElement;
use Magento\Backend\Block\Media\Uploader;
use Magento\Framework\View\Element\AbstractBlock;
use Magento\Framework\App\Filesystem\DirectoryList;
class Content extends \Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Gallery\Content
{
protected function _prepareLayout()
{
$this->addChild('uploader', 'Magento\Backend\Block\Media\Uploader');
$a = $this->getUploader()->getConfig()->setUrl(
$this->_urlBuilder->addSessionParam()->getUrl('[vendor]/grid_gallery/upload')/* here set you upload Controller */
)->setFileField(
'image'
)->setFilters(
[
'images' => [
'label' => __('Images (.gif, .jpg, .png)'),
'files' => ['*.gif', '*.jpg', '*.jpeg', '*.png'],
],
]
);
}
public function getImageTypes()
{
return '[]';
}
public function getMediaAttributes()
{
return '[]';
}
}
Note :- you should extends properly above both blocks
4) Create template
You should copy gallery.phtml form vendor/magento/module-product-video/view/adminhtml/templates/helper/gallery.phtml
and modified it as par your requirement and pest in your module [vendor]/[module]/view/adminhtml/templates/helper/gallery.phtml
Please let me know in comment if you need any help