If you are working on your Magento, to implement functionality releated to Magento’s product’s attribute, and if you are displaying them in admin product form, and want to depend one of the field on other field’s value then this blog is helpful for you.
For example:
You have two attribute of product: field_main and field_sub.
Your attribute field_sub is dependent on field_main attribute’s value.
Now, you need to add following code in your plugin:
firstly you have to create a file at path:
app/code/Webkul/Test/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="field_sub" xsi:type="array"> <item name="class" xsi:type="string">Webkul\Test\Ui\DataProvider\Product\Form\Modifier\FieldSub</item> <item name="sortOrder" xsi:type="number">10</item> </item> </argument> </arguments> </virtualType> </config>
Now define your attribute’s data provider:
create file at path:
Webkul\Test\Ui\DataProvider\Product\Form\Modifier\FieldSub.php
<?php namespace Webkul\Test\Ui\DataProvider\Product\Form\Modifier; use Magento\Framework\Stdlib\ArrayManager; class FieldSub extends \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier { /** * @var ArrayManager * @since 101.0.0 */ protected $arrayManager; public function __construct( ArrayManager $arrayManager ) { $this->arrayManager = $arrayManager; } public function modifyMeta(array $meta) { $meta = $this->customizeFieldSub($meta); return $meta; } public function modifyData(array $data) { return $data; } protected function customizeFieldSub(array $meta) { $weightPath = $this->arrayManager->findPath('field_sub', $meta, null, 'children'); if ($weightPath) { $meta = $this->arrayManager->merge( $weightPath . static::META_CONFIG_PATH, $meta, [ 'dataScope' => 'field_sub', 'validation' => [ 'required-entry' => true, 'validate-zero-or-greater' => true ], 'additionalClasses' => 'admin__field-small', 'imports' => [ 'disabled' => '!${$.provider}:' . self::DATA_SCOPE_PRODUCT . '.field_main:value' ] ] ); } return $meta; } }
Here,
in di.xml we map the field’s data provider.
in fieldSub.php file,
modifyMeta() calls to modify the attribute, in this function we added additional checks to our field, which are:
data_scope: is the name of the field (field code)
validation: validations which we want to apply on our filed.
additionalClasses : if want to add additional class to our field.
imports: in this we pass the attributes value, like: disabled, product-id, or any other attribute to add in element.
After this flush the cache, and your field (field_sub) will be dependent on other field’s (field_main) value.
I hope this blog will help you with Dependency of One Attribute field on Another Attribute’s field in Admin form for Magento 2. You may also check our wide range of best Magento 2 Extensions.
Please reach out to our team via a support ticket if you have any queries.
Try this and if you have any queries then just comment below 🙂
Be the first to comment.