Magento 2产品编辑/添加管理表单自定义部分


9

我正在学习UI组件。

我想在产品编辑/添加后端表单中添加自定义部分,因此

我创建了以下文件。

供应商/模块/视图/adminhtml/ui_component/product_form.xml

    <?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">
        <fieldset name="mobile">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string" translate="true">Time</item>
                    <item name="provider" xsi:type="string">product</item>
                    <item name="dataScope" xsi:type="string">data.product</item>
                    <item name="sortOrder" xsi:type="number">2</item>
                    <item name="collapsible" xsi:type="boolean">true</item>
                    <item name="opened" xsi:type="boolean">false</item>
                    <item name="ns" xsi:type="string">product_form</item>
                </item>
            </argument>
            <container name="monday_time_group">
                <argument name="data" xsi:type="array">
                    <item name="type" xsi:type="string">group</item>
                    <item name="config" xsi:type="array">
                        <item name="additionalClasses" xsi:type="string">admin__control-grouped-date</item>
                        <item name="component" xsi:type="string">Magento_Ui/js/form/components/group</item>
                        <item name="label" xsi:type="string" translate="true">Monday</item>
                        <item name="required" xsi:type="boolean">false</item>
                        <item name="sortOrder" xsi:type="number">220</item>
                        <item name="breakLine" xsi:type="boolean">false</item>
                        <item name="scopeLabel" xsi:type="string">[STORE VIEW]</item>
                    </item>
                </argument>
                <field name="monday_design_from">
                    <argument name="data" xsi:type="array">
                        <item name="options" xsi:type="object">Vendor\Module\Model\Config\Source\TimeSetup</item>
                        <item name="config" xsi:type="array">
                            <item name="label" xsi:type="string" translate="true">Monday</item>
                            <item name="additionalClasses" xsi:type="string">admin__field-date</item>
                            <item name="sortOrder" xsi:type="number">230</item>
                            <item name="dataType" xsi:type="string">string</item>
                            <item name="formElement" xsi:type="string">select</item>
                        </item>
                    </argument>
                </field>
                 <field name="monday_design_to">
                    <argument name="data" xsi:type="array">
                        <item name="options" xsi:type="object">Vendor\Module\Model\Config\Source\TimeSetup</item>
                        <item name="config" xsi:type="array">
                            <item name="additionalClasses" xsi:type="string">admin__field-date</item>
                            <item name="sortOrder" xsi:type="number">240</item>
                            <item name="dataType" xsi:type="string">string</item>
                            <item name="formElement" xsi:type="string">select</item>
                        </item>
                    </argument>
                </field>
            </container>
        </fieldset>
    </form>

供应商\模块\模型\配置\源\ TimeSetup.php

<?php
namespace Vendor\Module\Model\Config\Source;

class TimeSetup extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{

    protected $_optionsData;

    /**
     * getAllOptions
     *
     * @return array
     */
    public function getAllOptions()
    {
        if ($this->_options === null) {
            $this->_options = [
                ['value' => 'one', 'label' => __('one')],
                ['value' => 'two', 'label' => __('two')]
            ];
        }
        return $this->_options;
    }
    final public function toOptionArray()
    {
         return array(
            array('value' => 'one', 'label' => __('one')),
            array('value' => 'two', 'label' => __('two'))
         );
     }
}

它可以按照我的意愿正确显示在后端,但是当尝试保存产品时,并没有保存该字段值。

我是否需要创建属性“ monday_time_group ”?这是正确的方法吗?如何获得产品前端的值?

更新:

我已经创建了属性monday_time_group,但仍然无法正常工作。


有人可以帮忙吗?
Kaushal Suthar '17

其他字段的值是否保存?
Suresh Chikani

@SHPatel:其他默认的Magento字段工作正常,仅不保存这两个字段的值。
Kaushal Suthar '17

您是否从后端创建了这些属性?
Suresh Chikani

1
我会尝试使用插件。在di.xml中,您可以输入以下内容:<type name =“ Magento \ Catalog \ Model \ ResourceModel \ Product”> <plugin name =“ AddFilter” type =“ Namespace \ Modulename \ Model \ ResourceModel \ Product” />然后使用devdocs.magento.com/guides/v2.0/extension-dev-guide/…作为make和afterSave函数的参考,并在其中放置逻辑以获取数据并保存。不完美,但应该可以。

Answers:


7

好吧,终于我自己解决了这是模块的完整代码...

registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);

等/ module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="1.0.0"></module>
</config>

Setup / InstallData.php

<?php

