I am trying to make a UI component form for creating products in Magento 2.1 module. However, I stuck with the problem that it throws this error:
Fatal error: Method
Magento\Ui\TemplateEngine\Xhtml\Result::__toString() must not throw an
exception, caught Error: Call to a member function addFieldToFilter()
on null in
/home/nikolay/Projects/project_name/vendor/magento/module-ui/Component/Wrapper/UiComponent.php
on line 0
I think that it's causing an error because I don't have any collection to pass for DataProvider. But the thing is that I don't need one. So I am trying to figure out how to avoid this error without having any collection to pass.
/Model/DataProvider.php
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Company_Name\ProductParser\Model;
use Magento\Ui\DataProvider\AbstractDataProvider;
/**
* Class DataProvider
*/
class DataProvider extends AbstractDataProvider
{
/**
* @param string $name
* @param string $primaryFieldName
* @param string $requestFieldName
* @param array $data
*/
public function __construct(
$name,
$primaryFieldName,
$requestFieldName,
array $data = []
) {
parent::__construct($name, $primaryFieldName, $requestFieldName, $data);
}
/**
* {@inheritdoc}
*/
public function getData()
{
return [];
}
}
view/adminhtml/layout/productparser_parseproduct_index.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<title>
Parse
</title>
</head>
<body>
<referenceContainer name="content">
<uiComponent name="parse_form"/>
</referenceContainer>
</body>
</page>
view/adminhtml/ui_component/parse_form.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<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">parse_form.parse_form_data_source</item>
<item name="deps" xsi:type="string">parse_form.parse_form_data_source</item>
<item name="namespace" xsi:type="string">parse_form</item>
</item>
<item name="label" xsi:type="string" translate="true">Category Information</item>
<!--<item name="buttons" xsi:type="array">
<item name="delete" xsi:type="string">Magento\Catalog\Block\Adminhtml\Category\Edit\DeleteButton</item>
<item name="save" xsi:type="string">Magento\Catalog\Block\Adminhtml\Category\Edit\SaveButton</item>
</item>-->
<!--<item name="reverseMetadataMerge" xsi:type="boolean">true</item>-->
<item name="config" xsi:type="array">
<item name="dataScope" xsi:type="string">data</item>
<item name="namespace" xsi:type="string">parse_form</item>
</item>
<item name="template" xsi:type="string">templates/form/collapsible</item>
</argument>
<dataSource name="parse_form_data_source">
<argument name="dataProvider" xsi:type="configurableObject">
<argument name="class" xsi:type="string">Company_Name\ProductParser\Model\DataProvider</argument>
<argument name="name" xsi:type="string">parse_form_data_source</argument>
<argument name="primaryFieldName" xsi:type="string">entity_id</argument>
<argument name="requestFieldName" xsi:type="string">id</argument>
<argument name="data" xsi:type="array">
<!--<item name="js_config" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
<item name="componentType" xsi:type="string">field</item>
</item>-->
<!--<item name="config" xsi:type="array">
<item name="submit_url" xsi:type="string">admin/productparser/create</item>
<!–<item name="validate_url" xsi:type="string">admin/productparser/validate</item>–>
</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="data">
<!-- This field represents form id and is hidden -->
<field name="entity_id">
<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">parse_form</item>
</item>
</argument>
</field>
<!-- This field has data type 'text' and standard 'input' form element and looks like input -->
<field name="title">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string">Some text</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">parse_form</item>
</item>
</argument>
</field>
</fieldset>
</form>
Best Answer
Not sure if this will completely solve your problem, but it should take you a step further.
Add this method in your data provider
Since you are extending
AbstractDataProvider
you need to override the method.If something else crashes, you might need to do the same thing for other methods involving
$this->getCollection()
.I'm thinking one of these methods
addField
,addOrder
,setLimit
,removeField
,removeAllFields
,count