dpwgzi7987 2019-03-06 09:35
浏览 125

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

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 易语言把MYSQL数据库中的数据添加至组合框
    • ¥20 求数据集和代码#有偿答复
    • ¥15 关于下拉菜单选项关联的问题
    • ¥20 java-OJ-健康体检
    • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
    • ¥15 使用phpstudy在云服务器上搭建个人网站
    • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
    • ¥15 vue3+express部署到nginx
    • ¥20 搭建pt1000三线制高精度测温电路
    • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况