namespace Vendor\Module\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;
    public function __construct(
        EavSetupFactory $eavSetupFactory
    )
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
          \Magento\Catalog\Model\Product::ENTITY,
          "testone",
          [
          'group' => "",
          'label' => "Test One",
          'is_html_allowed_on_front' => true,
          'default' => '1',
          'note' => '',
          'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
          'visible' => true,
          'required' => false,
          'user_defined' => false,
          'searchable' => false,
          'filterable' => false,
          'comparable' => false,
          'visible_on_front' => true,
          'visible_in_advanced_search' => false,
          'unique' => false,
          "frontend_class" => "",
          "used_in_product_listing" => true,
          "input" => "select",
          "type" => "varchar",
          "source" => "Vendor\Module\Model\Config\Source\TimeSetup",
          'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend'
          ]
      );
        $eavSetup->addAttribute(
          \Magento\Catalog\Model\Product::ENTITY,
          "testtwo",
          [
          'group' => "",
          'label' => "Test Two",
          'is_html_allowed_on_front' => true,
          'default' => '1',
          'note' => '',
          'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
          'visible' => true,
          'required' => false,
          'user_defined' => false,
          'searchable' => false,
          'filterable' => false,
          'comparable' => false,
          'visible_on_front' => true,
          'visible_in_advanced_search' => false,
          'unique' => false,
          "frontend_class" => "",
          "used_in_product_listing" => true,
          "input" => "select",
          "type" => "varchar",
          "source" => "Vendor\Module\Model\Config\Source\TimeSetup",
          'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend'
          ]
      );
    }
}

型号\ Config \ Source \ TimeSetup.php

<?php
namespace Vendor\Module\Model\Config\Source;

class TimeSetup extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{

    protected $_optionsData;

    /**
     * getAllOptions
     *
     * @return array
     */
    public function getAllOptions()
    {
        if ($this->_options === null) {
            $this->_options = [
                ['value' => 'one', 'label' => __('one')],
                ['value' => 'two', 'label' => __('two')]
            ];
        }
        return $this->_options;
    }
    final public function toOptionArray()
    {
         return array(
            array('value' => 'one', 'label' => __('one')),
            array('value' => 'two', 'label' => __('two'))
         );
     }
}

查看/adminhtml/ui_component/product_form.xml

<?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">
    <fieldset name="testingproduct">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Testing Group</item>
                <item name="provider" xsi:type="string">product</item>
                <item name="dataScope" xsi:type="string">data.product</item>
                <item name="sortOrder" xsi:type="number">2</item>
                <item name="collapsible" xsi:type="boolean">true</item>
                <item name="opened" xsi:type="boolean">false</item>
                <item name="ns" xsi:type="string">product_form</item>
            </item>
        </argument>
        <container name="testing_group">
            <argument name="data" xsi:type="array">
                <item name="type" xsi:type="string">group</item>
                <item name="config" xsi:type="array">
                    <item name="formElement" xsi:type="string">container</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/form/components/group</item>
                    <item name="label" xsi:type="string" translate="true">Testing Group</item>
                    <item name="required" xsi:type="boolean">false</item>
                    <item name="sortOrder" xsi:type="number">220</item>
                    <item name="breakLine" xsi:type="boolean">false</item>
                    <item name="scopeLabel" xsi:type="string">[STORE VIEW]</item>
                </item>
            </argument>
            <field name="testone">
                <argument name="data" xsi:type="array">
                    <item name="options" xsi:type="object">Vendor\Module\Model\Config\Source\TimeSetup</item>
                    <item name="config" xsi:type="array">
                        <item name="label" xsi:type="string" translate="true">Testing Row</item>
                        <item name="sortOrder" xsi:type="number">230</item>
                        <item name="dataType" xsi:type="string">string</item>
                        <item name="formElement" xsi:type="string">select</item>
                    </item>
                </argument>
            </field>
             <field name="testtwo">
                <argument name="data" xsi:type="array">
                    <item name="options" xsi:type="object">Vendor\Module\Model\Config\Source\TimeSetup</item>
                    <item name="config" xsi:type="array">
                        <item name="sortOrder" xsi:type="number">240</item>
                        <item name="dataType" xsi:type="string">string</item>
                        <item name="formElement" xsi:type="string">select</item>
                    </item>
                </argument>
            </field>
        </container>
    </fieldset>
</form>

使用上面的代码,可以正确保存数据,现在我面临的唯一问题是,这些属性也出现在“常规”部分以及我创建的自定义部分“测试组”中,到目前为止,我已经使用“常规”部分隐藏了这些属性CSS。


如何向现有部分添加字段?例如,我需要在产品详细信息部分添加一个文本字段。怎么做?
Jafar Pinjar

2

从我这里看到的内容来看,您的属性没有什么特别的。
他们是普通的。
您可以简单地添加属性monday_design_frommonday_design_from并将它们放在单独的组中。
我建议您通过代码而不是手动执行此操作,因为我假设您将对这些属性有逻辑。

这是有关如何执行此操作的示例:https : //magento.stackexchange.com/a/162115/146

如果您希望属性显示在单独的部分中,则唯一需要更改的就是在配置数组中添加以下内容

'group' => 'Time',

♦感谢您的答复,是的,关于属性没有什么特别的,我想将它们显示在“自定义”部分中,但是两个应该在同一个组中,就像我们在“从中设置新设计”属性中看到的那样,其中有两个日期框在同一行中,我想以这种方式显示我的属性。我总共要创建16个属性,并且两个属性将位于我的自定义部分的同一组中,因此总共将有8行,每行中将有一个标签和两个下拉属性。希望您能理解我的要求,如果您想分享给我它的屏幕截图,请告诉我...
Kaushal Suthar,2017年

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.