Magento 2类别属性选择

时间:2016-09-25 15:17:31

标签: magento2

我尝试使用自定义源类创建自定义类别属性,但是选择输入没有选项。我不知道为什么,我错过了什么。我检查并在数据库中存在属性。谢谢你的帮助。

UpgradeData.php

if (version_compare($context->getVersion(), '0.1.3') < 0) {
        $categorySetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $categorySetup->addAttribute(Category::ENTITY, 'category_heroslider', [
            'type' => 'int',
            'label' => 'Category Hero Slider',
            'input' => 'select',
            'source' => xxx\xxx\Model\Entity\Attribute\Source\HeroSlider::class,
            'required' => false,
            'sort_order' => 100,
            'global' => ScopedAttributeInterface::SCOPE_STORE,
            'group' => 'General Information',
            'user_defined' => true,
            'visible' => true,
        ]);
    }

源类

namespace xxx\xxx\Model\Entity\Attribute\Source;

use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;

class xxx extends AbstractSource
{
    /**
     * @var \xxx\xxx\Model\ResourceModel\Slider\CollectionFactory
     */
    private $slidersCollection;

    /**
     * HeroSlider constructor.
     * @param \xxx\xxxx\Model\ResourceModel\Slider\CollectionFactory $slidersCollection
     */
    public function __construct(\xxx\xxx\Model\ResourceModel\Slider\CollectionFactory $slidersCollection)
    {
        $this->slidersCollection = $slidersCollection;
    }

    public function getAllOptions()
    {
        $this->_options = [
            ['label' => __('Yes'), 'value' => 1],
            ['label' => __('asd'), 'value' => 2],
        ];

        return $this->_options;
    }

    public function toOptionArray()
    {
        return [1 => 'test'];
    }


}

category_form.xml

<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="general">
    <field name="category_heroslider">
        <argument name="data" xsi:type="array">
            <item name="options" xsi:type="object">xxx\xxx\Model\Entity\Attribute\Source\HeroSlider</item>
            <item name="config" xsi:type="array">
                <item name="sortOrder" xsi:type="number">100</item>
                <item name="dataType" xsi:type="string">int</item>
                <item name="formElement" xsi:type="string">select</item>
                <item name="label" xsi:type="string" translate="true">Category Hero Slider</item>
            </item>
        </argument>
    </field>
</fieldset>

1 个答案:

答案 0 :(得分:0)

修改您的源模型以实现

Magento\Framework\Data\OptionSourceInterface