Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Magento CE 2.1 Not able to add attribute to category #5438

Closed
nirajketan opened this issue Jul 4, 2016 · 13 comments
Closed

Magento CE 2.1 Not able to add attribute to category #5438

nirajketan opened this issue Jul 4, 2016 · 13 comments
Labels
bug report Component: Catalog Fixed in 2.1.x The issue has been fixed in 2.1 release line Issue: Format is not valid Gate 1 Failed. Automatic verification of issue format is failed

Comments

@nirajketan
Copy link

nirajketan commented Jul 4, 2016

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

@wert2all wert2all added the TECH label Jul 4, 2016
@Danielc3
Copy link

Danielc3 commented Jul 4, 2016

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.

@nirajketan
Copy link
Author

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
#5438 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AMRmmWj_iUCf8bzBUvyWlUof578Ad9rcks5qSTKGgaJpZM4JEHug
.

@erictrn
Copy link

erictrn commented Jul 5, 2016

For "Custom Source Model", we do not need to extend DataProvider. Just do as sample xml below:

2016-07-05_14-10-27

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!

@tkacheva tkacheva added the JS label Jul 5, 2016
@tkacheva
Copy link

tkacheva commented Jul 5, 2016

@guz-anton please check if it has something to do with ui components

@duongquyet
Copy link

Hi,
I have same problem. Do you have solution?

@duongquyet
Copy link

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\view\adminhtml\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
screen

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 :(

@guz-anton guz-anton assigned NadiyaS and unassigned guz-anton and que-etc Jul 13, 2016
@vkorotun vkorotun added Area: Frontend and removed JS labels Aug 3, 2016
@allanpaiste
Copy link

allanpaiste commented Aug 3, 2016

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).

  1. Part of 'image' type sits in Controller/Adminhtml/Category/Save.php
  2. Some of it is in Model/Category/DataProvider.php
  3. And some in Image Uploader controller

@JacobDrummond
Copy link

@allanpaiste,

Do you know when your pull request will make it to release?

@allanpaiste
Copy link

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.

@JacobDrummond
Copy link

@allanpaiste,

Just patched-in #5978 using di.xml overrides. It works perfectly. I hope it makes it into the next release.

Also, Category::getImageUrl($attributeCode = 'image') will be very useful on the frontend too.

Thank you so much!

@allanpaiste
Copy link

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 :)

@magento-engcom-team
Copy link
Contributor

@ngagestudios, thank you for your report.
The issue is already fixed in 2.1.8

@magento-engcom-team magento-engcom-team added the Fixed in 2.1.x The issue has been fixed in 2.1 release line label Sep 20, 2017
@minlare
Copy link

minlare commented Oct 17, 2017

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;

pdohogne-magento pushed a commit that referenced this issue Jul 28, 2020
Fixed Issues:
- MC-31362: Media folder update
- MC-30944: Incorrect flow of saving Newsletter templates
- MC-31357: Customer file uploader update
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug report Component: Catalog Fixed in 2.1.x The issue has been fixed in 2.1 release line Issue: Format is not valid Gate 1 Failed. Automatic verification of issue format is failed
Projects
None yet
Development

No branches or pull requests