I'm trying to create a plugin for \Magento\Swatches\Block\LayeredNavigation\RenderLayered in order to modify the return of getOptionViewData() It's not working.

My di.xml looks like that:


<type name="Magento\Swatches\Block\LayeredNavigation\RenderLayered">
    <plugin name="vendor_plugin_block_swatches_layerednavigation_renderlayered" type="Vendor\Module\Plugin\Block\Swatches\LayeredNavigation\RenderLayered" sortOrder="10"/>


My plugin looks like that:


 * @param \Magento\Swatches\Block\LayeredNavigation\RenderLayered $subject
 * @param callable $proceed
 * @param FilterItem $filterItem
 * @param Option $swatchOption
 * @return array
public function aroundGetOptionViewData(
    \Magento\Swatches\Block\LayeredNavigation\RenderLayered $subject,
    callable $proceed,
    FilterItem $filterItem,
    Option $swatchOption
) {
    $data = $proceed($filterItem, $swatchOption);
    $data['test'] = 'working!';
    return $data;


I'm using Magento version 2.2.4

I don't want to override the entire class, I prefer to use a plugin.

Of course, I did setup:di:compile, cache:flush

Does anyone have the idea about this?

  • have did the di compile – Amit Bera May 23 '18 at 7:25
  • of course I did that... – Pini May 23 '18 at 7:26

The quick answer is No. You can't use a plugin for a protected method, in this case: getOptionViewData().

As the limitation on the DevDocs:

Limitations Plugins cannot be used with any of the following:

  • Final methods
  • Final classes
  • Non-public methods
  • Static methods
  • __construct
  • Virtual types
  • Objects that are instantiated before Magento\Framework\Interception is bootstrapped
  • Objects that are not instantiated by the ObjectManager (e.g. by using new directly).

Reference https://devdocs.magento.com/guides/v2.2/extension-dev-guide/plugins.html

I suggest you write an after plugin for this method: getSwatchData() instead of getOptionViewData().


  • I know that because I read the docs, but it did work for me in other cases to override protected methods. Also, just to make sure, I temporarily changed the original method to public & it still didn't work, so I believe that it's not the reason. – Pini May 23 '18 at 7:37
  • There is no way you can override a protected method without using the preferences. I don't think that you able to use a plugin with non-public methods. If you turn on the xdebug and see the trace, it will be very easy to understand why. – Toan Nguyen May 23 '18 at 7:40
  • About the suggestion to override the getSwatchData() instead. Thanks! I'll do that. – Pini May 23 '18 at 7:40
  • @Pini toHtml() is a public method all the time and in any blocks my friend. But sometimes, there is _toHtml() as well, and that's protected method, they're quite similar... – Toan Nguyen May 23 '18 at 7:45
  • Just noticed, I guess you are correct. – Pini May 23 '18 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.