I have created a custom category select attribute to list active categories, after choosing a category from the list and saving there is an error message displayed "attribute value is empty. Set the attribute and try again" I have checked the values of the selected category it seems like good
Category_form.xml
<?xml version="1.0" ?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="general">
<field name="category_category_attribute" formElement="select" sortOrder="20">
<argument name="data" xsi:type="array">
<item name="options" xsi:type="object">xxxx\CustomCategoryAttribute\Model\Config\Source\CustomOptionList</item>
</argument>
<settings>
<dataType>text</dataType>
<label translate="true">Category</label>
<dataScope>category</dataScope>
</settings>
</field>
</fieldset>
</form>
Patch file
<?php
namespace xxxx\CustomCategoryAttribute\Setup\Patch\Data;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Catalog\Model\Category;
class CategoryAttribute implements DataPatchInterface
{
protected $_moduleDataSetup;
protected $_eavSetupFactory;
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
EavSetupFactory $eavSetupFactory
) {
$this->_moduleDataSetup = $moduleDataSetup;
$this->_eavSetupFactory = $eavSetupFactory;
}
public function apply()
{
/** @var EavSetup $eavSetup */
$eavSetup = $this->_eavSetupFactory->create(['setup' => $this->_moduleDataSetup]);
$eavSetup->addAttribute(Category::ENTITY, 'category_category_attribute', [
'type' => 'int',
'label' => 'category list',
'input' => 'select',
'default' => '',
'source' => 'xxxx\CustomCategoryAttribute\Model\Config\Source\CustomOptionList',
'sort_order' => 3,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
'group' => 'General Information',
'visible_on_front' => true
]);
}
public static function getDependencies()
{
return [];
}
public function getAliases()
{
return [];
}
}