Skip to content

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="MagentoCatalogUiDataProviderProductFormModifierPool">
        <arguments>
            <argument name="modifiers" xsi:type="array">
                <item name="testAttribute" xsi:type="array">
                    <item name="class" xsi:type="string">MypackageCustomUiDataProviderProductFormModifierAttributes</item>
                    <item name="sortOrder" xsi:type="number">1000</item>
                </item>
            </argument>
        </arguments>
    </virtualType>
</config>

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

<?php

namespace MypackageCustomUiDataProviderProductFormModifier;

use MagentoCatalogUiDataProviderProductFormModifierAbstractModifier;
use MagentoFrameworkStdlibArrayManager;
use MagentoCatalogModelLocatorLocatorInterface;

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 ?