使用 EAV 的客户自定义属性未在网格中显示值

Customer custom attribute using EAV is not displaying values in Grid

场景

我正在尝试为 Magento Customer 实现一个自定义属性,它应该接受 boolean 值(True/False、Yes/No...)。
我正在使用 Magento CE 2.2.4。
这是 /app/code/TheVendor_TheModule/.
下自定义模块的一部分 模块的其他组件工作正常。


预期结果


实际结果


屏幕

后端的客户表单视图

客户网格和过滤器视图


代码

<?php

namespace TheVendor\TheModule\Setup;

use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Model\Config;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface {

    const ATTRIBUTE_APPROVED = 'attribute_approved';

    protected $customerSetupFactory;

    private $eavSetupFactory;
    private $eavConfig;
    private $attributeResource;

    public function __construct(
        CustomerSetupFactory $customerSetupFactory, 
        EavSetupFactory $eavSetupFactory, 
        Config $eavConfig, 
        \Magento\Customer\Model\ResourceModel\Attribute $attributeResource
    ){
        $this->eavSetupFactory = $eavSetupFactory;
        $this->eavConfig = $eavConfig;
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeResource = $attributeResource;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, self::ATTRIBUTE_APPROVED, [
            'type' => 'int',
            'label' => 'Attribute Approved',
            'input' => 'boolean',
            'required' => false,
            'visible' => true,
            'system' => false,
            'position' => 9,
            'sort_order' => 9,
            'is_used_in_grid' => true,
            'is_visible_in_grid' => true,
            'is_filterable_in_grid' => true,
            'is_searchable_in_grid' => true,
            //'user_defined' => true, //commented because causing attribute fail on module install
            //'searchable' => true,
            'filterable' => true,
            'comparable' => true,
            'default' => '0',
            //'unique' => 0,
        ]);

        $myAttribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, self::ATTRIBUTE_APPROVED);

        $myAttribute->setData('used_in_forms', ['adminhtml_customer']);

        $this->attributeResource->save($myAttribute);

        $setup->endSetup();

    }
}

尝试和测试

我尝试了以下方法:

测试例程

对于我所做的每项测试,我都遵循以下步骤以确保模块安装正确:


问题

有人对如何处理这个问题或如何检测问题出处有建议吗?

问题已解决

解决方案:

编辑 app/code/TheVendor/TheModule/Setup/InstallData.php 中的 addAttribute(...) 个选项

使用源模型 'Magento\Eav\Model\Entity\Attribute\Source\Boolean' 输入 select

...

$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, self::ATTRIBUTE_APPROVED, [
        'type' => 'int',
        'label' => 'Attribute Approved',

        /** [Solution] Changed from 'boolean' to 'select' */
        'input' => 'select',  

        /** [Solution] Use source model Boolean */
        'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean', 

        'default' => '0',
        'required' => false,
        'visible' => true,
        'system' => false,
        'position' => 9,
        'sort_order' => 9,
        //'user_defined' => true,
        //'searchable' => true,
        'filterable' => true,
        'comparable' => true,
        'is_used_in_grid' => true,
        'is_visible_in_grid' => true,
        'is_filterable_in_grid' => true,
        'is_searchable_in_grid' => true,
        //'unique' => 0,
   ]); 

...

屏幕

希望对您有所帮助!