I have an plugin with after method, that should append piece of html to return.
I think that the html shoul be in some template block. Is there a way to render block from plugin?
Thanks.
I have an plugin with after method, that should append piece of html to return.
I think that the html shoul be in some template block. Is there a way to render block from plugin?
Thanks.
You can use LayoutInterface
for block creating.
/**
* @param Invoice $subject
* @param $result
* @return string
*/
public function afterGetItemHtml(\Magento\Sales\Block\Order\PrintOrder\Invoice $subject, $result)
{
$renderer = $subject->getLayout()
->createBlock(MyCustomBlock::class);
return $result . $renderer->toHtml();
}
Maybe will helpful my answer.
I added plugin for Magento\Catalog\Controller\Category\View
Will override method - execute()
Get block by name - and return in json (for my case).
public function afterExecute(\Magento\Catalog\Controller\Category\View $subject, $result)
{
$isAjax = $this->_request->getParam('isAjax', null);
if (!is_null($isAjax)) {
$block = $result->renderResult($subject->getResponse());
$layout = $block->getLayout();
$responseData = [];
if ($layout->hasElement('main.content')) {
$mainContent = $layout->renderElement('main.content');
$responseData['main'] = $mainContent;
}
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$resultJson->setData($responseData);
return $resultJson;
}
return $result;
}