0

I used Plugin for this, But my plugin is not working. Below is my code.
di.xml

<type name="Magento\Framework\View\Element\Html\Link\Current">
    <plugin name="Current" type="CP\Coreoverride\Plugin\Current" />
</type>

Current.php

<?php
namespace CP\Coreoverride\Plugin;

class Current{

  protected function _toHtml()
  {
    echo "fdsfds"; die;
  }

}

Anyone can look into please.

2 Answers 2

1

You can't plugin a protected method

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

Limitations
Plugins can not be used on following:

  • Final methods
  • Final classes
  • Non-public methods
  • Class methods (such as static methods)
  • __construct Virtual types
  • Objects that are instantiated before Magento\Framework\Interception is bootstrapped

so your best bet is to either plug a first public method in inheritance chain

grep '>_toHtml(' vendor/magento/framework/View -rsn 
vendor/magento/framework/View/Element/AbstractBlock.php:659: $html = $this->_toHtml();

and that is Magento\Framework\View\Element\AbstractBlock toHtml method

2
  • Thanks for sharing. Can you please suggest. How can I achieve this ? I want override protected function _toHtml(). Commented Apr 17, 2018 at 9:26
  • updated my answer Commented Apr 17, 2018 at 9:35
0

Whenever you use the plugin you need to need to add After, Before or Around before the function. Please check the following link for more detail.

http://devdocs.magento.com/guides/v2.0/extension-dev-guide/plugins.html

Hope it'll help you.

1
  • 2
    You can't plugin a protected method Commented Apr 17, 2018 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.