1

I have a problem with the page builder on my custom module. In the adminhtml, everything is working fine, and my code propeller is saved to DB, but when I'm trying "echo" my code, the widget isn't displayed. I can see for example {{widget type="Magento\Cms\Block\Widget\Page\Link" anchor_text="test" title="test" template="widget/link/link_block.phtml" page_id="1"}} instead of my widget. Does anyone know how to render this code?

2
  • @dundan did you found a solution for this ? Commented Feb 20 at 14:29
  • @KrzysztofLinetty yes, thanks for asking.
    – dandan
    Commented Feb 28 at 15:15

2 Answers 2

0
<?php
declare(strict_types=1);

namespace Custom\CustomProductAttribute\ViewModel;

use Magento\Cms\Api\BlockRepositoryInterface;
use Magento\Cms\Api\Data\BlockInterface;
use Magento\Cms\Api\GetBlockByIdentifierInterface;
use Magento\Cms\Model\Template\FilterProvider;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\View\Element\Block\ArgumentInterface;
use Magento\Store\Model\StoreManagerInterface;

class Product implements ArgumentInterface
{
    /**
     * @var GetBlockByIdentifierInterface
     */
    private $blockByIdentifier;

    /**
     * @var BlockRepositoryInterface
     */
    private $blockRepository;

    /**
     * @var StoreManagerInterface
     */
    private $storeManager;

    /**
     * @var FilterProvider
     */
    private $filterProvider;

    /**
     * @param FilterProvider $filterProvider
     * @param StoreManagerInterface $storeManager
     * @param GetBlockByIdentifierInterface $blockByIdentifier
     * @param BlockRepositoryInterface $blockRepository
     */
    public function __construct(
        FilterProvider $filterProvider,
        StoreManagerInterface $storeManager,
        GetBlockByIdentifierInterface $blockByIdentifier,
        BlockRepositoryInterface $blockRepository
    ) {
        $this->filterProvider = $filterProvider;
        $this->blockByIdentifier = $blockByIdentifier;
        $this->blockRepository = $blockRepository;
        $this->storeManager = $storeManager;
    }

    /**
     * Return Block from ID
     *
     * @param string $id
     * @return BlockInterface
     * @throws LocalizedException
     */
    public function getBlockById(string $id)
    {
        return $this->blockRepository->getById($id);
    }

    /**
     * Getting block from blockIdentifier
     *
     * @param string $blockIdentifier
     * @return string
     * @throws NoSuchEntityException
     */
    public function getBlockFromIdentifier(string $blockIdentifier)
    {
        $block =  $this->blockByIdentifier->execute($blockIdentifier, (int)$this->storeManager->getStore()->getId());
        return $this->filterProvider->getBlockFilter()->filter($block->getContent());
    }

    /**
     * Getting label of product attributes
     *
     * @param object $product
     * @param string $attributeName
     * @return mixed
     */
    public function getProductAttributeLabel($product, $attributeName)
    {
        return $product->getResource()->getAttribute($attributeName)->getFrontendLabel();
    }

    /**
     * Getting value of product attributes
     *
     * @param object $product
     * @param string $attributeName
     * @return mixed
     */
    public function getProductAttributeValue($product, $attributeName)
    {
        return $product->getResource()->getAttribute($attributeName)
            ->getFrontend()->getValue($product);
    }
}
0

Use this class

use Magento\Cms\Model\Template\FilterProvider;

I fixed this issue with:

public function generateHtmlWithWidgets($html): string
{
    return $this->getCmsFilterContent($html);
}

private function getCmsFilterContent($value = ''): string
{
    return $this->filterProvider->getPageFilter()->filter($value);
}

public function _prepareLayout(): Column
{
    return parent::_prepareLayout();
}

and on .phtml file:

  $pageBuilder = $block->getPageBuilder();
  echo $block->generateHtmlWithWidgets($pageBuilder);
1
  • @MagentoDev2 Your last edit breaks formatting. Please do not put normal text into a code block. Commented Jan 23 at 15:20

Your Answer

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

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