我已经成功地在 magento2产品添加/编辑表单中添加了“自定义字段”,但是其值未保存?由于它的值也不会以编辑形式显示。这是我的完整代码
这是我的di.xml文件代码:
出售\ 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>
这是我的NewField.php文件代码:
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;
}
}
字段正确显示,但单击“保存”按钮后,没有影响数据未保存。调试日志中没有错误