Get CMS Block content by identifier in Magento 2.

You can get the CMS Static Blocks collection by an identifier in Magento 2 by calling the BlockRepositoryInterface interface.

Magento\Cms\Api\BlockRepositoryInterface is used for getting CMS Static Blocks in Magento 2.

BlockRepositoryInterface is used when you need to fetch a collection of CMS Static blocks or get specific CMS static block data.

Continue reading “Get CMS Block content by identifier in Magento 2.”

How to Convert CMS page content to html interpretation in Magento 2?

We can convert CMS page content into HTML specific format using native Magento class.

We need to use, \Magento\Cms\Model\Template\FilterProvider class to filter page content in the template file.
In PHP file,

<?php
public function __construct(
    \Magento\Cms\Model\Template\FilterProvider $filter
) {
    $this->filter = $filter;
}
/*
* Page content $content
*/
public function getContentFromPage($content)
{
    return $this->filter->getPageFilter()->filter($content);
}

Call from template file,

$content = "YOUR_CMS_PAGE_CONTENT";
echo $this->getContentFromPage($content);

How to filter CMS Page(Block) content in template file using magento 2?

Sometimes we have defined a native static variable in Static Block in Magento 2 and we need to call that block in template file at that time static variable will not be interpreted,
Like Custom variable {{config path=”web/unsecure/base_url”}} in static block. using below method content will be displayed the dynamic value of static block variables.
We need to use, \Magento\Cms\Model\Template\FilterProvider class to filter static content in Blocks.

<?php
public function __construct(
    \Magento\Cms\Model\Template\FilterProvider $filter
) {
    $this->filter = $filter;
}
/*
* Static block $content
*/
public function getContentFromStaticBlock($content)
{
    return $this->filter->getBlockFilter()->filter($content);
}

Call from the template file,

$content = "YOUR_STATIC_BLOCK_CONTENT";
echo $this->getContentFromStaticBlock($content);

The result will be displayed filterable static variable value with your content.