devdocs icon indicating copy to clipboard operation
devdocs copied to clipboard

Register attribute output handlers via di.xml

Open thomas-kl1 opened this issue 4 years ago • 7 comments

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;
    }
}

thomas-kl1 avatar Jan 29 '21 09:01 thomas-kl1

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


m2-assistant[bot] avatar Jan 29 '21 09:01 m2-assistant[bot]

@magento I am working on this

dineshvb avatar Jan 29 '21 15:01 dineshvb

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 avatar Jan 29 '21 22:01 nahall

@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)

thomas-kl1 avatar Jan 29 '21 22:01 thomas-kl1

Ah, I had found the first typo but didn't realize that productattribute was case sensitive. It seems to be working now. Thanks much!

nahall avatar Jan 29 '21 22:01 nahall

Hi @thomas-kl1 do you think we could close this issue?, was it just a typo then?. Thanks!

danidelcar avatar Aug 04 '21 14:08 danidelcar

Hi @danidelcar it doesn't seems that the feature has been documented in the Magento Devdoc, so the issue should remain open.

thomas-kl1 avatar Aug 04 '21 19:08 thomas-kl1