Magento CE 2.1 Not able to add Custom attribute to category dynamically like Magento 1 or Magento 2.0 version
I had to go to here to add xml part to get the fields
/vendor/magento/module-catalog/view/adminhtml/ui_component/category_form.xml
I don't think its good idea to add xml code everytime you add new attribute
Even after adding xml code.. there is an issue for custom image attribute. On Adminhtml/Catalog/Category/Image/Upload controller
I am having similar issues in CE 2.1. I've had issues with the same controller. It seems to have a hardcoded reference to 'image'. Is there some other controller we should be using when adding additional image attributes to categories?
I am also having problems when trying to add attributes that have custom source models. Could you please provide some documentation on how this should be done? Is it correct to extend \Magento\Catalog\Model\Category\DataProvider and to have to override the getFieldsMap() function? There seems to be a hardcoded array within the original function.
Yes right.. anyhow I figured it out and you are right I got to extend
Dataprovider.php as well.
But it should me something dynamic that it adds the attribute direct in
fieldset.
On Jul 4, 2016 9:49 PM, "Danielc3" [email protected] wrote:
I am having similar issues in CE 2.1. I've had issues with the same
controller. It seems to have a hardcoded reference to 'image'. Is there
some other controller we should be using when adding additional image
attributes to categories?I am also having problems when trying to add attributes that have custom
source models. Could you please provide some documentation on how this
should be done? Is it correct to extend
\Magento\Catalog\Model\Category\DataProvider and to have to override the
getFieldsMap() function? There seems to be a hardcoded array within the
original function.—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/magento/magento2/issues/5438#issuecomment-230322318,
or mute the thread
https://github.com/notifications/unsubscribe/AMRmmWj_iUCf8bzBUvyWlUof578Ad9rcks5qSTKGgaJpZM4JEHug
.
For "Custom Source Model", we do not need to extend DataProvider. Just do as sample xml below:

I'm having same issue with adding image attribute to category. Seems we have to create a custom controller. Not sure why you guys made things more complex. How was easy in magento 2.0!
@guz-anton please check if it has something to do with ui components
Hi,
I have same problem. Do you have solution?
Hi,
I have one more problem, We try add a image attribute for categories:
<field name="thumb_nail"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="dataType" xsi:type="string">string</item> <item name="source" xsi:type="string">category</item> <item name="label" xsi:type="string" translate="true">Category Thumbnail</item> <item name="visible" xsi:type="boolean">true</item> <item name="formElement" xsi:type="string">fileUploader</item> <item name="elementTmpl" xsi:type="string">ui/form/element/uploader/uploader</item> <item name="previewTmpl" xsi:type="string">Magento_Catalog/image-preview</item> <item name="required" xsi:type="boolean">false</item> <item name="sortOrder" xsi:type="number">40</item> <item name="uploaderConfig" xsi:type="array"> <item name="url" xsi:type="url" path="catalog/category_image/upload"/> </item> </item> </argument> </field>
in file: vendor\magento\module-catalog\viewadminhtml\ui_component.xml
And code add attribute
$eavSetup->addAttribute(
    \Magento\Catalog\Model\Category::ENTITY,
    'thumb_nail',
    [
            'type' => 'varchar',
            'label' => 'Thumbnail',
            'input' => 'image',
             'backend' => 'Magento\Catalog\Model\Category\Attribute\Backend\Image',
            'required' => false,
            'sort_order' => 50,
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'group' => 'General Information',
    ]
    );
When I checked in controller(vendor\magento\module-catalog\Controller\Adminhtml\Category\Image\Upload.php), I see a hard code:
try {
$result = $this->imageUploader->saveFileToTmpDir('image');
        $result['cookie'] = [
            'name' => $this->_getSession()->getName(),
            'value' => $this->_getSession()->getSessionId(),
            'lifetime' => $this->_getSession()->getCookieLifetime(),
            'path' => $this->_getSession()->getCookiePath(),
            'domain' => $this->_getSession()->getCookieDomain(),
        ];
    } catch (\Exception $e) {
        $result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
    }
When we try upload a image it show a bug

It's so easy to add a attribute for category in magento 2.0.x, Why you make it so hard in magento 2.1 :(
The problem with the 'image' attribute type is that the attribute code 'image' has been hard-coded in many places which means that the category image attribute backend model actually only works with one specific attribute-code (which happens to be called 'image' as well).
@allanpaiste,
Do you know when your pull request will make it to release?
I have no clue :) I would say that one is better off just patching the changes into their installation (which is how we decided to roll) and remove them on the next release.
@allanpaiste,
Just patched-in #5978 using di.xml 
Also, Category::getImageUrl($attributeCode = 'image') will be very useful on the frontend too.
Thank you so much!
Nice! We use this https://github.com/cweagans/composer-patches to incorporate patches to our builds. Might be easier to include than go through framework :)
@ngagestudios, thank you for your report.
The issue is already fixed in 2.1.8
This needs an extra fix in Magento\Catalog\Model\Category\DataProvider::convertValues to get custom image attributes fully working (so thumbnail displays in backend).
Specifically lines 497 & 498
https://github.com/magento/magento2/blob/2.2-develop/app/code/Magento/Catalog/Model/Category/DataProvider.php#L497
Should use $attributeCode  as lines 495 & 496 rather than hardcoded ['image']
$categoryData[$attributeCode][0]['size'] = isset($stat) ? $stat['size'] : 0;
$categoryData[$attributeCode][0]['type'] = $mime;
                    
Most helpful comment
Hi,
I have one more problem, We try add a image attribute for categories:
in file: vendor\magento\module-catalog\viewadminhtml\ui_component.xml
And code add attribute
When I checked in controller(vendor\magento\module-catalog\Controller\Adminhtml\Category\Image\Upload.php), I see a hard code:
When we try upload a image it show a bug

It's so easy to add a attribute for category in magento 2.0.x, Why you make it so hard in magento 2.1 :(