Added custom customer attribute with patch script but it gives an error
-
16-04-2021 - |
Frage
I added a custom attribute to my customer with a data patch script. The attribute is added in the database tables but when I try to register a new customer I get this error:
main.CRITICAL: Exception message: Missing required argument $options of Magento\Eav\Model\Entity\Attribute\Source\Config.
Did I do something wrong? and how to fix it?
This is my data patch script:
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace OSP\SubUsers\Setup\Patch\Data;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchRevertableInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Setup\CustomerSetup;
/**
*/
class AddDebtorId implements DataPatchInterface, PatchRevertableInterface
{
/**
* @var \Magento\Framework\Setup\ModuleDataSetupInterface
*/
private $moduleDataSetup;
/**
* @var CustomerSetup
*/
private $customerSetupFactory;
/**
* @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
* @param CustomerSetupFactory $customerSetupFactory
*/
public function __construct(
\Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup,
CustomerSetupFactory $customerSetupFactory
) {
/**
* If before, we pass $setup as argument in install/upgrade function, from now we start
* inject it with DI. If you want to use setup, you can inject it, with the same way as here
*/
$this->moduleDataSetup = $moduleDataSetup;
$this->customerSetupFactory = $customerSetupFactory;
}
/**
* {@inheritdoc}
*/
public function apply()
{
$this->moduleDataSetup->getConnection()->startSetup();
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
$customerSetup->addAttribute( \Magento\Customer\Model\Customer::ENTITY, 'debtor_id', [
'type' => 'int',
'input' => 'select',
'label' => 'Debtor ID',
'backend' => '',
'source' => '',
'frontend' => '',
'required' => false,
'default' => 0,
'visible' => true,
'user_defined' => true,
'system' => false,
'is_visible_in_grid' => true,
'is_used_in_grid' => true,
'is_filterable_in_grid' => true,
'is_searchable_in_grid' => true,
'position' => 300
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'debtor_id');
$attribute->addData(
[
'used_in_forms' => ['adminhtml_checkout','adminhtml_customer','customer_account_edit','customer_account_create']
]);
$attribute->save();
$this->moduleDataSetup->getConnection()->endSetup();
}
/**
* {@inheritdoc}
*/
public static function getDependencies()
{
return [
];
}
public function revert()
{
$this->moduleDataSetup->getConnection()->startSetup();
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
$customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'debtor_id');
$this->moduleDataSetup->getConnection()->endSetup();
}
/**
* {@inheritdoc}
*/
public function getAliases()
{
/**
* This internal Magento method, that means that some patches with time can change their names,
* but changing name should not affect installation process, that's why if we will change name of the patch
* we will add alias here
*/
return [];
}
}
Lösung
As you have created an attribute of type "select" you need to set a source for the options.
'source' => \OSP\SubUsers\Model\Entity\Attribute\Source\Options::class
You source file can be like this.
<?php
namespace OSP\SubUsers\Model\Entity\Attribute\Source;
use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
class Options extends AbstractSource
{
/**
* Retrieve All options
*
* @return array[]
*/
public function getAllOptions()
{
if (null === $this->_options) {
$this->_options = [
['label' => __('Option 1'), 'value' => 0],
['label' => __('Option 2'), 'value' => 1],
['label' => __('Option 3'), 'value' => 2],
['label' => __('Option 4'), 'value' => 4],
];
}
return $this->_options;
}
}
If you just want the ID to be display you can set the input to text.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit magento.stackexchange