1

I use the "core_block_abstract_to_html_after" event in magento 1. What is the equivalent in Magento 2?

enter image description here

2 Answers 2

2

It seems that \Magento\Framework\View\Element\AbstractBlock::toHtml haven't such event. You can achieve such result in different ways:

  1. If you need to add some customization for specific block, you can use it's _afterToHtml method.
  2. To customize all block's output globally, use plugin for Magento\Framework\View\Element\AbstractBlock and implement aftreToHtml method. More information available here: http://devdocs.magento.com/guides/v2.0/extension-dev-guide/plugins.html
3
  • i add afterToHtml method using plugin. it's showing blank page if i return $html i show following error Interceptor could not be converted to string i use this interceptor in my di.xml Magento\Framework\View\Element\AbstractBlock
    – Pratik
    Dec 10, 2015 at 13:01
  • @Pratik, Try to clean var/generation folder. Here is code snippet, which worked for me: <type name="Magento\Framework\View\Element\BlockInterface"> <plugin name="block.plugin" type="Vendor\Module\Model\BlockPlugin" sortOrder="1"/> </type> In Vendor\Module\Model\BlockPlugin I implemented such method: public function afterToHtml($subject, $result) { return "<b>" . $result . "</b>"; } Dec 10, 2015 at 14:11
  • yes you r right Thx for help :)
    – Pratik
    Dec 11, 2015 at 6:42
0

_afterToHtml is a protected function, plugin only works with public functions. There is a similar event of m1 that you can use in replacement of old 'core_block_abstract_to_html_after', for m2: 'view_block_abstract_to_html_after'.

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.