1

What the code should do is to get fields filled with values because it's edit page. You see on the picture my controller class is loaded and it loads xml file as well but addBreadcrumb doesn't seem to work. I tested $this->_coreRegistry->register('product_comments', $model); and it returns null. enter image description here

Controller class:

<?php
namespace Dev\ProductComments\Controller\Adminhtml\Comment;

use Magento\CatalogImportExport\Model\Import\Proxy\Product\ResourceModel;
use Magento\Framework\App\Action\Context;
use Dev\ProductComments\Model\Comment;
use Dev\ProductComments\Model\ResourceModel\Comment as ResourceComment;
use Magento\Framework\Registry;
//use Magento\Framework\View\Result\PageFactory;
use Magento\Backend\Model\View\Result\Page;

class Add extends \Dev\ProductComments\Controller\Adminhtml\Comment
{

    /**
     * @var Page
     */
    protected $resultPageFactory;
    /**
     * @var Comment
     */
    private $commentModel;
    /**
     * @var ResourceModel
     */
    private $resourceModel;

    public function __construct(
        Context $context,
//        PageFactory $resultPageFactory,
        Page $resultPageFactory,
        Registry $coreRegistry,
        Comment $commentModel,
        ResourceModel $resourceModel
    ) {
        parent::__construct($context, $coreRegistry);
        $this->resultPageFactory = $resultPageFactory;
        $this->commentModel = $commentModel;
        $this->resourceModel = $resourceModel;
    }


    public function execute()
    {
        $id = $this->getRequest()->getParam('comment_id');
        $model = $this->commentModel;

        if ($id) {
            $model->load($id);
            if (!$model->getId()) {
                $this->messageManager->addErrorMessage(__('This Comment no longer exists.'));
                $resultRedirect = $this->resultRedirectFactory->create();
                return $resultRedirect->setPath('*/*/');
            }
        }

        $this->_coreRegistry->register('product_comments', $model);

        $resultPage = $this->resultPageFactory->initLayout();
        $this->initPage($resultPage)->addBreadcrumb(
            $id ? __('Edit Comment') : __('New Comment'),
            $id ? __('Edit Comment') : __('New Comment')
        );
        $resultPage->getConfig()->getTitle()->prepend(__('Product Comments'));
        $resultPage->getConfig()->getTitle()->prepend($model->getId() ? __('Edit Comment %1', $model->getId()) : __('New Comment'));
        return $resultPage;
    }
}

It extend From:

<?php
namespace Dev\ProductComments\Controller\Adminhtml\Comment;

use Magento\CatalogImportExport\Model\Import\Proxy\Product\ResourceModel;
use Magento\Framework\App\Action\Context;
use Dev\ProductComments\Model\Comment;
use Dev\ProductComments\Model\ResourceModel\Comment as ResourceComment;
use Magento\Framework\Registry;
//use Magento\Framework\View\Result\PageFactory;
use Magento\Backend\Model\View\Result\Page;

class Add extends \Dev\ProductComments\Controller\Adminhtml\Comment
{

    /**
     * @var Page
     */
    protected $resultPageFactory;
    /**
     * @var Comment
     */
    private $commentModel;
    /**
     * @var ResourceModel
     */
    private $resourceModel;

    public function __construct(
        Context $context,
//        PageFactory $resultPageFactory,
        Page $resultPageFactory,
        Registry $coreRegistry,
        Comment $commentModel,
        ResourceModel $resourceModel
    ) {
        parent::__construct($context, $coreRegistry);
        $this->resultPageFactory = $resultPageFactory;
        $this->commentModel = $commentModel;
        $this->resourceModel = $resourceModel;
    }


