{"id":369570,"date":"2023-02-21T10:51:03","date_gmt":"2023-02-21T10:51:03","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=369570"},"modified":"2024-01-12T06:40:36","modified_gmt":"2024-01-12T06:40:36","slug":"how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/","title":{"rendered":"How to add media gallery field using ui-component for the admin custom form in Magento2."},"content":{"rendered":"\n<p><strong><a href=\"https:\/\/webkul.com\/blog\/create-ui-form-magento2-part-1\/\">What is the ui-component in magento2: <\/a><\/strong><\/p>\n\n\n\n<p>Basically, we are using UI components in magento2 to create the <strong>Grids<\/strong>, <strong>Forms<\/strong>, <strong>Dialogs<\/strong> and etc. And we can easily be designed the form and grid for the user interface And we can easily add multiple types of fields to the form. So help of this blog we want to add the media gallery field on the admin custom ui-component form.<\/p>\n\n\n\n<p><strong>Media Gallery:<\/strong><\/p>\n\n\n\n<p>To upload multiple images at the same time from one field in the form.<\/p>\n\n\n\n<p><strong>Step1:<\/strong> We need to create the layout file to define the ui component form in view\/adminhtml\/layout\/routename_controllername_actionname.xml:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?xml version=&quot;1.0&quot;?&gt;\n&lt;page xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xsi:noNamespaceSchemaLocation=&quot;urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd&quot;&gt;\n    &lt;update handle=&quot;styles&quot;\/&gt;\n    &lt;update handle=&quot;editor&quot;\/&gt;\n\t&lt;body&gt;\n\t    &lt;referenceBlock name=&quot;menu&quot;&gt;\n\t        &lt;action method=&quot;setActive&quot;&gt;\n   \t\t    &lt;argument name=&quot;itemId&quot; xsi:type=&quot;string&quot;&gt;\n                        VendorName_ModuleName::businesslisting\n                    &lt;\/argument&gt;\n\t\t&lt;\/action&gt;\n\t    &lt;\/referenceBlock&gt;\n\t    &lt;referenceBlock name=&quot;page.title&quot;&gt;\n\t        &lt;action method=&quot;setTitleClass&quot;&gt;\n\t\t    &lt;argument name=&quot;class&quot; xsi:type=&quot;string&quot;&gt;\n                        complex\n                    &lt;\/argument&gt;\n\t\t&lt;\/action&gt;\n\t    &lt;\/referenceBlock&gt;\n            &lt;referenceContainer name=&quot;content&quot;&gt;\n\t\t&lt;uiComponent name=&quot;business_form&quot;\/&gt;\n\t    &lt;\/referenceContainer&gt;\n\t&lt;\/body&gt;\n&lt;\/page&gt;<\/pre>\n\n\n\n<p><strong>Step2:<\/strong> Create the ui-component form and we need to write below code to add the media gallery field:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;fieldset name=&quot;gallery&quot;&gt;\n    &lt;argument name=&quot;data&quot; xsi:type=&quot;array&quot;&gt;\n        &lt;item name=&quot;config&quot; xsi:type=&quot;array&quot;&gt;\n            &lt;item name=&quot;collapsible&quot; xsi:type=&quot;boolean&quot;&gt;true&lt;\/item&gt;\n            &lt;item name=&quot;label&quot; xsi:type=&quot;string&quot; translate=&quot;true&quot;&gt;\n                Media Gallery\n            &lt;\/item&gt;\n            &lt;item name=&quot;sortOrder&quot; xsi:type=&quot;number&quot;&gt;22&lt;\/item&gt;\n        &lt;\/item&gt;    \n    &lt;\/argument&gt;\n    &lt;container name=&quot;gallery&quot; &gt;\n        &lt;argument name=&quot;data&quot; xsi:type=&quot;array&quot;&gt;\n            &lt;item name=&quot;config&quot; xsi:type=&quot;array&quot;&gt;\n                &lt;item name=&quot;sortOrder&quot; xsi:type=&quot;number&quot;&gt;10&lt;\/item&gt;\n            &lt;\/item&gt;\n        &lt;\/argument&gt;\n        &lt;htmlContent name=&quot;gallery&quot;&gt;\n            &lt;argument name=&quot;block&quot; xsi:type=&quot;object&quot;&gt;\nVendorName\\ModuleName\\Block\\Adminhtml\\Listing\\Helper\\Form\\Gallery\n            &lt;\/argument&gt;\n        &lt;\/htmlContent&gt;\n    &lt;\/container&gt;\n&lt;\/fieldset&gt;<\/pre>\n\n\n\n<p><strong>Step3<\/strong>: Create the block file, <code>VendorName\\ModuleName\\Block\\Adminhtml\\Listing\\Helper\\Form\\Gallery<\/code>.php<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\nnamespace \nVendorName\\ModuleName\\Block\\Adminhtml\\Listing\\Helper\\Form;\n\nuse Magento\\Framework\\Registry;\n\nclass Gallery extends \\Magento\\Framework\\View\\Element\\AbstractBlock\n{\n    \/**\n     * Gallery field name suffix\n     *\n     * @var string\n     *\/\n    protected $fieldNameSuffix = &#039;business_list&#039;;\n\n    \/**\n     * Gallery html id\n     *\n     * @var string\n     *\/\n    protected $htmlId = &#039;media_gallery&#039;;\n\n    \/**\n     * Gallery name\n     *\n     * @var string\n     *\/\n    protected $name = &#039;media_gallery&#039;;\n\n    \/**\n     * Html id for data scope\n     *\n     * @var string\n     *\/\n    protected $image = &#039;image&#039;;\n\n    \/**\n     * @var string\n     *\/\n    protected $formName = &#039;business_form&#039;;\n\n    \/**\n     * @var \\Magento\\Framework\\Data\\Form\n     *\/\n    protected $form;\n\n    \/**\n     * @var Registry\n     *\/\n    protected $registry;\n\n    \/**\n     * @var \\VendorName\\ModuleName\\Model\\BusinessListFactory     *\/\n    protected $businessListFactory;\n\n    \/**\n     * @var \\Magento\\Store\\Model\\StoreManagerInterface\n     *\/\n    protected $storeManager;\n\n    \/**\n     * @param \\Magento\\Framework\\View\\Element\\Context $context\n     * @param Registry $registry\n     * @param \\Magento\\Framework\\Data\\Form $form\n     * @param array $data\n     *\/\n    public function __construct(\n        \\Magento\\Framework\\View\\Element\\Context $context,\n        Registry $registry,\n        \\Magento\\Framework\\Data\\Form $form,\n        \\VendorName\\ModuleName\\Model\\BusinessListFactory $businessListFactory,\n        \\Magento\\Store\\Model\\StoreManagerInterface $storeManager,\n        $data = &#091;]\n    ) {\n        $this-&gt;registry = $registry;\n        $this-&gt;form = $form;\n        $this-&gt;businessListFactory = $businessListFactory;\n        $this-&gt;storeManager = $storeManager;\n        parent::__construct($context, $data);\n    }\n\n    \/**\n     * @return string\n     *\/\n    public function getElementHtml()\n    {\n        return $this-&gt;getContentHtml();\n    }\n\n    \/**\n     * Get product images\n     *\n     * @return array|null\n     *\/\n    public function getImages()\n    {\n        $result = &#091;];\n        $gallery = &#091;];\n        $id = $this-&gt;registry-&gt;registry(&#039;id&#039;);\n        $businessList = $this-&gt;businessListFactory-&gt;create()-&gt;getCollection()-&gt;addFieldToFilter(&#039;entity_id&#039;, $id)-&gt;getFirstItem();\n        $mediaUrl = $this-&gt;storeManager-&gt;getStore()-&gt;getBaseUrl(\\Magento\\Framework\\UrlInterface::URL_TYPE_MEDIA);\n        if ($businessList-&gt;getMediaGallery()) {\n            $gallery = explode(&quot;;&quot;, $businessList-&gt;getMediaGallery());\n        }\n        if (count($gallery)) {\n            $result&#091;&#039;images&#039;] = &#091;];\n            $position = 1;\n            foreach ($gallery as $image) {\n                $label = str_replace(&quot;business\/business\/mediagallery\/&quot;,&quot;&quot;,$image);\n                $result&#091;&#039;images&#039;]&#091;] = &#091;\n                    &#039;value_id&#039; =&gt; $image,\n                    &#039;file&#039; =&gt; $image,\n                    &#039;label&#039; =&gt; $label,\n                    &#039;position&#039; =&gt; $position,\n                    &#039;url&#039; =&gt; $mediaUrl.$image,\n                ];\n                $position++;\n            }\n        }\n\n        return $result;\n    }\n\n    \/**\n     * Prepares content block\n     *\n     * @return string\n     *\/\n    public function getContentHtml()\n    {\n        $content = $this-&gt;getChildBlock(&#039;content&#039;);\n        if (!$content) {\n            $content = $this-&gt;getLayout()-&gt;createBlock(      \\VendorName\\ModuleName\\Block\\Adminhtml\\Listing\\Helper\\Form\\Gallery\\Content::class,\n                &#039;&#039;,\n                &#091;\n                    &#039;config&#039; =&gt; &#091;\n                        &#039;parentComponent&#039; =&gt; &#039;business_form.business_form.block_gallery.block_gallery&#039;\n                    ]\n                ]\n            );\n        }\n\n        $content\n            -&gt;setId($this-&gt;getHtmlId() . &#039;_content&#039;)\n            -&gt;setElement($this)\n            -&gt;setFormName($this-&gt;formName);\n        $galleryJs = $content-&gt;getJsObjectName();\n        $content-&gt;getUploader()-&gt;getConfig()-&gt;setMegiaGallery($galleryJs);\n        return $content-&gt;toHtml();\n    }\n\n    \/**\n     * @return string\n     *\/\n    protected function getHtmlId()\n    {\n        return $this-&gt;htmlId;\n    }\n\n    \/**\n     * @return string\n     *\/\n    public function getName()\n    {\n        return $this-&gt;name;\n    }\n\n    \/**\n     * @return string\n     *\/\n    public function getFieldNameSuffix()\n    {\n        return $this-&gt;fieldNameSuffix;\n    }\n\n    \/**\n     * @return string\n     *\/\n    public function getDataScopeHtmlId()\n    {\n        return $this-&gt;image;\n    }\n\n    \/**\n     * @return string\n     *\/\n    public function toHtml()\n    {\n        return $this-&gt;getElementHtml();\n    }\n}<\/pre>\n\n\n\n<p><strong>Step4<\/strong>: Create another block file to pass the phtml path: <code>VendorName\\ModuleName\\Block\\Adminhtml\\Listing\\Helper\\Form\\Gallery<\/code>\\Content.php<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\nnamespace VendorName\\ModuleName\\Block\\Adminhtml\\Listing\\Helper\\Form\\Gallery;\n\nuse Magento\\Framework\\App\\ObjectManager;\nuse Magento\\Backend\\Block\\Media\\Uploader;\nuse Magento\\Framework\\View\\Element\\AbstractBlock;\n\nclass Content extends \\Magento\\Backend\\Block\\Widget\n{\n    \/**\n     * @var string\n     *\/\n    protected $_template = &#039;business\/form\/gallery.phtml&#039;;\n\n    \/**\n     * @var \\Magento\\Framework\\Json\\EncoderInterface\n     *\/\n    protected $_jsonEncoder;\n\n    \/**\n     * @var ImageUploadConfigDataProvider\n     *\/\n    private $imageUploadConfigDataProvider;\n\n    \/**\n     * Content constructor.\n     * @param \\Magento\\Backend\\Block\\Template\\Context $context\n     * @param \\Magento\\Framework\\Json\\EncoderInterface $jsonEncoder\n     * @param array $data\n     * @param null $imageUploadConfigDataProvider\n     *\/\n    public function __construct(\n        \\Magento\\Backend\\Block\\Template\\Context $context,\n        \\Magento\\Framework\\Json\\EncoderInterface $jsonEncoder,\n        array $data = &#091;],\n        $imageUploadConfigDataProvider = null\n    ) {\n        $this-&gt;_jsonEncoder = $jsonEncoder;\n        parent::__construct($context, $data);\n        try {\n            \/* Try for old magento version where ImageUploadConfigDataProvider does not exist *\/\n            if (class_exists(\\Magento\\Backend\\Block\\DataProviders\\ImageUploadConfig::class)) {\n                $this-&gt;imageUploadConfigDataProvider = $imageUploadConfigDataProvider\n                    ?: ObjectManager::getInstance()-&gt;get(\\Magento\\Backend\\Block\\DataProviders\\ImageUploadConfig::class);\n            } elseif (class_exists(\\Magento\\Backend\\Block\\DataProviders\\UploadConfig::class)) {\n                \/* Workaround for Magento 2.2.8 *\/\n                $this-&gt;imageUploadConfigDataProvider = ObjectManager::getInstance()-&gt;get(\n                    \\Magento\\Backend\\Block\\DataProviders\\UploadConfig::class\n                );\n            }\n        } catch (\\Exception $e) {\n            return;\n        }\n    }\n\n    \/**\n     * @return AbstractBlock\n     *\/\n    protected function _prepareLayout()\n    {\n        $this-&gt;addChild(\n            &#039;uploader&#039;,\n            \\Magento\\Backend\\Block\\Media\\Uploader::class,\n            &#091;&#039;image_upload_config_data&#039; =&gt; $this-&gt;imageUploadConfigDataProvider]\n        );\n\n        $this-&gt;getUploader()-&gt;getConfig()-&gt;setUrl(\n            $this-&gt;_urlBuilder-&gt;getUrl(&#039;business\/listing_upload\/gallery&#039;)\n        )-&gt;setFileField(\n            &#039;image&#039;\n        )-&gt;setFilters(\n            &#091;\n                &#039;images&#039; =&gt; &#091;\n                    &#039;label&#039; =&gt; __(&#039;Images (.gif, .jpg, .png)&#039;),\n                    &#039;files&#039; =&gt; &#091;&#039;*.gif&#039;, &#039;*.jpg&#039;, &#039;*.jpeg&#039;, &#039;*.png&#039;],\n                ],\n            ]\n        );\n\n        $this-&gt;_eventManager-&gt;dispatch(&#039;business_listing_gallery_prepare_layout&#039;, &#091;&#039;block&#039; =&gt; $this]);\n\n        return parent::_prepareLayout();\n    }\n\n    \/**\n     * Retrieve uploader block\n     *\n     * @return Uploader\n     *\/\n    public function getUploader()\n    {\n        return $this-&gt;getChildBlock(&#039;uploader&#039;);\n    }\n\n    \/**\n     * Retrieve uploader block html\n     *\n     * @return string\n     *\/\n    public function getUploaderHtml()\n    {\n        return $this-&gt;getChildHtml(&#039;uploader&#039;);\n    }\n\n    \/**\n     * @return string\n     *\/\n    public function getJsObjectName()\n    {\n        return $this-&gt;getHtmlId() . &#039;JsObject&#039;;\n    }\n\n    \/**\n     * @return string\n     *\/\n    public function getAddImagesButton()\n    {\n        return $this-&gt;getButtonHtml(\n            __(&#039;Add New Images&#039;),\n            $this-&gt;getJsObjectName() . &#039;.showUploader()&#039;,\n            &#039;add&#039;,\n            $this-&gt;getHtmlId() . &#039;_add_images_button&#039;\n        );\n    }\n\n    \/**\n     * @return string\n     *\/\n    public function getImagesJson()\n    {\n        $value = $this-&gt;getElement()-&gt;getImages();\n        if (is_array($value) &amp;&amp;\n            array_key_exists(&#039;images&#039;, $value) &amp;&amp;\n            is_array($value&#091;&#039;images&#039;]) &amp;&amp;\n            count($value&#091;&#039;images&#039;])\n        ) {\n            $images = $this-&gt;sortImagesByPosition($value&#091;&#039;images&#039;]);\n\n            return $this-&gt;_jsonEncoder-&gt;encode($images);\n        }\n        return &#039;&#091;]&#039;;\n    }\n\n    \/**\n     * Sort images array by position key\n     *\n     * @param array $images\n     * @return array\n     *\/\n    private function sortImagesByPosition($images)\n    {\n        if (is_array($images)) {\n            usort($images, function ($imageA, $imageB) {\n                return ($imageA&#091;&#039;position&#039;] &lt; $imageB&#091;&#039;position&#039;]) ? -1 : 1;\n            });\n        }\n        return $images;\n    }\n}<\/pre>\n\n\n\n<p><strong>Step5<\/strong>: Create the phtml file to declare the js content and file: \/view\/adminhtml\/templates\/business\/form\/gallery.phtml<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\n\/** @var $block \\VendorName\\ModuleName\\Block\\Adminhtml\\Listing\\Helper\\Form\\Gallery\\Content *\/\n$elementName = $block-&gt;getElement()-&gt;getName() . &#039;&#091;images]&#039;;\n$formName = $block-&gt;getFormName();\n?&gt;\n&lt;div id=&quot;&lt;?= $block-&gt;getHtmlId() ?&gt;&quot;\n     class=&quot;gallery&quot;\n     data-mage-init=&#039;{&quot;VendorName_ModuleName\/js\/post-gallery&quot;:{&quot;template&quot;:&quot;#&lt;?= $block-&gt;getHtmlId() ?&gt;-template&quot;}}&#039;     data-parent-component=&quot;&lt;?= $block-&gt;escapeHtml($block-&gt;getData(&#039;config\/parentComponent&#039;)) ?&gt;&quot;\n     data-images=&quot;&lt;?= $block-&gt;escapeHtml($block-&gt;getImagesJson()) ?&gt;&quot;\n     data-types=&quot;{}&quot;\n    &gt;\n    &lt;?php if (!$block-&gt;getElement()-&gt;getReadonly()) {?&gt;\n        &lt;div class=&quot;image image-placeholder&quot;&gt;\n            &lt;?= $block-&gt;getUploaderHtml(); ?&gt;\n            &lt;div class=&quot;product-image-wrapper&quot;&gt;\n                &lt;p class=&quot;image-placeholder-text&quot;&gt;\n                    &lt;?=  $block-&gt;escapeHtml(__(&#039;Browse to find or drag image here&#039;)); ?&gt;\n                &lt;\/p&gt;\n            &lt;\/div&gt;\n        &lt;\/div&gt;\n    &lt;?php } ?&gt;\n\n    &lt;script id=&quot;&lt;?= $block-&gt;getHtmlId() ?&gt;-template&quot; type=&quot;text\/x-magento-template&quot;&gt;\n        &lt;div class=&quot;image item&lt;% if (data.disabled == 1) { %&gt; hidden-for-front&lt;% } %&gt;&quot;\n             data-role=&quot;image&quot;&gt;\n            &lt;input type=&quot;hidden&quot;\n                   name=&quot;&lt;?= $block-&gt;escapeHtml($elementName) ?&gt;&#091;&lt;%- data.file_id %&gt;]&#091;position]&quot;\n                   value=&quot;&lt;%- data.position %&gt;&quot;\n                   data-form-part=&quot;&lt;?= $block-&gt;escapeHtml($formName) ?&gt;&quot;\n                   class=&quot;position&quot;\/&gt;\n            &lt;input type=&quot;hidden&quot;\n                   name=&quot;&lt;?= $block-&gt;escapeHtml($elementName) ?&gt;&#091;&lt;%- data.file_id %&gt;]&#091;file]&quot;\n                   data-form-part=&quot;&lt;?= $block-&gt;escapeHtml($formName) ?&gt;&quot;\n                   value=&quot;&lt;%- data.file %&gt;&quot;\/&gt;\n            &lt;input type=&quot;hidden&quot;\n                   name=&quot;&lt;?= $block-&gt;escapeHtml($elementName) ?&gt;&#091;&lt;%- data.file_id %&gt;]&#091;value_id]&quot;\n                   data-form-part=&quot;&lt;?= $block-&gt;escapeHtml($formName) ?&gt;&quot;\n                   value=&quot;&lt;%- data.value_id %&gt;&quot;\/&gt;\n            &lt;input type=&quot;hidden&quot;\n                   name=&quot;&lt;?= $block-&gt;escapeHtml($elementName) ?&gt;&#091;&lt;%- data.file_id %&gt;]&#091;label]&quot;\n                   data-form-part=&quot;&lt;?= $block-&gt;escapeHtml($formName) ?&gt;&quot;\n                   value=&quot;&lt;%- data.label %&gt;&quot;\/&gt;\n            &lt;input type=&quot;hidden&quot;\n                   name=&quot;&lt;?= $block-&gt;escapeHtml($elementName) ?&gt;&#091;&lt;%- data.file_id %&gt;]&#091;disabled]&quot;\n                   data-form-part=&quot;&lt;?= $block-&gt;escapeHtml($formName) ?&gt;&quot;\n                   value=&quot;&lt;%- data.disabled %&gt;&quot;\/&gt;\n            &lt;input type=&quot;hidden&quot;\n                   name=&quot;&lt;?= $block-&gt;escapeHtml($elementName) ?&gt;&#091;&lt;%- data.file_id %&gt;]&#091;media_type]&quot;\n                   data-form-part=&quot;&lt;?= $block-&gt;escapeHtml($formName) ?&gt;&quot;\n                   value=&quot;image&quot;\/&gt;\n            &lt;input type=&quot;hidden&quot;\n                   name=&quot;&lt;?= $block-&gt;escapeHtml($elementName) ?&gt;&#091;&lt;%- data.file_id %&gt;]&#091;removed]&quot;\n                   data-form-part=&quot;&lt;?= $block-&gt;escapeHtml($formName) ?&gt;&quot;\n                   value=&quot;&quot;\n                   class=&quot;is-removed&quot;\/&gt;\n\n            &lt;div class=&quot;product-image-wrapper&quot;&gt;\n                &lt;img class=&quot;product-image&quot;\n                     data-role=&quot;image-element&quot;\n                     src=&quot;&lt;%- data.url %&gt;&quot;\n                     alt=&quot;&lt;%- data.label %&gt;&quot;\/&gt;\n\n                &lt;div class=&quot;actions&quot;&gt;\n                    &lt;button type=&quot;button&quot;\n                            class=&quot;action-remove&quot;\n                            data-role=&quot;delete-button&quot;\n                            title=&quot;&lt;?= $block-&gt;escapeHtml(__(&#039;Delete image&#039;)) ?&gt;&quot;&gt;\n                    &lt;span&gt;\n                        &lt;?= $block-&gt;escapeHtml(__(&#039;Delete image&#039;)) ?&gt;\n                    &lt;\/span&gt;\n                    &lt;\/button&gt;\n                    &lt;div class=&quot;draggable-handle&quot;&gt;&lt;\/div&gt;\n                &lt;\/div&gt;\n                &lt;div class=&quot;image-fade&quot;&gt;\n                    &lt;span&gt;&lt;?= $block-&gt;escapeHtml(__(&#039;Hidden&#039;)) ?&gt;&lt;\/span&gt;\n                &lt;\/div&gt;\n            &lt;\/div&gt;\n\n            &lt;div class=&quot;item-description&quot;&gt;\n                &lt;div class=&quot;item-title&quot; data-role=&quot;img-title&quot;&gt;&lt;%- data.label %&gt;&lt;\/div&gt;\n            &lt;\/div&gt;\n        &lt;\/div&gt;\n    &lt;\/script&gt;\n&lt;\/div&gt;\n&lt;script&gt;\n    jQuery(&#039;body&#039;).trigger(&#039;contentUpdated&#039;);\n&lt;\/script&gt;<\/pre>\n\n\n\n<p><strong>Step6<\/strong>: Create the js file to write the js code: view\/adminhtml\/web\/js\/post-gallery.js<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">define(&#091;\n    &#039;jquery&#039;,\n    &#039;underscore&#039;,\n    &#039;mage\/template&#039;,\n    &#039;uiRegistry&#039;,\n    &#039;productGallery&#039;,\n    &#039;jquery-ui-modules\/core&#039;,\n    &#039;jquery-ui-modules\/widget&#039;,\n    &#039;baseImage&#039;\n], function ($, _, mageTemplate, registry, productGallery) {\n    &#039;use strict&#039;;\n    $.widget(&#039;mage.productGallery&#039;, $.mage.productGallery, {\n        _showDialog: function (imageData) {}\n    });\n    return $.mage.productGallery;\n});<\/pre>\n\n\n\n<p>After created above files we need to run these commands from our end:<\/p>\n\n\n\n<p><strong>php bin\/magento setup:di:compile<\/strong><\/p>\n\n\n\n<p><strong>php bin\/magento setup:static-content:deploy<\/strong> <\/p>\n\n\n\n<p><strong>php bin\/magento cache:flush<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img decoding=\"async\" width=\"1200\" height=\"617\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/02\/Selection_322-1200x617.png\" alt=\"Media Gallery field using ui-component\" class=\"wp-image-369577\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/02\/Selection_322-1200x617.png 1200w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/02\/Selection_322-300x154.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/02\/Selection_322-250x128.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/02\/Selection_322-768x395.png 768w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/02\/Selection_322.png 1290w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" loading=\"lazy\" \/><\/figure>\n\n\n\n<p><strong>Step7<\/strong>: After create the field then we can save the multiple image through this code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\nnamespace VendorName\\ModuleName\\Controller\\Adminhtml\\Upload\\Image;\n\nuse Magento\\Framework\\Controller\\ResultFactory;\n\n\/**\n * Business media image upload controller\n *\/\nabstract class Action extends \\Magento\\Catalog\\Controller\\Adminhtml\\Category\\Image\\Upload\n{\n    \/**\n     * File key\n     *\n     * @var string\n     *\/\n    protected $_fileKey;\n\n    public function execute()\n    {\n        try {\n            $result = $this-&gt;imageUploader-&gt;saveFileToTmpDir($this-&gt;_fileKey);\n\n            $result&#091;&#039;cookie&#039;] = &#091;\n                &#039;name&#039; =&gt; $this-&gt;_getSession()-&gt;getName(),\n                &#039;value&#039; =&gt; $this-&gt;_getSession()-&gt;getSessionId(),\n                &#039;lifetime&#039; =&gt; $this-&gt;_getSession()-&gt;getCookieLifetime(),\n                &#039;path&#039; =&gt; $this-&gt;_getSession()-&gt;getCookiePath(),\n                &#039;domain&#039; =&gt; $this-&gt;_getSession()-&gt;getCookieDomain(),\n            ];\n        } catch (\\Exception $e) {\n            $result = &#091;&#039;error&#039; =&gt; $e-&gt;getMessage(), &#039;errorcode&#039; =&gt; $e-&gt;getCode()];\n        }\n        return $this-&gt;resultFactory-&gt;create(ResultFactory::TYPE_JSON)-&gt;setData($result);\n    }\n}<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\nnamespace VendorName\\ModuleName\\Controller\\Adminhtml\\Listing\\Upload;\n\nuse VendorName\\ModuleName\\Controller\\Adminhtml\\Upload\\Image\\Action;\n\n\/**\n * busimess gallery image upload controller\n *\/\nclass Gallery extends Action\n{\n    \/**\n     * File key\n     *\n     * @var string\n     *\/\n    protected $_fileKey = &#039;image&#039;;\n}<\/pre>\n\n\n\n<p><strong><code>VendorName\\ModuleName<\/code><\/strong>\\<strong>Controller\\Adminhtml\\Listing\\Save.php<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\n\nnamespace VendorName\\ModuleName\\Controller\\Adminhtml\\Listing;\n\nuse Magento\\Backend\\App\\Action;\nuse Magento\\Backend\\App\\Action\\Context;\nuse Magento\\Framework\\View\\Result\\PageFactory;\nuse Magento\\Framework\\App\\Filesystem\\DirectoryList;\nuse Magento\\Framework\\Filesystem;\nuse Magento\\Framework\\Filesystem\\Driver\\File;\n\nclass Save extends Action\n{\n    \/**\n     * @var Magento\\Framework\\View\\Result\\PageFactory\n     *\/\n    protected $resultPageFactory;\n\n    \/**\n     * @var \\Magento\\Backend\\Model\\View\\Result\\ForwardFactory\n     *\/\n    protected $resultForwardFactory;\n\n    \/**\n     * @var \\Magento\\Framework\\Filesystem\\Directory\\WriteInterface\n     *\/\n    protected $mediaDirectory;\n\n    \/**\n     * @var \\Magento\\Framework\\Filesystem\\Driver\\File\n     *\/\n    protected $fileDriver;\n\n    \/**\n     * @var Filesystem\n     *\/\n    protected $filesystem;\n\n    \/**\n     * @var \\Magento\\MediaStorage\\Helper\\File\\Storage\\Database\n     *\/\n    protected $coreFileStorageDatabase;\n\n    \/**\n     * @var Webkul\\Businessform\\Model\\BusinessListFactory *\/\n    protected $businessListFactory;\n\n    \/**\n     * @var \\Magento\\Framework\\Registry\n     *\/\n    protected $coreRegistry = null;\n\n    \/**\n     * @param Context $context\n     * @param PageFactory $resultPageFactory\n     * @param \\Magento\\Backend\\Model\\View\\Result\\ForwardFactory $resultForwardFactory\n     * @param Filesystem $filesystem\n     * @param \\Magento\\MediaStorage\\Helper\\File\\Storage\\Database $coreFileStorageDatabase\n     * @param File $fileDriver\n     * @param \\VendorName\\ModuleName\\Model\\BusinessListFactory $businessListFactory\n     * @param \\Magento\\Framework\\Registry $coreRegistry\n     *\/\n    public function __construct(\n        Context $context,\n        PageFactory $resultPageFactory,\n        \\Magento\\Backend\\Model\\View\\Result\\ForwardFactory $resultForwardFactory,\n        Filesystem $filesystem,\n        \\Magento\\MediaStorage\\Helper\\File\\Storage\\Database $coreFileStorageDatabase,\n        File $fileDriver,\n        \\VendorName\\ModuleName\\Model\\BusinessListFactory $businessListFactory,\n        \\Magento\\Framework\\Registry $coreRegistry\n    ) {\n        parent::__construct($context);\n        $this-&gt;resultPageFactory = $resultPageFactory;\n        $this-&gt;resultForwardFactory = $resultForwardFactory;\n        $this-&gt;fileDriver = $fileDriver;\n        $this-&gt;filesystem = $filesystem;\n        $this-&gt;mediaDirectory = $filesystem-&gt;getDirectoryWrite(DirectoryList::MEDIA);\n        $this-&gt;coreFileStorageDatabase = $coreFileStorageDatabase;\n        $this-&gt;businessListFactory = $businessListFactory;\n        $this-&gt;coreRegistry = $coreRegistry;\n    }\n\n    \/**\n     * Save Media Gallery Data\n     *\n     * @return \\Magento\\Framework\\App\\Response\\Http\n     *\/\n    public function execute()\n    {\n        $resultRedirect = $this-&gt;resultRedirectFactory-&gt;create();\n        $originalRequestData = $this-&gt;getRequest()-&gt;getPostValue();\n        $galleryArray = $this-&gt;uploadMediaGallery($originalRequestData);\n        if(!empty($galleryArray)){\n            $mediaGallery = implode(&#039;;&#039;,$galleryArray);\n            $data&#091;&#039;media_gallery&#039;] = $mediaGallery;\n            $businessList = $this-&gt;businessListFactory-&gt;create();\n            $businessList-&gt;setData($data)-&gt;save();\n            $id = $businessList-&gt;getId();\n            $this-&gt;coreRegistry-&gt;register(&quot;entity_id&quot;, $id);\n            $this-&gt;messageManager-&gt;addSuccess(&quot;Gallery data has been saved.&quot;);\n            return $resultRedirect-&gt;setPath(&#039;*\/*\/edit&#039;,&#091;&#039;entity_id&#039;=&gt; $id]);\n        } else{\n            $this-&gt;messageManager-&gt;addError(\n                __(&#039;Something went wrong while saving the media gallery.&#039;)\n            );\n            return $resultRedirect-&gt;setPath(&#039;*\/*\/edit&#039;);\n        }\n       \n    }\n\n    \/**\n     * Upload MediaGallery\n     *\n     * @return array Data\n     *\/\n    public function uploadMediaGallery($data)\n    {\n        $bannerimageDirPath = $this-&gt;mediaDirectory-&gt;getAbsolutePath(&quot;business\/business\/mediagallery&quot;);\n        $tmp = $this-&gt;mediaDirectory-&gt;getAbsolutePath(&quot;business\/business\/mediagallery\/tmp&quot;);\n        if (!$this-&gt;fileDriver-&gt;isExists($bannerimageDirPath)) {\n            $this-&gt;fileDriver-&gt;createDirectory($bannerimageDirPath, 0777);\n            $this-&gt;fileDriver-&gt;createDirectory($tmp, 0777);\n        }\n        $gallery = &#091;];\n        if (!empty($data&#091;&#039;media_gallery&#039;]&#091;&#039;images&#039;])) {\n            $images = $data&#091;&#039;media_gallery&#039;]&#091;&#039;images&#039;];\n            foreach ($images as $image) {\n                if (empty($image&#091;&#039;removed&#039;])) {\n                    if (!empty($image&#091;&#039;value_id&#039;])) {\n                        $gallery&#091;] = $image&#091;&#039;value_id&#039;];\n                    } elseif (!empty($image&#091;&#039;file&#039;])) {\n                        $originalImageName = $image&#091;&#039;file&#039;];\n                        $imageName = $originalImageName;\n                        $basePath = &quot;business\/business\/mediagallery&quot;;\n                        $baseTmpImagePath = &quot;catalog\/tmp\/category\/&quot; . $imageName;\n                        $baseImagePath = $basePath . &quot;\/&quot; . $imageName;\n                        $mediaPath = $this-&gt;filesystem-&gt;getDirectoryRead(DirectoryList::MEDIA)-&gt;getAbsolutePath();\n                        $baseImageAbsolutePath = $mediaPath . $baseImagePath;\n                        $i = 1;\n                        while (file_exists($baseImageAbsolutePath)) {\n                            $i++;\n                            $p = mb_strrpos($originalImageName, &#039;.&#039;);\n                            if (false !== $p) {\n                                $imageName = mb_substr($originalImageName, 0, $p) . $i .  mb_substr($originalImageName, $p);\n                            } else {\n                                $imageName = $originalImageName . $i;\n                            }\n                            $baseImagePath = $basePath . &quot;\/&quot; . $imageName;\n                            $baseImageAbsolutePath = $mediaPath . $baseImagePath;\n                        }\n                        $this-&gt;coreFileStorageDatabase-&gt;copyFile(\n                            $baseTmpImagePath,\n                            $baseImagePath\n                        );\n                        $this-&gt;mediaDirectory-&gt;renameFile(\n                            $baseTmpImagePath,\n                            $baseImagePath\n                        );\n\n                        $gallery&#091;] = $baseImagePath;\n                    }\n                }\n            }\n        }\n        return $gallery;\n    }\n}<\/pre>\n\n\n\n<p>After uploading the images will show in the form:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"964\" height=\"219\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/02\/blog.png\" alt=\"blog\" class=\"wp-image-369580\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/02\/blog.png 964w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/02\/blog-300x68.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/02\/blog-250x57.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/02\/blog-768x174.png 768w\" sizes=\"(max-width: 964px) 100vw, 964px\" loading=\"lazy\" \/><\/figure>\n\n\n\n<p>Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is the ui-component in magento2: Basically, we are using UI components in magento2 to create the Grids, Forms, Dialogs and etc. And we can easily be designed the form and grid for the user interface And we can easily add multiple types of fields to the form. So help of this blog we want <a href=\"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":368,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9121],"tags":[],"class_list":["post-369570","post","type-post","status-publish","format-standard","hentry","category-magento-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to add media gallery field using ui-component for the admin custom form in Magento2.<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to add media gallery field using ui-component for the admin custom form in Magento2.\" \/>\n<meta property=\"og:description\" content=\"What is the ui-component in magento2: Basically, we are using UI components in magento2 to create the Grids, Forms, Dialogs and etc. And we can easily be designed the form and grid for the user interface And we can easily add multiple types of fields to the form. So help of this blog we want [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-21T10:51:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-12T06:40:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2023\/02\/Selection_322-1200x617.png\" \/>\n<meta name=\"author\" content=\"Anurag Jain\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Anurag Jain\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/\"},\"author\":{\"name\":\"Anurag Jain\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/c62926ca988352d74ad5c2e85fcd4a63\"},\"headline\":\"How to add media gallery field using ui-component for the admin custom form in Magento2.\",\"datePublished\":\"2023-02-21T10:51:03+00:00\",\"dateModified\":\"2024-01-12T06:40:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/\"},\"wordCount\":251,\"commentCount\":3,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2023\/02\/Selection_322-1200x617.png\",\"articleSection\":[\"Magento 2\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/\",\"url\":\"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/\",\"name\":\"How to add media gallery field using ui-component for the admin custom form in Magento2.\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2023\/02\/Selection_322-1200x617.png\",\"datePublished\":\"2023-02-21T10:51:03+00:00\",\"dateModified\":\"2024-01-12T06:40:36+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/#primaryimage\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/02\/Selection_322.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/02\/Selection_322.png\",\"width\":1290,\"height\":663,\"caption\":\"Selection_322\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to add media gallery field using ui-component for the admin custom form in Magento2.\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/c62926ca988352d74ad5c2e85fcd4a63\",\"name\":\"Anurag Jain\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e15e522c60de7a76ffd1efc2152f6d848fcd5c14ce27b1d30bac29701d4c9837?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e15e522c60de7a76ffd1efc2152f6d848fcd5c14ce27b1d30bac29701d4c9837?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Anurag Jain\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/anuragjain-magento209\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to add media gallery field using ui-component for the admin custom form in Magento2.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/","og_locale":"en_US","og_type":"article","og_title":"How to add media gallery field using ui-component for the admin custom form in Magento2.","og_description":"What is the ui-component in magento2: Basically, we are using UI components in magento2 to create the Grids, Forms, Dialogs and etc. And we can easily be designed the form and grid for the user interface And we can easily add multiple types of fields to the form. So help of this blog we want [...]","og_url":"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2023-02-21T10:51:03+00:00","article_modified_time":"2024-01-12T06:40:36+00:00","og_image":[{"url":"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2023\/02\/Selection_322-1200x617.png","type":"","width":"","height":""}],"author":"Anurag Jain","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Anurag Jain","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/"},"author":{"name":"Anurag Jain","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/c62926ca988352d74ad5c2e85fcd4a63"},"headline":"How to add media gallery field using ui-component for the admin custom form in Magento2.","datePublished":"2023-02-21T10:51:03+00:00","dateModified":"2024-01-12T06:40:36+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/"},"wordCount":251,"commentCount":3,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"image":{"@id":"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/#primaryimage"},"thumbnailUrl":"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2023\/02\/Selection_322-1200x617.png","articleSection":["Magento 2"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/","url":"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/","name":"How to add media gallery field using ui-component for the admin custom form in Magento2.","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/#primaryimage"},"image":{"@id":"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/#primaryimage"},"thumbnailUrl":"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2023\/02\/Selection_322-1200x617.png","datePublished":"2023-02-21T10:51:03+00:00","dateModified":"2024-01-12T06:40:36+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/#primaryimage","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/02\/Selection_322.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/02\/Selection_322.png","width":1290,"height":663,"caption":"Selection_322"},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/how-to-add-media-gallery-field-using-ui-component-for-the-admin-custom-form-in-magento2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to add media gallery field using ui-component for the admin custom form in Magento2."}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/c62926ca988352d74ad5c2e85fcd4a63","name":"Anurag Jain","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e15e522c60de7a76ffd1efc2152f6d848fcd5c14ce27b1d30bac29701d4c9837?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e15e522c60de7a76ffd1efc2152f6d848fcd5c14ce27b1d30bac29701d4c9837?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Anurag Jain"},"url":"https:\/\/webkul.com\/blog\/author\/anuragjain-magento209\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/369570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/368"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=369570"}],"version-history":[{"count":16,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/369570\/revisions"}],"predecessor-version":[{"id":417635,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/369570\/revisions\/417635"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=369570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=369570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=369570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}