Magento 2:产品添加/编辑中不保存自定义字段数据

I have successfully added Custom Field in magento2 Product add/Edit form, but its value is not getting saved ? due to that its value is not displayed in edit form as well. Here is my full code

Here is my di.xml file code :

Vend\Customattrib\etc\adminhtml\di.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <virtualType name="Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Pool">
        <arguments>
            <argument name="modifiers" xsi:type="array">
                <item name="customTab" xsi:type="array">
                    <item name="class" xsi:type="string">Vend\Customattrib\Ui\DataProvider\Product\Form\Modifier\NewField</item>
                    <item name="sortOrder" xsi:type="number">70</item>
                </item>
            </argument>
        </arguments>
    </virtualType>
</config>

Here is my NewField.php file code :

Vend\Customattrib\Ui\DataProvider\Product\Form\Modifier\NewField.php

namespace Vend\Customattrib\Ui\DataProvider\Product\Form\Modifier;
use Magento\Catalog\Model\Locator\LocatorInterface;
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
use Magento\Ui\Component\Form\Fieldset;
use Magento\Ui\Component\Form\Field;
use Magento\Framework\Stdlib\ArrayManager;
use Magento\Framework\UrlInterface;
use Magento\Ui\Component\Container;
use Magento\Ui\Component\Form\Element\DataType\Number;
use Magento\Ui\Component\Form\Element\DataType\Text;
use Magento\Ui\Component\Form\Element\Input;
use Magento\Ui\Component\Form\Element\Select;
use Magento\Ui\Component\Form\Element\MultiSelect;

        class NewField extends AbstractModifier
        {
            private $locator;


            public function __construct(
            LocatorInterface $locator,
            ArrayManager $arrayManager,
            UrlInterface $urlBuilder
            ) {
                $this->locator = $locator;
                $this->arrayManager = $arrayManager;
                $this->urlBuilder = $urlBuilder;
            }

            public function modifyData(array $data)
            {
                return $data;
            }


            public function modifyMeta(array $meta)
            {
                $meta = array_replace_recursive(
                $meta,
                [
                'custom_fieldset' => [
                'arguments' => [
                'data' => [
                'config' => [
                'label' => __('Custom Section'),
                'componentType' => Fieldset::NAME,
                'dataScope' => 'data.product.custom_fieldset',
                'collapsible' => true,
                'sortOrder' => 200,
                ],
                ],
                ],
                'children' => [
                'custom_field' => $this->getCustomField()
                ],
                ]
                ]
                );
                return $meta;
            }

            public function getCustomField()
            {
                return [
                'arguments' => [
                'data' => [
                'config' => [
                'label' => __('Select Question Category'),
                'componentType' => Field::NAME,
                'formElement' => Select::NAME,
                'dataScope' => 'question_category_legal',
                'dataType' => Text::NAME,
                'sortOrder' => 10,
                'options' => $this->_getOptions(),
                'visible' => true,
                'disabled' => false,
                ],
                ],
                ],
                ];
            }
            /* Fetch From Database */
            protected function _getOptions()
            {

                $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
                $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
                $connection = $resource->getConnection();
                $tableName = $resource->getTableName('custom_data');
                $sql    = "SELECT `id`, `opt_name` FROM ".$tableName." WHERE is_active = 1";
                $result = $connection->fetchAll($sql);
                $options        =   array();
                $options[]  =   array('value' => '', 'label' => 'Select Option');
                if($result)
                {
                    foreach($result as $r)
                    {
                        $id             = $r['id'];
                        $opt_name       = $r['opt_name'];
                        $options[]      =   array('value' => $id, 'label' => $opt_name);
                    }
                }

                return $options;
            }

        }   

Field is showing correctly but after hit save button there is no impact data is not saving. There is no error in debug log

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