    public function execute()
    {
        $id = $this->getRequest()->getParam('comment_id');
        $model = $this->commentModel;

        if ($id) {
            $model->load($id);
            if (!$model->getId()) {
                $this->messageManager->addErrorMessage(__('This Comment no longer exists.'));
                $resultRedirect = $this->resultRedirectFactory->create();
                return $resultRedirect->setPath('*/*/');
            }
        }

        $this->_coreRegistry->register('product_comments', $model);

        $resultPage = $this->resultPageFactory->initLayout();
        $this->initPage($resultPage)->addBreadcrumb(
            $id ? __('Edit Comment') : __('New Comment'),
            $id ? __('Edit Comment') : __('New Comment')
        );
        $resultPage->getConfig()->getTitle()->prepend(__('Product Comments'));
        $resultPage->getConfig()->getTitle()->prepend($model->getId() ? __('Edit Comment %1', $model->getId()) : __('New Comment'));
        return $resultPage;
    }
}

Data Provider class:

<?php

namespace Dev\ProductComments\Model\Comment;
use Dev\ProductComments\Model\ResourceModel\Comment\CollectionFactory;

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

    public function getData()
    {
        if (isset($this->loadedData)) {
            return $this->loadedData;
        }

        $items = $this->collection->getItems();
        $this->loadedData = array();
        foreach ($items as $contact) {
            $this->loadedData[$contact->getId()]['comment'] = $contact->getData();
        }


        return $this->loadedData;

    }
}

xml file:

<?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">dev_productcomments_comment_form.comment_form_data_source</item>
            <item name="deps" xsi:type="string">dev_productcomments_comment_form.comment_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">Dev\ProductComments\Block\Adminhtml\Comment\Add\BackButton</item>
            <item name="delete" xsi:type="string">Dev\ProductComments\Block\Adminhtml\Comment\Add\DeleteButton</item>
            <item name="reset" xsi:type="string">Dev\ProductComments\Block\Adminhtml\Comment\Add\ResetButton</item>
            <item name="save" xsi:type="string">Dev\ProductComments\Block\Adminhtml\Comment\Add\SaveButton</item>
            <item name="save_and_continue" xsi:type="string">Dev\ProductComments\Block\Adminhtml\Comment\Add\SaveAndContinueButton</item>
        </item>
    </argument>

    <dataSource name="comment_form_data_source">
        <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>
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">Dev\ProductComments\Model\Comment\DataProvider</argument>
            <argument name="name" xsi:type="string">comment_form_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">comment_id</argument>
            <argument name="requestFieldName" xsi:type="string">comment_id</argument>
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="submit_url" xsi:type="url" path="*/*/saveComment"/>
                </item>
            </argument>
        </argument>
    </dataSource>

    <fieldset name="comments">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Comment</item>
            </item>
        </argument>
        <field name="ids">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="visible" xsi:type="boolean">false</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">Comment</item>
                </item>
            </argument>
        </field>
        <field name="name">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Name</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="source" xsi:type="string">Comment</item>
                </item>
            </argument>
            <settings>
                <validation>
                    <rule name="required-entry" xsi:type="boolean">true</rule>
                </validation>
            </settings>
        </field>
        <field name="email">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Email</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="source" xsi:type="string">Comment</item>
                </item>
            </argument>
            <settings>
                <validation>
                    <rule name="required-entry" xsi:type="boolean">true</rule>
                    <rule name="validate-email" xsi:type="boolean">true</rule>
                </validation>
            </settings>
        </field>
        <field name="comment">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Comment</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="source" xsi:type="string">Comment</item>
                </item>
            </argument>
            <settings>
                <validation>
                    <rule name="required-entry" xsi:type="boolean">true</rule>
                </validation>
            </settings>
        </field>
    </fieldset>
</form>
0

For UI component data_source is important for loading data in form. Check your Cms module how data source work. How data source declare in ui component form

Check the following class. getData return data to form.


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

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

    return $this->loadedData;
}
| improve this answer | |
  • I have data source and get data function as well. I've edited my question where you can see them. Still don't understand what causes the issue. Everything looks like I wrote them correctly. – Sandro Mirijanashvili Jan 13 '19 at 22:50
  • You can give here git repo for this module, that will easy for help. – Sohel Rana Jan 14 '19 at 3:19
  • github.com/sandro768/Dev_ProductComments This is my module. If you could help me understand what's wrong, that would be great – Sandro Mirijanashvili Jan 14 '19 at 21:15

Your Answer

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

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