Magento 2:-将选择属性添加到类别


9

我想将select / dropdown属性添加到类别并在后端显示它,以便商店所有者选择它。经过一番研究,我发现以下几点:

  • 通过Setupscript创建属性
  • 通过UI组件声明包括它

这就是我所做的:

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    /** @var $eavSetup \Magento\Eav\Setup\EavSetup */
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Category::ENTITY,
        'attribute_code',
        [
            'type' => 'int',
            'label' => 'Top Navigation CMS Block',
            'input' => 'select',
            'source' => 'Magento\Catalog\Model\Category\Attribute\Source\Page',
            'required' => false,
            'sort_order' => 10,
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'group' => 'MyOwnGroup'
        ]
    );
}

查看/adminhtml/ui_component/category_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="content">
        <field name="attribute_code">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortOrder" xsi:type="number">61</item>
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="label" xsi:type="string" translate="true">Top Navigation CMS Block</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

但这还不够,后端中的选项卡排序混杂在一起。下拉菜单也会被渲染,但是没有选项。因此我发现您还必须进行编辑Magento\Catalog\Model\Category\DataProvider::getFieldsMap()。为此,我们必须通过di.xml覆盖它:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Catalog\Model\Category\DataProvider" type="Vendor\Module\Model\Category\DataProvider" />
</config>

在模块自己的数据提供程序中,您必须在函数中添加属性。然后工作。

所以这是我的问题:

  • 我认为您不必真的覆盖此数据提供程序以使其正常运行,必须有更好的方法
  • 您必须在代码中的3个地方声明新属性(设置脚本,UI组件,数据提供程序),我不相信这么多的开销是必要的吗?
  • 为什么该属性有多余的信息?例如,标签位于安装脚本和ui组件内部,将从ui组件声明中读取后端中的标签。

我已经创建了下拉类型的类别属性,没有要求超过DataProvider。
Dhiren Vasoya '17

您可以发布解决方案吗?
测试人员

我发布答案,请检查。
Dhiren Vasoya

Answers:


3

您需要向ui-component config添加节点“选项”

<field name="roflcopter">
    <argument name="data" xsi:type="array">
        <item name="options" xsi:type="object">Magento\Catalog\Model\Category\Attribute\Source\Page</item>
        <item name="config" xsi:type="array">
            <item name="sortOrder" xsi:type="number">70</item>
            <item name="dataType" xsi:type="string">string</item>
            <item name="formElement" xsi:type="string">select</item>
            <item name="label" xsi:type="string" translate="true">Roflcopter</item>
        </item>
    </argument>
</field>

从此处获取Magento2.1类别自定义属性下拉列表


2

我使用以下代码在InstallData.php中创建属性。

供应商\扩展名\设置\ InstallData.php

<?php
namespace Vendor\Extension\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)
    {
        $setup->startSetup();
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        //Category Attribute Create Script
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Category::ENTITY,
            'attribute_id',
            [
                'group' => 'Mygroup',
                'label' => 'Attribute Label',
                'type'  => 'int',
                'input' => 'boolean',
                'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                'source' => '',
                'required' => false,
                'sort_order' => 1,
                'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
                'used_in_product_listing' => true,
                'visible_on_front' => false
            ]
        );

        $setup->endSetup();
    }
}

然后创建一个文件category_form.xml

供应商\扩展\视图\ adminhtml \ ui_component \ category_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="attribute_id">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Attribute Label</item>
                <item name="collapsible" xsi:type="boolean">true</item>
                <item name="sortOrder" xsi:type="number">25</item>
            </item>
        </argument>
        <field name="attribute_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortOrder" xsi:type="number">20</item>
                    <item name="dataType" xsi:type="string">boolean</item>
                    <item name="formElement" xsi:type="string">checkbox</item>
                    <item name="source" xsi:type="string">category</item>
                    <item name="prefer" xsi:type="string">toggle</item>
                    <item name="valueMap" xsi:type="array">
                        <item name="true" xsi:type="string">1</item>
                        <item name="false" xsi:type="string">0</item>
                    </item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">false</item>
                    </item>
                    <item name="default" xsi:type="string">0</item>
                    <item name="label" xsi:type="string" translate="true">Attribute Label</item>
                </item>
            </argument>
        </field>
</form>

如何为下拉菜单创建自定义值并保存这些值?请参阅:magento.stackexchange.com/questions/181756/...
heady12
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.