cancel
Showing results for 
Search instead for 
Did you mean: 

Setting default attribute_set_id via form modifier

Setting default attribute_set_id via form modifier

I am currently able to hide, disable or set default values for fields on product creation form via the following:
etc/adminhtml/di.xml:

<?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="testAttribute" xsi:type="array">
                    <item name="class" xsi:type="string">Mypackage\Custom\Ui\DataProvider\Product\Form\Modifier\Attributes</item>
                    <item name="sortOrder" xsi:type="number">1000</item>
                </item>
            </argument>
        </arguments>
    </virtualType>
</config>

Ui/DataProvider/Product/Form/Modifier/Attributes.php

 

 

<?php

namespace Mypackage\Custom\Ui\DataProvider\Product\Form\Modifier;

use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
use Magento\Framework\Stdlib\ArrayManager;
use Magento\Catalog\Model\Locator\LocatorInterface;

class Attributes extends AbstractModifier
{
    private $arrayManager;
    private $locator;

    public function __construct(
        ArrayManager $arrayManager ,LocatorInterface $locator

    ) {
        $this->arrayManager = $arrayManager;
                $this->locator = $locator;
    }

    public function modifyData(array $data)
    {
        //var_dump($this);
        //exit;

        $model = $this->locator->getProduct();
        $modelId = $model->getId();

        if (!isset($data[$modelId][self::DATA_SOURCE_DEFAULT]['quantity_and_stock_status']['qty'])) {
            $data[$modelId][self::DATA_SOURCE_DEFAULT]['quantity_and_stock_status']['qty'] = '1';
        }
         $data[$modelId][self::DATA_SOURCE_DEFAULT]['attribute_set_id'] = '9';

        return $data;
      }
}

 

 

Everything works even the Attribute Set dropdown shows the name of the said attribute but the form doesn't pull ajax fields of the assigned attribute_set_id.I have to toggle the dropdown to default and back to module set value to show the fields of the attribute set.

 

Is this not the correct approach of selecting a different default attribute_set_id ? can this be corrected ? Please guide.