-
Notifications
You must be signed in to change notification settings - Fork 9.4k
[2.3] Provide fallback GalleryOptions class when argument missing from layout xml file #23733
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
Conversation
|
Hi @gwharton. Thank you for your contribution
For more details, please, review the Magento Contributor Assistant documentation |
|
There are no fallback options implemented for any of the other countless arguments injected into the various classes within "catalog_product_view.xml" so I'm erring on the side of closing the PR as won't fix and it should be on module developers to update their modules to inject the relevant required arguments if they instantiate the gallery class directly, or inherit from it, however there is the backward compatibility to think about, as many third party modules have now stopped working due to the requirement for a new argument to be injected. |
|
I also don't know what the best solution would be in cases like this. The problem for module developers is then that they have to add this argument to their block but if they want their module to also be backwards compatible with older versions of Magento they can't just use the class I had to fix a similar problem in one of our custom modules after Magento 2.3.1 got released which also added an a new argument to a certain block which we were also using in a custom module or ours and if you want to remain backwards compatible with older versions of Magento, you have to do a silly thing like adding your own new class as argument to the block containing a bunch of checks to make sure it works on all kinds of Magento versions (not saying this is the best solution, but this worked for us in that particular case, but it's far from ideal): <!-- some layout xml file in our custom module -->
<block class="Magento\Store\Block\Switcher" name="some.name" template="switch/languages.phtml">
<arguments>
<argument name="view_model" xsi:type="object">Vendor\ModuleName\ViewModel\SwitcherUrlProvider</argument>
</arguments>
</block><?php
// This is a dummy class which we need to add a view model to the Magento\Store\Block\Switcher block.
// In Magento 2.3.1, the following change got introduced: https://github.com/magento/magento2/commit/5f2ad89601f00a87313ca25d8e8277872c64637f#diff-c454e4f2b71801fb00105c7492dce9f0
// This adds a view_model to the Magento\Store\Block\Switcher block using xml
// So we need to do that as well in this module
// But because the class Magento\Store\ViewModel\SwitcherUrlProvider doesn't exists in Magento < 2.3.1,
// we need to add this dummy class thingy which checks if that new class exists, and then inherits from it
// And if the new class doesn't exist, then just create an empty class
// -> which still needs to implement the ArgumentInterface though for Magento 2.2
// -> and which needs to implement the CollectionDataSourceInterface for Magento 2.1 (this doesn't make any sense, but it works!)
namespace Vendor\ModuleName\ViewModel;
// For Magento 2.3.1 and higher
if (class_exists(\Magento\Store\ViewModel\SwitcherUrlProvider::class))
{
class SwitcherUrlProvider extends \Magento\Store\ViewModel\SwitcherUrlProvider
{
}
}
// For Magento 2.2.0 - 2.3.0 (including)
else if (interface_exists(\Magento\Framework\View\Element\Block\ArgumentInterface::class))
{
class SwitcherUrlProvider implements \Magento\Framework\View\Element\Block\ArgumentInterface
{
}
}
// For Magento versions lower then 2.2.0
else
{
class SwitcherUrlProvider implements \Magento\Framework\Data\CollectionDataSourceInterface
{
}
}I believe there aren't really backwards compatibility rules for frontend related code in Magento at this point yet. |
orlangur
left a comment
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.
This has broken backwards compatability with old third party modules code, who now need to update to inject this argument.
@gwharton I don't see to which item of https://devdocs.magento.com/guides/v2.3/contributor-guide/backward-compatible-development/ it corresponds.
| $block->setData( | ||
| 'gallery_options', | ||
| \Magento\Framework\App\ObjectManager::getInstance() | ||
| ->get(\Magento\Catalog\Block\Product\View\GalleryOptions::class) |
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.
Can we obtain it from layout somehow or it's not present? Using ObjectManager in template is quite messy approach.
This is simply not a right module releasing approach. |
|
@orlangur: Why not? Should module vendors make separate release branches for Magento 2.3.0, 2.3.1 and 2.3.2? That's not really maintainable? |
|
On reflection on the morning after, I'm going to close this PR. Any objections, feel free to reopen. :) |
|
Hi @gwharton, thank you for your contribution! |
|
@hostep wow. I didn't expect it could happen within |
Description (*)
The main product gallery layout file "catalog_product_view.xml" in "Magento_Catalog" now requires the argument "gallery_options" to be injected to the instance of the
"\Magento\Catalog\Block\Product\View\Gallery" class for the "product.info.media.image" block in order for the product page to be rendered correctly (This is a recent change). If this option is not present then the product page will just render the product image, and nothing else as the getGalleryOptions() call will just return null.
This is not a problem in vanilla Magento, as the argument is present in the layout file, however third party module providers, or anyone instantiating an instance of the "\Magento\Catalog\Block\Product\View\Gallery" class, or inheriting from it now needs to inject this argument for the template to render correctly. This has broken backwards compatability with old third party modules code, who now need to update to inject this argument.
This PR introduces code into the phtml template, that detects if the option is not injected, and falls back to hard coded "\Magento\Catalog\Block\Product\View\GalleryOptions" class.
Fixed Issues (if relevant)
Manual testing scenarios (*)
Questions or comments
Someone from Magento will need to determine whether this is required, or whether this is the right place for this check, given it is not needed in vanilla Magento, and is a fallback for when third party modules and users do not implement the argument correctly in layout.xml, or have not updated their module to include the argument. Given there is an issue raised on this, it is happening in the wild and this PR would help. If deemed not required, close accordingly.
Contribution checklist (*)