Scenario
I'm trying to implement a custom attribute for Magento Customer, that should accept boolean values (True/False, Yes/No...).
I'm using Magento CE 2.2.4.
This is part of a custom Module under /app/code/TheVendor_TheModule/
.
The other components of the Module are working correctly.
Expected Result
Actual Result
Label
shows in the Customer Grid but the values are missing.Screens
Customer Form View in back-end
Customer Grid and Filter View
Code
<?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();
}
}
Attempts and Tests
I tried the following:
$customerSetup->addAttribute(...)
options:
'user_defined' => true
. When used, this one is causing the attribute setup to fail without errors.'default' => 0
and 'default' => '0'
'searchable' => true
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy -f
Testing Routine
For every test that I made I followed these steps to ensure Module is being installed correctly:
php bin/magento module:disable TheVendor_TheModule
mage_setup_module
mage_eav_attribute
app/etc/config.php
php bin/magento module:enable TheVendor_TheModule
php bin/magento setup:upgrade
php bin/magento indexer:reindex
php bin/magento cache:clean
Question
Anyone with suggestions on how to handle this or how to detect where the problem is coming from?
Problem Solved
Solution:
Edit addAttribute(...)
options in app/code/TheVendor/TheModule/Setup/InstallData.php
Use source model 'Magento\Eav\Model\Entity\Attribute\Source\Boolean'
with input 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,
]);
...
Screen
Hope this helps!