0

I have a very basic custom module showing a compact catalog/product list for logged in customers.

The content of category descriptions etc. is being rendered correctly.

But at the top of my template I include a static block using the code

<?php echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('trade-intro')->toHtml();?>

This block is a pagebuilder block but it is rendered as straight HTML which means layout, images etc. are broken

How do I add it to my template in a way that goes through Pagebuilder rendering?

note: for the category description I go through the helper

$this->helper(Magento\Catalog\Helper\Output::class)->categoryAttribute($winemakercategory, $_description, 'description') 

and that uses the right context, but I can't find the same for the simple cms block

asked Oct 22 at 10:22

1 Answer 1

1

This is just an quick example using objectManager of how you will need to output your block on the template to get the result you want

<?php
/** @var \Magento\Framework\View\Element\Template $block */
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
/** @var \Magento\Cms\Model\BlockRepository $blockRepository */
$blockRepository = $objectManager->get(\Magento\Cms\Model\BlockRepository::class);
/** @var \Magento\PageBuilder\Model\Template\Filter $pageBuilderFilter */
$pageBuilderFilter = $objectManager->get(\Magento\PageBuilder\Model\Template\Filter::class);
try {
 $cmsBlock = $blockRepository->getById('trade-intro');
 $content = $cmsBlock->getContent();
 // Render Page Builder JSON content as HTML
 echo $pageBuilderFilter->filter($content);
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
 echo '<!-- CMS block not found -->';
}
?>

Use a proper block class in your project.

<?php
namespace Vendor\Module\Block;
use Magento\Framework\View\Element\Template;
use Magento\Cms\Model\BlockRepository;
use Magento\PageBuilder\Model\Template\Filter;
class PageBuilderBlock extends Template
{
 protected $blockRepository;
 protected $pageBuilderFilter;
 public function __construct(
 Template\Context $context,
 BlockRepository $blockRepository,
 Filter $pageBuilderFilter,
 array $data = []
 ) {
 parent::__construct($context, $data);
 $this->blockRepository = $blockRepository;
 $this->pageBuilderFilter = $pageBuilderFilter;
 }
 public function renderBlock($identifier)
 {
 try {
 $cmsBlock = $this->blockRepository->getById($identifier);
 return $this->pageBuilderFilter->filter($cmsBlock->getContent());
 } catch (\Exception $e) {
 return '<!-- Block not found or error rendering -->';
 }
 }
}

and then in your template

<?= $block->renderBlock('trade-intro'); ?>
answered Oct 23 at 9:01
2
  • Thanks - I though I had used a proper block class, the Block code extends \Magento\Framework\View\Element\Template - what I had missed is that i now need to add this whole pagebuilderfilter - because that's how old this block's been working. Do I need to do it when calling the CMS block or should I do it on the entire custom block's output? Commented Oct 23 at 9:55
  • Create a block class for your template and there you can add a function to render your block using pagebuilderfilter and then call it form your template. Commented Oct 23 at 10:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.