magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.56k stars 9.32k forks source link

Bug : Product Media getMediaAttributeCodes #31795

Open Nuranto opened 3 years ago

Nuranto commented 3 years ago

Preconditions (*)

Magento 2.4.1

Steps to reproduce (*)

Add a media attribute for a non-product entity type. The attribute_code should not be an attribute_code used by product (like 'image'. Use 'base_image' for example). For instance, to do the quick test we could do :

INSERT INTO `eav_attribute` (`attribute_id`, `entity_type_id`, `attribute_code`, `attribute_model`, `backend_model`, `backend_type`, `backend_table`, `frontend_model`, `frontend_input`, `frontend_label`, `frontend_class`, `source_model`, `is_required`, `is_user_defined`, `default_value`, `is_unique`, `note`)
VALUES
    (null, 1, 'base_image', NULL, NULL, 'varchar', NULL, NULL, 'media_image', 'Base Image', NULL, NULL, 0, 0, NULL, 0, NULL);

Debug the return of method : Magento\Catalog\Model\Product\Media\Config::getMediaAttributeCodes()

Expected result (*)

The method only returns product media attribute codes. (without 'base_image')

Actual result (*)

The method returns all entity types media attribute codes. (with 'base_image')

Magento\Catalog\Model\Product\Media\Config::getMediaAttributeCodes() is used in many parts of the code, so I think it will create problems on differents locations. One of them is that you cannot save a product having images anymore : getMediaAttributeCodes is called by processMediaAttributes() in Product\Gallery\CreateHandler which is called on product save - when there is images) => That gives a 500 error (try to call getBackend() on null). Which makes sense since it tries to load product attribute 'base_image' which don't exist.


Please provide Severity assessment for the Issue as Reporter. This information will help during Confirmation and Issue triage processes.

m2-assistant[bot] commented 3 years ago

Hi @Nuranto. Thank you for your report. To help us process this issue please make sure that you provided the following information:

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, please, review the Magento Contributor Assistant documentation.

Please, add a comment to assign the issue: @magento I am working on this


:clock10: You can find the schedule on the Magento Community Calendar page.

:telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, please join the Community Contributions Triage session to discuss the appropriate ticket.

:movie_camera: You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel

:pencil2: Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

Nuranto commented 3 years ago

If you want a fast way to confirm issue, you can, on a fresh instance :

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 14 days if no further activity occurs. Is this issue still relevant? If so, what is blocking it? Is there anything you can do to help move it forward? Thank you for your contributions!

Nuranto commented 3 years ago

Still relevant

m2-assistant[bot] commented 3 years ago

Hi @engcom-Golf. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

Nuranto commented 3 years ago

(Note : Bug still there on 2.4.2)

engcom-Golf commented 3 years ago

@Nuranto You are creating a new attribute_code base_image for Customer entity having Input Type as Media Image. As per Magento documentation, Media Image is only associated with Product entity rather Customer. Given below Input Types are available for creating Customer's attribute codes. image

Note: As per existing Logic, getMediaAttributeCodes() will show attribute codes having input type as media_image. That's why It is showing newly created Customer's attribute code having Input Type as Media Image. image

Also It is not recommended to create custom attribute via Insert query.

Nuranto commented 3 years ago

@Nuranto You are creating a new attribute_code i.e base_image for Customer entity having Input Type as Media Image. As per Magento documentation, Media Image is only associated with Product entity rather Customer.

Customer was only for error-reproducing purpose. My real goal is to use a media gallery for a custom entity type (Actually, It's already done - and working - on the Magento1 that I want to migrate). However, if someone want to add a media gallery to Customer entity type, I don't see the reason why he couldn't.

I don't understand why a media gallery input type should be hardly linked to product entity type only.

In my understanding, it just needs an additionnal filter on "entity_type_id", to get only attributes from Product entity type.

Note: As per existing Logic, getMediaAttributeCodes() will show attribute codes having input type as media_image. That's why It is showing newly created Customer's attribute code having Input Type as Media Image.

Ok but getMediaAttributeCodes() is a Product method (Magento\Catalog\Model\Product), so it would make sense to also filter on Product entity type.

Also It is not recommended to create custom attribute via Insert query.

That was only for reproducing the error quickly

engcom-Golf commented 3 years ago

@Nuranto As of now, Input type Media Image is only associated with Product entity rather Customer. If you want Customer specific Media Gallery then Please create Customer's Custom Column having Input Type as Image File.

Image File | A field that allows an image to be uploaded to the gallery and associated with the customer attribute.

Nuranto commented 3 years ago

I don't need an image attribute only, but a media gallery also.

But ok, I will do this differently then..

Nuranto commented 3 years ago

If someone else struggle with this, here is a patch : https://github.com/magento/magento2/pull/33150 You can use it through composer patch system.