magento / devdocs

[Deprecated] Magento Developer Documentation
Open Software License 3.0
673 stars 1.77k forks source link

Register attribute output handlers via di.xml #8587

Closed thomas-kl1 closed 2 weeks ago

thomas-kl1 commented 3 years ago

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;
    }
}
m2-assistant[bot] commented 3 years ago

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


dineshvb commented 3 years ago

@magento I am working on this

nahall commented 3 years ago

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.

thomas-kl1 commented 3 years ago

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

nahall commented 3 years ago

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

danidelcar commented 3 years ago

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

thomas-kl1 commented 3 years ago

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

dshevtsov commented 2 weeks ago

The Magento developer documentation was migrated to Adobe Commerce Developer Documentation. If the issue is still relevant, please feel free to reopen the issue in the corresponding project.