-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Add getters to product image builder #13414
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
Add getters to product image builder #13414
Conversation
In general builder should not have getters, but it leads to issues with using plugins or replace builder with your one, that is worst because risk of conflicting with another modules is much higher. We also got this issue recently, we were doing some workarounds. I'd like to add some description to phpdoc blocks of these getters that they are designed to be used in plugins only (I don't like this idea too much) |
Same as #10792 (comment) - please provide exact customization scenario you trying to achieve with plugin. Breaking class encapsulation is not a good idea in general and especially attaching plugins to getters as they can be called arbitrary amount of times being public. |
@orlangur in my case I was trying to add data- attributes to the image that are depends on the product. Reason of adding plugin in my case - we are loosing product instance in the image block during creating it in https://github.com/magento/magento2/blob/2.2-develop/app/code/Magento/Catalog/Block/Product/ImageBuilder.php#L139-L151 |
Hi @orlangur, I wanted to add a 2x image on my products so I needed the 2x image url, width and height exactly like the current image and add this data to the returned array of the As @ihor-sviziev said, I was unable to get product information in the plugin. |
@VincentMarmiesse you mean add a second image for each product? |
@orlangur not really a second image but 2x data, with
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like idea to add getters for builder, it's not good idea to get plugins for builder. Better idea would be send needed data to block and write plugins for block if it's needed.
My vision is following:
- add product to list of attributes that will be passed to Image Block Factory in https://github.com/magento/magento2/blob/2.2-develop/app/code/Magento/Catalog/Block/Product/ImageBuilder.php#L139-L151
- Add additional check to
\Magento\Catalog\Block\Product\Image::__construct
if we have$data['product']
- then set value to$this->product
(property already exists, so let's fill it) - Add getter for $product property in
\Magento\Catalog\Block\Product\Image
that allow you to customize block as you want
@VincentMarmiesse please wait for answer from @orlangur before doing requested changed, might be he has better ideas |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @VincentMarmiesse, together with @ihor-sviziev we came to conclusion to just add 'product_id'
into https://github.com/magento/magento2/blob/2.2-develop/app/code/Magento/Catalog/Block/Product/ImageBuilder.php#L139-L151 instead of such getters and instead of product model (then product instance in plugin may be obtained via repository).
Please share your thoughts regarding suggested implementation approach, if it covers scenarios you faced with.
If so, please apply necessary changes and force push as a single commit.
Hi @orlangur, Yes it covers my needs, I update my PR ASAP. |
Hi @VincentMarmiesse, |
84d838b
to
400629c
Compare
I have updated my PR according to your recommandations. Just to know, we are adding the Thanks. |
@VincentMarmiesse becasue view should not know about product, it should know minimum data that it needs to render block |
400629c
to
24bed8d
Compare
Hi @ihor-sviziev, thank you for the review. |
Hi @orlangur, thank you for the review. |
Hi @VincentMarmiesse, thank you for your contribution. Your changes were merged. Could you please create new PR to |
Hello,
Description
I have added the attributes getter into the product image builder, class
Magento\Catalog\Block\Product\ImageBuilder
in order to get them in a plugin for example. We may want to add some attributes and use$subject->getProduct()
to get product's data.Fixed Issues (if relevant)
N/A.
Manual testing scenarios
N/A.
Contribution checklist