Last active
May 10, 2019 14:52
-
-
Save quisse/b78ee3d121a4c9b8dd141334c8fa27a8 to your computer and use it in GitHub Desktop.
Adding a productAttribute handler in Magento 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | |
<type name="Magento\Catalog\Helper\Output"> | |
<plugin name="OutputHelper" type="Vendor\Module\Plugin\OutputPlugin" sortOrder="1"/> | |
</type> | |
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Vendor\Module\Plugin; | |
use Magento\Catalog\Helper\Output; | |
class OutputPlugin { | |
// this plugin method should be on a method which collects all handlers and isn't called everytime the handlers are processed | |
public function beforeProcess(Output $outputHelper, $method, $result, $params) { | |
$handlers=$outputHelper->getHandlers('productAttribute'); | |
if(!in_array($this,$handlers)){ | |
$outputHelper->addHandler('productAttribute', $this); | |
} | |
return [$method, $result, $params]; | |
} | |
public function productAttribute($helper, $value, $parameters) { | |
$attribute = $parameters['attribute']; | |
switch ($attribute) { | |
case 'attribute' : | |
$r = '<ul class="advice">' . implode("\n", $attribute) . '</ul>'; | |
break; | |
default: | |
$r = $value; | |
break; | |
} | |
return $r; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment