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

Add getters to product image builder #13414

Conversation

VincentMarmiesse
Copy link
Contributor

@VincentMarmiesse VincentMarmiesse commented Jan 29, 2018

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

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds on Travis CI are green)

@orlangur orlangur self-assigned this Jan 30, 2018
@ihor-sviziev
Copy link
Contributor

ihor-sviziev commented Jan 31, 2018

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)
OR add product as additional parameter to list of attributes that are passed to imageFactory in https://github.com/magento/magento2/blob/2.2-develop/app/code/Magento/Catalog/Block/Product/ImageBuilder.php#L139-L151, so you wouldn't need to have any plugin for builder

@orlangur
Copy link
Contributor

orlangur commented Jan 31, 2018

Hi @VincentMarmiesse,

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.

@ihor-sviziev
Copy link
Contributor

@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

@VincentMarmiesse
Copy link
Contributor Author

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 create function.

As @ihor-sviziev said, I was unable to get product information in the plugin.

@orlangur
Copy link
Contributor

@VincentMarmiesse you mean add a second image for each product?

@VincentMarmiesse
Copy link
Contributor Author

VincentMarmiesse commented Jan 31, 2018

@orlangur not really a second image but 2x data, with srcset attribute, so it renders like that

<img src="<1x url with standard Magento data>" alt="..." srcset="<2x url with my plugin data>" width="<1x width with standard Magento data>" height=<1x height with standard Magento data>" />

Copy link
Contributor

@ihor-sviziev ihor-sviziev left a 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:

  1. 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
  2. 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)
  3. Add getter for $product property in \Magento\Catalog\Block\Product\Image that allow you to customize block as you want

@ihor-sviziev ihor-sviziev self-assigned this Jan 31, 2018
@ihor-sviziev
Copy link
Contributor

@VincentMarmiesse please wait for answer from @orlangur before doing requested changed, might be he has better ideas

Copy link
Contributor

@orlangur orlangur left a 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.

@VincentMarmiesse
Copy link
Contributor Author

Hi @orlangur,

Yes it covers my needs, I update my PR ASAP.

@ihor-sviziev
Copy link
Contributor

Hi @VincentMarmiesse,
Are you interested in finalizing your PR? Do you need any help?

@VincentMarmiesse VincentMarmiesse force-pushed the add-imagebuilder-getters branch 2 times, most recently from 84d838b to 400629c Compare March 12, 2018 11:28
@VincentMarmiesse
Copy link
Contributor Author

Hi @ihor-sviziev @orlangur,

I have updated my PR according to your recommandations.

Just to know, we are adding the product_id so when we'll want to get the product, we'll have to load it, is there any reason to no add the product model directly?

Thanks.

@ihor-sviziev ihor-sviziev self-requested a review March 12, 2018 11:49
@ihor-sviziev
Copy link
Contributor

@VincentMarmiesse becasue view should not know about product, it should know minimum data that it needs to render block

@magento-engcom-team magento-engcom-team added this to the March 2018 milestone Mar 12, 2018
@magento-engcom-team
Copy link
Contributor

Hi @ihor-sviziev, thank you for the review.
ENGCOM-865 has been created to process this Pull Request

@magento-engcom-team
Copy link
Contributor

Hi @orlangur, thank you for the review.
ENGCOM-865 has been created to process this Pull Request

@ihor-sviziev
Copy link
Contributor

Hi @VincentMarmiesse, thank you for your contribution. Your changes were merged. Could you please create new PR to 2.3-develop branch with the same changes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants