Skip to content

Instantly share code, notes, and snippets.

@quisse
Last active May 10, 2019 14:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quisse/b78ee3d121a4c9b8dd141334c8fa27a8 to your computer and use it in GitHub Desktop.
Save quisse/b78ee3d121a4c9b8dd141334c8fa27a8 to your computer and use it in GitHub Desktop.
Adding a productAttribute handler in Magento 2
<?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>
<?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