Call A Custom template in catalog product form in magento 2
Today I am going to explain that how you can call your custom template through uicomponent in product form in magento 2 in this article.
At first you will need to create a uicomponent, i.e. product_form.xml for product form under app/code/Company/Namespace/view/adminhtml/ui_component/product_form.xml And write the below code in product_form.xml which you created, and keep this in mind that you can not rename product_form.xml with any other name, because it is the file which magento calls for product form.
<?xml version="1.0"?> <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd"> <fieldset name="custom_tab"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="label" xsi:type="string" translate="true">Gallery Information</item> <item name="collapsible" xsi:type="boolean">true</item> <item name="opened" xsi:type="boolean">true</item> <!-- this attribute is, if you want your custom section by default opened when product form calls, if not then set the value as false --> <item name="canShow" xsi:type="boolean">true</item> <item name="sortOrder" xsi:type="string">1</item> </item> </argument> <container name="custom_tab_container"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="sortOrder" xsi:type="string">1</item> </item> </argument> <htmlContent name="html_content"> <argument name="block" xsi:type="object">Company\Namespace\Block\Adminhtml\Catalog\Product\Form\Gallery</argument><!-- and this the path to our block file through which we will call our template file --> </htmlContent> </container> </fieldset> </form>
And then create Block file which we given above, i.e.Company\Namespace\Block\Adminhtml\Catalog\Product\Form\Gallery.php, and write the below code in this block file, which is to call template file.
<?php namespace Company\Namespace\Block\Adminhtml\Catalog\Product\Form; class Gallery extends \Magento\Backend\Block\Template { /** * Block template. * * @var string */ protected $_template = 'group/gallery.phtml'; }
As you can see that in block file we are calling template file, in which you can write your code according to your need, I printed only Hello World through this template file, just for an example. So the template file will be here – app/code/Company/Namespace/view/adminhtml/templates/group/gallery.phtml and I have just written the html in this file to print Hello World as below:
<div> <h1><?php echo __("HELLO WORLD") ?></h1> </div>
Now, the output will be as :
Hope this blog will help you to develop custom functionality in your module in a better way. Try this and if you have any query then just comment below 🙂
Be the first to comment.