2

I have created a custom module. In which I want to use data modifier. I have written this code but it gives the following error:

Missing required argument $modifiers of Magento\Ui\DataProvider\Modifier\Pool

My Code is Like di.xml

<virtualType name="Vendor\Module\Ui\DataProvider\Customer\Form\Modifier\Pool" type="Magento\Ui\DataProvider\Modifier\Pool">
    <arguments>
        <argument name="modifiers" xsi:type="array">
            <item name="customer" xsi:type="array">
                <item name="class" xsi:type="string">Vendor\Module\Ui\DataProvider\Customer\Form\Modifier\Customer</item>
                <item name="sortOrder" xsi:type="number">120</item>
            </item>
        </argument>
    </arguments>
</virtualType>
<type name="Vendor\Module\Ui\DataProvider\Customer\Form\Modifier\CustomerDataProvider">
    <arguments>
        <argument name="pool" xsi:type="object">Vendor\Module\Ui\DataProvider\Customer\Form\Modifier\Pool</argument>
    </arguments>
</type>
<type name="Vendor\ModuleUi\DataProvider\Customer\Form\Modifier\Customer">
    <arguments>
        <argument name="scopeName" xsi:type="string">customer_form.customer_form</argument>
    </arguments>
</type>

CustomerDataProvider.php

namespace Vendor\Module\Ui\DataProvider\Customer\Form;

use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory;
use Magento\Ui\DataProvider\AbstractDataProvider;
use Magento\Ui\DataProvider\Modifier\ModifierInterface;
use Magento\Ui\DataProvider\Modifier\PoolInterface;
/**
 * DataProvider for product edit form
 */
class CustomerDataProvider extends AbstractDataProvider
{
    /**
     * @var PoolInterface
     */
    private $pool;
    /**
     * @param string $name
     * @param string $primaryFieldName
     * @param string $requestFieldName
     * @param CollectionFactory $collectionFactory
     * @param PoolInterface $pool
     * @param array $meta
     * @param array $data
     */
    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        CollectionFactory $collectionFactory,
        PoolInterface $pool,
        array $meta = [],
        array $data = []
    ) {
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
        $this->collection = $collectionFactory->create();
        $this->pool = $pool;
    }
    /**
     * {@inheritdoc}
     */
    public function getData()
    {
        /** @var ModifierInterface $modifier */
        foreach ($this->pool->getModifiersInstances() as $modifier) {
            $this->data = $modifier->modifyData($this->data);
        }
        return $this->data;
    }
    /**
     * {@inheritdoc}
     */
    public function getMeta()
    {
        $meta = parent::getMeta();
        /** @var ModifierInterface $modifier */
        foreach ($this->pool->getModifiersInstances() as $modifier) {
            $meta = $modifier->modifyMeta($meta);
        }
        return $meta;
    }
}

modifier file Customer.php

<?php
namespace Vendor\Module\Ui\DataProvider\Customer\Form\Modifier;

use Magento\Ui\DataProvider\Modifier\ModifierInterface;
/**
 * Class Prices
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class Customer implements ModifierInterface
{
    /**
     * {@inheritdoc}
     */
    public function modifyMeta(array $meta)
    {
        return $meta;
    }

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

I can't find where it is missing arguments. Please correct way to add modifiers.

Note - I am adding this modifier for customer edit form

2 Answers 2

3

Try with following updated code.

namespace Vendor\Module\Ui\DataProvider\Customer\Form;

use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory;
use Magento\Ui\DataProvider\AbstractDataProvider;
use Magento\Ui\DataProvider\Modifier\ModifierInterface;
use Magento\Ui\DataProvider\Modifier\PoolInterface;
/**
 * DataProvider for product edit form
 */
class CustomerDataProvider extends AbstractDataProvider implements PoolInterface
{

    protected $modifiers = [];

    protected $modifiersInstances = [];

    /**
     * @param string $name
     * @param string $primaryFieldName
     * @param string $requestFieldName
     * @param CollectionFactory $collectionFactory
     * @param PoolInterface $pool
     * @param array $meta
     * @param array $data
     */
    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        CollectionFactory $collectionFactory,
        array $meta = [],
        array $data = []
    ) {
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
        $this->collection = $collectionFactory->create();
    }
    /**
     * {@inheritdoc}
     */
    public function getData()
    {
        /** @var ModifierInterface $modifier */
        foreach ($this->pool->getModifiersInstances() as $modifier) {
            $this->data = $modifier->modifyData($this->data);
        }
        return $this->data;
    }
    /**
     * {@inheritdoc}
     */
    public function getMeta()
    {
        $meta = parent::getMeta();
        /** @var ModifierInterface $modifier */
        foreach ($this->pool->getModifiersInstances() as $modifier) {
            $meta = $modifier->modifyMeta($meta);
        }
        return $meta;
    }  

    /**
     * Retrieve modifiers
     *
     * @return array
     */
    public function getModifiers()
    {
        return $this->modifiers;
    }

    /**
     * Retrieve modifiers instantiated
     *
     * @return ModifierInterface[]
     * @throws LocalizedException
     */
    public function getModifiersInstances()
    {
        if ($this->modifiersInstances) {
            return $this->modifiersInstances;
        }

        foreach ($this->modifiers as $modifier) {
            if (empty($modifier['class'])) {
                throw new LocalizedException(__('Parameter "class" must be present.'));
            }

            if (empty($modifier['sortOrder'])) {
                throw new LocalizedException(__('Parameter "sortOrder" must be present.'));
            }

            $this->modifiersInstances[$modifier['class']] = $this->factory->create($modifier['class']);
        }

        return $this->modifiersInstances;
    }
}
2
  • Thanks for ans. But where I add this file? Commented Jun 13, 2018 at 9:19
  • 1
    Where I add this Pool class in my module? at which location? As per my knowledge Pool is virtual class. May be issue in di.xml file. Commented Jun 13, 2018 at 13:13
0

Your dataprovider namespace is wrong replace following line in your di.xml

<type name="Vendor\Module\Ui\DataProvider\Customer\Form\Modifier\CustomerDataProvider">

with

<type name="Vendor\Module\Ui\DataProvider\Customer\Form\CustomerDataProvider">

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.