Call a Custom Template in catalog product form according to Product Type through Modifier

Call a Custom Template in catalog product form according to Product Type through Modifier.

Today I am going to explain that how you can call your custom template in product form according to product type through modifiers in magento 2 in this article. I hope you are aware about how to call your custom template through uicomponent in product form in magento 2. IF not then read this article – https://webkul.com/blog/call-custom-template-catalog-product-form-magento-2/.

At first you will need to create a class for modifier in Ui, i.e. under app/code/Company/Module/Ui/DataProvider/Custom.php and then create two necessary methods, i.e. modifyMeta() and modifyData(). For e.g :

 

<?php
namespace Company\Module\Ui\DataProvider;

use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
use Magento\Catalog\Model\Locator\LocatorInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\View\LayoutFactory;

class Custom extends AbstractModifier
{
    /**
     * @var LocatorInterface
     */
    protected $locator;

    /**
     * @var RequestInterface
     */
    protected $request;

    /**
     * @var LayoutFactory
     */
    private $layoutFactory;

    public function __construct(
        LocatorInterface $locator,
        RequestInterface $request,
        LayoutFactory $layoutFactory
    ) {
        $this->locator = $locator;
        $this->request = $request;
        $this->layoutFactory = $layoutFactory;
    }

    public function modifyMeta(array $meta)
    {
        if ($this->getProductType() == "virtual") { // you can add here product type check according to your need
            $meta["custom_tab"] = [
                "children" => [
                    "custom_tab_container" => [
                        "arguments" => [
                            "data" => [
                                "config" => [
                                    "formElement" => "container",
                                    "componentType" => "container",
                                    'component' => 'Magento_Ui/js/form/components/html',
                                    "label" => __("Custom Information"),
                                    "required" => 0,
                                    "sortOrder" => 1,
                                    "content" => $this->layoutFactory->create()->createBlock(
                                        'Company\Module\Block\Adminhtml\Catalog\Product\Edit\Tab\Custom'
                                    )->toHtml(), // custom block in which you can call your custom template
                                ]
                            ]
                        ]
                    ]
                ],
                "arguments" => [
                    "data" => [
                        "config" => [
                            "componentType" => "fieldset",
                            "label" => __("Custom Information"),
                            "collapsible" => true,
                            "sortOrder" => 1,
                            'opened' => true,
                            'canShow' => true
                        ]
                    ]
                ]
            ];
        }
        return $meta;
    }

    /**
     * {@inheritdoc}
     */
    public function modifyData(array $data)
    {
        return $data;
    }

    /**
     * Get product type
     *
     * @return null|string
     */
    private function getProductType()
    {
        return (string)$this->request->getParam('type', $this->locator->getProduct()->getTypeId());
    }
}

 

Now you will need to add entry in etc/adminhtml/di.xml for virtual type. Like below:

 

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <virtualType name="Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Pool">
        <arguments>
            <argument name="modifiers" xsi:type="array">
                <item name="advancedCustomOptions" xsi:type="array">
                    <item name="class" xsi:type="string">Company\Module\Ui\DataProvider\Custom</item>
                    <item name="sortOrder" xsi:type="number">20</item>
                </item>
            </argument>
        </arguments>
    </virtualType>
</config>

 

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 🙂

. . .

Ask a Feature