0

I am adding product attribute programmatically the following way:

use Magento\Framework\App\Bootstrap;
require '/www/public_html/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$eavEavSetupFactory = $obj->create('\Magento\Eav\Setup\EavSetupFactory');
$eavConfig = $obj->create('\Magento\Eav\Model\Config');
$attributeSetFactory = $obj->create('Magento\Eav\Model\Entity\Attribute\SetFactory');

/** @var ProductSetup $productSetup */
$productSetup = $eavEavSetupFactory->create();

$productEntity = $productSetup->getEntityTypeId('catalog_product');
$attributeSetId = $productSetup->getDefaultAttributeSetId($productEntity);

/** @var $attributeSet AttributeSet */
$attributeSet = $attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

$productSetup->addAttribute(
    \Magento\Catalog\Model\Product::ENTITY,
    'custom_text_field',
    [
        'type' => 'text',
        'backend' => '',
        'frontend' => '',
        'label' => 'test_attribute',
        'input' => 'swatch_text',
        'class' => '',
        'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
        'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
        'visible' => true,
        'required' => false,
        'user_defined' => true,
        'default' => '',
        'searchable' => false,
        'filterable' => false,
        'comparable' => false,
        'visible_on_front' => true,
        'used_in_product_listing' => false,
        'unique' => false,
        'apply_to' => ''
    ]
  );

Then I want to create a configurable product from the UI and I get a 500 error on the create page:

2020/01/26 04:06:54 [error] 1099#1099: *13639 FastCGI sent in stderr: "PHP message: PHP Fatal error: Uncaught TypeError: Argument 2 passed to Magento\Framework\View\Element\UiComponentFactory::argumentsResolver() must be of the type array, null given, called in /opt/magento/public_html/vendor/magento/framework/View/Element/UiComponentFactory.php on line 222 and defined in /opt/magento/public_html/vendor/magento/framework/View/Element/UiComponentFactory.php:174 Stack trace: 0 /opt/magento/public_html/vendor/magento/framework/View/Element/UiComponentFactory.php(222): Magento\Framework\View\Element\UiComponentFactory->argumentsResolver('custom_text_fie...', NULL) 1 /opt/magento/public_html/vendor/magento/module-ui/Component/Form/Field.php(85): Magento\Framework\View\Element\UiComponentFactory->create('custom_text_fie...', 'swatch_text', Array) 2 /opt/magento/public_html/vendor/magento/framework/View/Layout/Generator/UiComponent.php(164): Magento\Ui\Component\Form\Field->prepare()

What field am I missing that gets me this NULL value instead of array ?

PS: I am not using the declarative way suggested by Magento as I don't want to use patches to create new attributes as they are created dynamically based on user input. Therefore I am stuck in using the object manager (only way I have found to be still working in 2.3.3).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.