1

I want to set a field value from url params in an input field when I load the admin edit new page. The value for the field "material_id" is a parameter in my url. This is my ui_component:

        <field name="material_id">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="dataType" xsi:type="string">text</item>
                <item name="label" xsi:type="string" translate="true">Material</item>
                <item name="formElement" xsi:type="string">input</item>
                <item name="source" xsi:type="string">specification</item>
                <item name="sortOrder" xsi:type="number">10</item>
                <item name="dataScope" xsi:type="string">material_id</item>
                <item name="validation" xsi:type="array">
                    <item name="required-entry" xsi:type="boolean">true</item>
                </item>
            </item>
        </argument>
    </field>

How can i set the url param to this field???

This is my Dataprovider:

namespace Mcas\DataMaterial\Model\Specification;

class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider { /** * Loaded data cache * * @var array */ protected $loadedData;

/**
 * Data persistor
 * 
 * @var \Magento\Framework\App\Request\DataPersistorInterface
 */
protected $dataPersistor;

/**
 * constructor
 * 
 * @param string $name
 * @param string $primaryFieldName
 * @param string $requestFieldName
 * @param \Mcas\DataMaterial\Model\ResourceModel\Specification\CollectionFactory $collectionFactory
 * @param \Magento\Framework\App\Request\DataPersistorInterface $dataPersistor
 * @param array $meta
 * @param array $data
 */
public function __construct(
    $name,
    $primaryFieldName,
    $requestFieldName,
    \Mcas\DataMaterial\Model\ResourceModel\Specification\CollectionFactory $collectionFactory,
    \Magento\Framework\App\Request\DataPersistorInterface $dataPersistor,
    array $meta = [],
    array $data = []
) {
    $this->dataPersistor = $dataPersistor;
    parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
    $this->collection = $collectionFactory->create();
}

/**
 * Get data
 *
 * @return array
 */
public function getData()
{
    if (isset($this->loadedData)) {
        return $this->loadedData;
    }
    $items = $this->collection->getItems();
    /** @var \Mcas\DataMaterial\Model\Specification $specification */
    foreach ($items as $specification) {
        $this->loadedData[$specification->getId()] = $specification->getData();

    }
    $data = $this->dataPersistor->get('mcas_datamaterial_specification');
    if (!empty($data)) {
        $specification = $this->collection->getNewEmptyItem();
        $specification->setData($data);
        $this->loadedData[$specification->getId()] = $specification->getData();
        $this->dataPersistor->clear('mcas_datamaterial_specification');
    }
    return $this->loadedData;
}

}

How can I set the param value for my field "material_id" Hope to get help. Thanks, Christian

  • The form data cannot e loaded by url params. You need to add data via Dataprovider. – Milind Singh Dec 11 '18 at 7:07
  • Thanks for your answer. Unfortunately I dont know how and where to get/set the url parameter for new datasets – Dr. Christian Kusche Dec 11 '18 at 7:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.