Register attribute output handlers via di.xml
New feature request
Description
Register new attribute output handlers via the di.
Issue: https://github.com/magento/magento2/issues/12371 Pull Request: https://github.com/magento/magento2/pull/24405
Expected result
The developer can add new attribute output handlers via the dia without having to edit the templates/theme which reduce maintenance and cost and is great for extensibility.
Benefits
This feature allows to add output handler without modifying the templates/themes for specific attribtues
Possible solutions
Developer guide:
<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 Vendor\Module\Model\Handler\Output is:
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;
}
}
Hi @thomas-kl1. Thank you for your report. To help us process this issue please make sure that you provided sufficient information.
Please, add a comment to assign the issue: @magento I am working on this
- Join Magento Community Engineering Slack and ask your questions in #github channel.
@magento I am working on this
Can you confirm if this code does work for you? I'm trying it out but the productAttribute function in Vendor\Module\Model\Handler\Output never gets fired. I can see the constructor in Vendor\Module\Model\Handler\Output firing, though, but when I trace through Magento\Catalog\Helper\Output I can't see this new handler being added anywhere. Thanks.
@nahall sorry there was a typo in the xml, please try again, but set the key in lowercase in the di (I have updated the description)
Ah, I had found the first typo but didn't realize that productattribute was case sensitive. It seems to be working now. Thanks much!
Hi @thomas-kl1 do you think we could close this issue?, was it just a typo then?. Thanks!
Hi @danidelcar it doesn't seems that the feature has been documented in the Magento Devdoc, so the issue should remain open.