3

I am using Magento 2.1.5

I need to add a dynamic rows grid on the catalog product page

enter image description here

I am using the below di.xml for adding a dynamic row on product_form

app/code/vendor/module/etc/adminhtml/di.xml

<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="Duration" xsi:type="array">
                    <item name="class" xsi:type="string">[vendor]\[module]\Ui\DataProvider\Product\Modifier\Duration</item>
                    <item name="componentType" xsi:type="string">container</item>
                </item>
            </argument>
        </arguments>
    </virtualType>

     <type name="[vendor]\[module]\Ui\DataProvider\Product\Modifier\Duration">
        <arguments>
            <argument name="formName" xsi:type="string">product_form</argument>
            <argument name="dataScopeName" xsi:type="string">product_form.product_form</argument>
            <argument name="dataSourceName" xsi:type="string">product_form.product_form_data_source</argument>
        </arguments>
    </type>
</config>

My question is what can I do in the data provider to get these duration options?

1 Answer 1

-1

Add Form directory before Modifier [vendor][module]\Ui\DataProvider\Product\Form\Modifier\Duration

and add this in the Duration File

    <?php
[vendor]\[module]\Ui\DataProvider\Product\Form\Modifier\Duration

use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
use Magento\Catalog\Model\Locator\LocatorInterface;
use Magento\Catalog\Model\Product\Type as CatalogType;
use Magento\Ui\DataProvider\Modifier\ModifierInterface;
use Magento\Ui\DataProvider\Modifier\ModifierFactory;

class Duration extends AbstractModifier
{

const CHILDREN_PATH = '[your_system_path]/children';
const CONTAINER_FILES = 'container_files';

/**
 * @var ModifierFactory
 */
protected $modifierFactory;

/**
 * @var array
 */
protected $modifiers = [];

/**
 * @var ModifierInterface[]
 */
protected $modifiersInstances = [];

/**
 * @var LocatorInterface
 */
protected $locator;

/**
 * Composite constructor.
 * @param ModifierFactory $modifierFactory
 * @param LocatorInterface $locator
 * @param array $modifiers
 */
public function __construct(
    ModifierFactory $modifierFactory,
    LocatorInterface $locator,
    array $modifiers = []
) {
    $this->modifierFactory = $modifierFactory;
    $this->locator = $locator;
    $this->modifiers = $modifiers;
}

/**
 * @param array $data
 * @return array
 */
public function modifyData(array $data)
{
    foreach ($this->getModifiers() as $modifier) {
        $data = $modifier->modifyData($data);
    }

    return $data;
}

/**
 * @param array $meta
 * @return array
 */
public function modifyMeta(array $meta)
{
    foreach ($this->getModifiers() as $modifier) {
        $meta = $modifier->modifyMeta($meta);
    }


    return $meta;
}


/**
 * @return \Magento\Ui\DataProvider\Modifier\ModifierInterface[]
 */
protected function getModifiers()
{
    if (empty($this->modifiersInstances)) {
        foreach ($this->modifiers as $modifierClass) {
            $this->modifiersInstances[$modifierClass] = $this->modifierFactory->create($modifierClass);
        }
    }

    return $this->modifiersInstances;
}
}
1
  • Please explain your answer with why and not just what. Mar 31, 2022 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.