タイプのMagento 2で属性を作成しましたmultiselectが、カスタムソースモデルによってその属性を設定したいと思います。
これをMagento 1から覚えているのですが、これを行うには、データベースの属性を手動で編集しsource_model、をソースモデルのパスに設定する必要がありました。
しかし、Magento 2でこれを行うと、エラーが発生します。に変更source_modelしeav_attributeましたVendor\Module\Model\Config\Source\Product\Attributesが、管理者で製品を編集しようとすると、次のエラーが発生します。
Fatal error: Uncaught Error: Call to undefined method Vendor\Module\Model\Config\Source\Product\Attributes::setAttribute() in .../vendor/magento/module-eav/Model/Entity/Attribute/AbstractAttribute.php:547
Stack trace: #0 .../var/generation/Magento/Catalog/Model/ResourceModel/Eav/Attribute/Interceptor.php(1129): Magento\Eav\Model\Entity\Attribute\AbstractAttribute->getSource()
#1 .../vendor/magento/module-backend/Block/Widget/Form.php(232): Magento\Catalog\Model\ResourceModel\Eav\Attribute\Interceptor->getSource()
#2 .../vendor/magento/module-backend/Block/Widget/Form.php(201): Magento\Backend\Block\Widget\Form->_applyTypeSpecificConfig('multiselect', Object(Magento\Framework\Data\Form\Element\Multiselect), Object(Magento\Catalog\Model\ResourceModel\Eav\Attribute\Interceptor))
#3 .../vendor/magento/module-catalog/Block/Adminhtml/Product/Edit/Tab/Attributes.php(51): Magento\Backend\Block\Widget\Form->_setFieldset(Array, Object(Magento\Framework\Data\Form\El in .../vendor/magento/module-eav/Model/Entity/Attribute/AbstractAttribute.php on line 547カスタムソースモデルで製品属性を作成する方法を知っている人はいますか?
編集:
現在のソースモデル:
use Vendor\Module\Model\Config\Source\AbstractSource;
use Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory;
use Magento\Eav\Model\Entity\Attribute;
use Magento\Eav\Model\Entity\TypeFactory;
/**
 * Class Attributes
 */
class Attributes extends AbstractSource
{
    /**
     * @var AttributeFactory
     */
    protected $attributeFactory;
    /**
     * @var TypeFactory
     */
    protected $eavTypeFactory;
    /**
     * Attributes constructor.
     * @param AttributeFactory $attributeFactory
      @param TypeFactory $typeFactory
     */
    public function __construct(
        AttributeFactory $attributeFactory,
        TypeFactory $typeFactory
    )
    {
        $this->attributeFactory = $attributeFactory;
        $this->eavTypeFactory = $typeFactory;
    }
    /**
     * @return array
     */
    public function toArray()
    {
        $arr = [];
        $entityType = $this->eavTypeFactory->create()->loadByCode('catalog_product');        
        $collection = $this->attributeFactory->create()->getCollection();
        $collection->addFieldToFilter('entity_type_id', $entityType->getId());
        $collection->setOrder('attribute_code');
        /** @var Attribute $attribute */
        foreach ($collection as $attribute) {
            $arr[$attribute->getAttributeId()] = $attribute->getFrontendLabel();
        }
        return $arr;
    }
}クラスVendor\Module\Model\Config\Source\AbstractSource:
namespace Vendor\Module\Model\Config\Source;
abstract class AbstractSource implements \Magento\Framework\Option\ArrayInterface
{
    /**
     * Options getter
     * @return array
     */
    final public function toOptionArray()
    {
        $arr = $this->toArray();
        $ret = [];
        foreach ($arr as $key => $value) {
            $ret[] = [
                'value' => $key,
                'label' => $value
            ];
        }
        return $ret;
    }
    /**
     * Get options in "key-value" format
     * @return array
     */
    public function toArray()
    {
        return [];
    }
}
                  現在のソースモデルコードを投稿します。
                
                
                  
                    —
                    Ryan Hoerr 2016年
                    
                  
                
              
                  ソースモデルとそれが拡張する抽象クラスを追加しました。抽象クラスは、
                
                  
                    —
                    Giel Berkers 2016年
                    
                  
                
              toOptionArray()出力を生成するメソッドにすぎません。