-
Notifications
You must be signed in to change notification settings - Fork 9.4k
#12371 allows to add handlers via di #24405
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
#12371 allows to add handlers via di #24405
Conversation
Hi @thomas-kl1. Thank you for your contribution
For more details, please, review the Magento Contributor Guide documentation. |
This implementation has a design issue and will not work as intended.
the implementation will fail because method get handlers will return always empty because variable $method will always be equal to
the handler will return correct class but only one handler can be added, meaning you can not have different handlers for different attributes, or adding handlers from multiple modules without replacing the existing handlers. Can you please provide your di.xml configuration? Update:
but is not very clear for end developers how the implementation works. |
@sky-hub Actually it works as following, no need to make it more complex: <type name="Magento\Catalog\Helper\Output">
<arguments>
<argument name="handlers" xsi:type="array">
<item name="productattribute" xsi:type="array">
<item name="handlerA" xsi:type="object">Vendor\Module\Handler\A</item>
<item name="handlerB" xsi:type="object">Vendor\Module\Handler\B</item>
</item>
</argument>
</arguments>
</type> However a new handler interface should be introduced, |
Hi @sidolov, thank you for the review.
|
@@ -94,7 +102,7 @@ protected function _getTemplateProcessor() | |||
*/ | |||
public function addHandler($method, $handler) | |||
{ | |||
if (!is_object($handler)) { | |||
if (!\is_object($handler)) { |
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.
Please import it similar to strtolower
.
After changes are applied and all builds are green, please squash them into a single commit so that we have perfectly clean history 😉
Hi @orlangur, thank you for the review.
|
✔️ QA Passed |
Hi @thomas-kl1, thank you for your contribution! |
Is there documentation somewhere about how to now create a product attribute output helper since this code change was made? I see code samples from before the change that don't work but no code sample of how it is actually now designed to work. Thanks. |
@nahall No there is no documentation as far as I know, <type name="Magento\Catalog\Helper\Output">
<arguments>
<argument name="handlers" xsi:type="array">
<item name="productattribute" xsi:type="array">
<item name="myHandler" xsi:type="object">Vendor\Module\Model\Handler\Output</item>
</item>
<item name="categoryattribute" xsi:type="array">
<item name="myHandler" xsi:type="object">Vendor\Module\Model\Handler\Output</item>
</item>
</argument>
</arguments>
</type> Where namespace Vendor\Module\Model\Handler;
use Magento\Catalog\Helper\Output as HelperOutput;
class Output
{
public function productAttribute(HelperOutput $output, string $attributeHtml, array $params): string
{
// $params: ['product' => $product, 'attribute' => $attributeName]
// todo your output handler
return $attributeHtml;
}
public function categoryAttribute(HelperOutput $output, string $attributeHtml, array $params): string
{
// $params: ['category' => $category, 'attribute' => $attributeName]
// todo your output handler
return $attributeHtml;
}
} |
This PR allows to add handlers thanks to the di.xml configuration file.
Description (*)
Today we can't add easily handlers to manage the output of a product attribute by using the output helper.
The solution is add a plugin on
getHandlers
methods, but you have to check in your side that you didn't already added the handler.An other solution is to add the handlers once on the instantiation of the helper.
Fixed Issues (if relevant)
Contribution checklist (*)