0

I am new to Magento 2. I am scratching my head why is the data from the Dataprovider doesn't insert to my fields.

DataProvider.php

<?php

namespace Simple\BrandAdmin\Model;
use Simple\BrandAdmin\Model\ResourceModel\Brand\CollectionFactory;
use Magento\Framework\App\Request\DataPersistorInterface;

class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{
    protected $dataPersistor;
    protected $loadedData;
    /**
     * @param string $name
     * @param string $primaryFieldName
     * @param string $requestFieldName
     * @param CollectionFactory $contactCollectionFactory
     * @param array $meta
     * @param array $data
     */
    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        DataPersistorInterface $dataPersistor,
        CollectionFactory $contactCollectionFactory,
        array $meta = [],
        array $data = []
    ) {
        $this->collection = $contactCollectionFactory->create();
        $this->dataPersistor = $dataPersistor;
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
    }

    public function getData()
    {
        //echo '<pre>';
        if (isset($this->loadedData)) {
            return $this->loadedData;
        }

        $items = $this->collection->getItems();

        foreach ($items as $contact) {
            // notre fieldset s'apelle "contact" d'ou ce tableau pour que magento puisse retrouver ses datas :
            $this->loadedData[$contact->getBrandId()] = $contact->getData();
        }

        $data = $this->dataPersistor->get('current_brand');
        if (!empty($data)) {
            $model = $this->collection->getNewEmptyItem();
            $model->setData($data);
            $this->loadedData[$model->getBrandId()] = $model->getData();
            $this->dataPersistor->clear('current_brand');
        }

        return $this->loadedData;

        //return $this->loadedData;

    }
}

Controller.php

<?php
namespace Simple\BrandAdmin\Controller\Adminhtml\BrandForm;
use Magento\Backend\App\Action;
use Simple\BrandAdmin\Model\Brand as Brand;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Registry;

class SuperEdit extends \Magento\Backend\App\Action
{
    protected $registry;
    protected $brand;
    public function __construct(Action\Context $context, Registry $registry, Brand $brand)
    {
        $this->registry = $registry;
        $this->brand = $brand;
        parent::__construct($context);
    }

    /**
     * Edit A Contact Page
     *
     * @return \Magento\Framework\Controller\Result\Redirect
     * @SuppressWarnings(PHPMD.NPathComplexity)
     */
    public function execute()
    {
        $this->_view->loadLayout();
        $this->_view->renderLayout();

        $brandId = $this->getRequest()->getParam('brand_id');
        $resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
        if($brandId) {

            $brand = $this->brand->load($brandId,'brand_id');
            //die('test12');
            //$brand->setData($brand);
            //$contact->setData($contactDatas)->save();
            $this->registry->register('current_brand', $brand);
//            $brandsData = $this->registry->registry('current_brand');
//            var_dump($brandsData);
//            die();
            $resultRedirect = $this->resultRedirectFactory->create();
            return $resultPage->getConfig()->getTitle()->prepend(__($brand->getBrandName()));
            //return $resultRedirect->setPath('*/*/index');
        }
    }
}

UI Component: (simplebrand_superedit_form.xml):

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="provider" xsi:type="string">simple_superedit_form.brandadmin_form_data_source</item>
            <item name="deps" xsi:type="string">simple_superedit_form.brandadmin_form_data_source</item>
        </item>
        <item name="label" xsi:type="string" translate="true">Sample Form</item>
        <item name="layout" xsi:type="array">
            <item name="type" xsi:type="string">tabs</item>
        </item>

        <item name="buttons" xsi:type="array">
            <item name="back" xsi:type="string">Simple\BrandAdmin\Block\Adminhtml\BrandForm\Edit\BackButton</item>
            <item name="delete" xsi:type="string">Simple\BrandAdmin\Block\Adminhtml\BrandForm\Edit\DeleteButton</item>
            <item name="reset" xsi:type="string">Simple\BrandAdmin\Block\Adminhtml\BrandForm\Edit\ResetButton</item>
            <item name="save" xsi:type="string">Simple\BrandAdmin\Block\Adminhtml\BrandForm\Edit\SaveButton</item>
        </item>
    </argument>

    <dataSource name="brandadmin_form_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">Simple\BrandAdmin\Model\DataProvider</argument>
            <argument name="name" xsi:type="string">brandadmin_form_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">brand_id</argument>
            <argument name="requestFieldName" xsi:type="string">brand_id</argument>
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="submit_url" path="*/*/save" xsi:type="url"/>
                </item>
            </argument>
        </argument>
        <argument name="data" xsi:type="array">
            <item name="js_config" xsi:type="array">
                <item name="component" xsi:type="string">Magento_Ui/js/form/provider</item>
            </item>
        </argument>
    </dataSource>

    <fieldset name="General">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Sample Fieldset</item>
            </item>
        </argument>

        <!-- This field represents form id and is hidden -->
        <field name="brand_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="visible" xsi:type="string">false</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" translate="true" xsi:type="string">Brand Id</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="sortOrder" xsi:type="number">10</item>
                    <item name="dataScope" xsi:type="string">brand_id</item>
                    <item name="source" xsi:type="string">brands</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                    </item>
                </item>
            </argument>
        </field>

        <!-- This field has data type 'text' and standard 'input' form element and looks like input -->
        <field name="brand_name">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" translate="true" xsi:type="string">Brand Name</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="sortOrder" xsi:type="number">10</item>
                    <item name="dataScope" xsi:type="string">brand_name</item>
                    <item name="source" xsi:type="string">brands</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                    </item>
                    <item name="imports" xsi:type="array">
                        <item name="brand_name" xsi:type="string">${ $.provider }:data.brand_name</item>
                    </item>
                </item>
            </argument>
        </field>


 <!--       &lt;!&ndash; This field has data type 'text' and standard 'input' form element and looks like input &ndash;&gt;
        <field name="brand_descr">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Brand Description</item>
                     &lt;!&ndash;<item name="default" xsi:type="string">brand_descr</item>&ndash;&gt;
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">textarea</item>
                    <item name="dataScope" xsi:type="string">brand_descr</item>
                    <item name="source" xsi:type="string">brands</item>
                </item>
            </argument>
        </field>

        <field name="brand_image">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Brand URL Image</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="dataScope" xsi:type="string">brand_image</item>

                    <item name="source" xsi:type="string">brands</item>
                </item>
            </argument>
        </field>
-->
    </fieldset>
</form>

I'm stuck for 24hrs tutorials from the net is very confusing.

  • Please print this $this->loadedData in DataProvider.php and check value is blank or not. And make sure your field name are same. Let it check and inform me – Msquare Feb 4 at 3: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.