0

I want to add two attributes in the admin for categories, one textarea, and one text input, I already add the attributes in the database, and I use this code

Vendor/Module/view/adminhtml/ui_component/category_form.xml

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="content">

    <field name="cat_short_description">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="class" xsi:type="string">Magento\Catalog\Ui\Component\Category\Form\Element\Wysiwyg</item>
                <item name="formElement" xsi:type="string">wysiwyg</item>
                <item name="wysiwygConfigData" xsi:type="array">
                    <item name="settings" xsi:type="array">
                        <item name="theme_advanced_buttons1" xsi:type="string">bold,italic,|,justifyleft,justifycenter,justifyright,|,fontselect,fontsizeselect,|,forecolor,backcolor,|,link,unlink,image,|,bullist,numlist,|,code</item>
                        <item name="theme_advanced_buttons2" xsi:type="boolean">false</item>
                        <item name="theme_advanced_buttons3" xsi:type="boolean">false</item>
                        <item name="theme_advanced_buttons4" xsi:type="boolean">false</item>
                        <item name="theme_advanced_statusbar_location" xsi:type="boolean">false</item>
                    </item>
                    <item name="files_browser_window_url" xsi:type="boolean">false</item>
                    <item name="height" xsi:type="string">100px</item>
                    <item name="toggle_button" xsi:type="boolean">false</item>
                    <item name="add_variables" xsi:type="boolean">false</item>
                    <item name="add_widgets" xsi:type="boolean">false</item>
                    <item name="add_images" xsi:type="boolean">false</item>
                </item>
                <item name="template" xsi:type="string">ui/form/field</item>
                <item name="source" xsi:type="string">category</item>
                <item name="wysiwyg" xsi:type="boolean">true</item>
                <item name="dataScope" xsi:type="string">cat_short_description</item>
                <item name="sortOrder" xsi:type="number">110</item>
                <item name="rows" xsi:type="number">8</item>
                <item name="label" xsi:type="string" translate="true">Textarea</item>
                <item name="notice" xsi:type="string" translate="true"></item>
                <item name="additionalClasses" xsi:type="string"></item>

            </item>
        </argument>
    </field>
                <field name="title_review">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <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="sortOrder" xsi:type="number">333</item>
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="label" translate="true" xsi:type="string">Second Name</item>
                </item>
            </argument>
        </field>


</fieldset>
</form>

the code is okay but I don't have that Use Default Value checkbox when changing the Store View, what is wrong or how I can add Use Default Value checkbox?

enter image description here

2

I think you forgot to add the "SCOPE_STORE" while creating the custom category attribute.

You must need to add the

\Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE

For the global variable

Check the below code.

$eavSetup->addAttribute(
    \Magento\Catalog\Model\Category::ENTITY,
    'cat_short_description',
    [
        'type' => 'varchar',
        'label' => 'Category Short Description',
        'input' => 'textarea',
        'sort_order' => 100,
        'source' => '',
        'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
        'visible' => true,
        'required' => false,
        'user_defined' => false,
        'default' => null,
        'group' => 'General Information',
        'backend' => ''
    ]
);

Or you can change directly in the database by the below way (Not Tested).

Find your attribute code in "eav_attribute" and search it's attribute_id in "catalog_eav_attribute" and change the is_globle to 0 (Zero), Then reindex and check.

Hope this will help you.

|improve this answer|||||
  • Hi thank you, and if this attribute is already created, is there anyway to modify? I can add this through the phpmyadmin? – Robert Mar 11 at 9:01
  • You can do it by UpgradeData script, Check this: webkul.com/blog/… – Kishor Thummar Mar 11 at 9:22
  • Here you can use \Magento\Catalog\Model\Category::ENTITY, instead of \Magento\Catalog\Model\Product::ENTITY, – Kishor Thummar Mar 11 at 9:28
  • I got it but is not anyway to change this directly in database? – Robert Mar 11 at 9:30
  • I have updated my answer, Please check. – Kishor Thummar Mar 11 at 9:58
0

Try to extend Magento\Catalog\Model\Category\DataProvider.

Example :

<?php

namespace Your\Namespace;

use Magento\Catalog\Model\Category\DataProvider as CategoryDataProvider;

class DataProvider extends CategoryDataProvider
{

    /**
     * Display "Use Default Value" checkbox
     *
     * @return array
     */
    protected function getFieldsMap()
    {
        $parentFieldMap = parent::getFieldsMap();
        array_push($parentFieldMap['display_settings'], 'your_attribute');

        return $parentFieldMap;
    }
}
|improve this answer|||||

Your Answer

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

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