0

I created a custom category attribute "category_special" and placed it in the category form via ui_component. There I can select categories like in the Magento default product form. The attribute is visible in the category form. After saving the category the attribute content is still empty. Do I have to choose a specific backend model for the attribute or something like this?

Setup\InstallData.php

$eavSetup->addAttribute(
                \Magento\Catalog\Model\Category::ENTITY,
                'category_special',
                [
                    'type' => 'text',
                    'input' => 'select',
                    'label' => 'Category Special',
                    'visible' => true,
                    'required' => false,
                    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                    'source' => '',
                    'default' => null,
                ]
            );

view/adminhtml/ui_component/category_form.xml

<field name="category_special">
    <argument name="data" xsi:type="array">
        <item name="options" xsi:type="object">Magento\Catalog\Ui\Component\Product\Form\Categories\Options</item>
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">Category Special</item>
            <item name="componentType" xsi:type="string">field</item>
            <item name="formElement" xsi:type="string">select</item>
            <item name="component" xsi:type="string">Magento_Catalog/js/components/new-category</item>
            <item name="elementTmpl" xsi:type="string">ui/grid/filters/elements/ui-select</item>
            <item name="dataScope" xsi:type="string">category_special</item>
            <item name="filterOptions" xsi:type="boolean">true</item>
            <item name="showCheckbox" xsi:type="boolean">true</item>
            <item name="disableLabel" xsi:type="boolean">true</item>
            <item name="multiple" xsi:type="boolean">true</item>
            <item name="levelsVisibility" xsi:type="number">1</item>
            <item name="sortOrder" xsi:type="number">20</item>
            <item name="required" xsi:type="boolean">false</item>
            <item name="validation" xsi:type="array">
                <item name="required-entry" xsi:type="boolean">false</item>
            </item>
            <item name="listens" xsi:type="array">
                <item name="${ $.namespace }.${ $.namespace }:responseData" xsi:type="string">setParsed</item>
            </item>
        </item>
    </argument>
</field>

1 Answer 1

0

First check if you are getting category ids in post when you submit your form.

when you post your form you will get values of selected categories in form of array. You can implode it and save it in form of comma separated value in your database.

For Example try this in your controller where you get your post data.

$data = $this->getRequest()->getPostValue();

echo "<pre>";print_r($data['category_special']);die; // Check if you are getting category ids from post.

if(isset($data['category_special'])){
    $data['category_special'] = implode(',', $data['category_special']);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.