0

I tried the following solution:-

Magento 2 Load - Block (CMS Static Block in Magento 1 terms)

But getting some issue, you can visit here to see:-

http://nimb.ws/oURqfV

Here is my controller code:-

<?php
 namespace Test\Clientform\Controller\Show;
 class Index extends \Magento\Framework\App\Action\Action
 {
 protected $_filterProvider;
 protected $_storeManager;
 protected $_blockFactory;
 public function __construct(
 \Magento\Framework\View\Element\Context $context,
 \Magento\Cms\Model\Template\FilterProvider $filterProvider,
 \Magento\Store\Model\StoreManagerInterface $storeManager,
 \Magento\Cms\Model\BlockFactory $blockFactory,
 array $data = []
 )
 {
 parent::__construct($context, $data);
 $this->_filterProvider = $filterProvider;
 $this->_storeManager = $storeManager;
 $this->_blockFactory = $blockFactory;
 }
 public function execute()
 {
 $resultPage = $this->_resultPageFactory->create();
 return $resultPage;
 }
 protected function content()
 {
 $blockId = 'YOurBlock_Id';
 $html = '';
 if ($blockId) {
 $storeId = $this->_storeManager->getStore()->getId();
 /** @var \Magento\Cms\Model\Block $block */
 $block = $this->_blockFactory->create();
 $block->setStoreId($storeId)->load($blockId);
 $html = $this->_filterProvider->getBlockFilter()->setStoreId($storeId)->filter($block->getContent());
 }
 return $html;
 }
 }
 ?>
asked Jul 31, 2018 at 7:55

1 Answer 1

3

As you see in your exception log, issue is you should use App\Action\Context instead of View\Element\Context Class

<?php
namespace Test\Clientform\Controller\Show;
class Index extends \Magento\Framework\App\Action\Action
{
 protected $_filterProvider;
 protected $_storeManager;
 protected $_blockFactory;
 public function __construct(
 \Magento\Framework\App\Action\Context $context,
 \Magento\Cms\Model\Template\FilterProvider $filterProvider,
 \Magento\Store\Model\StoreManagerInterface $storeManager,
 \Magento\Cms\Model\BlockFactory $blockFactory,
 array $data = []
 )
 {
 parent::__construct($context, $data);
 $this->_filterProvider = $filterProvider;
 $this->_storeManager = $storeManager;
 $this->_blockFactory = $blockFactory;
 }
 public function execute()
 {
 $resultPage = $this->_resultPageFactory->create();
 return $resultPage;
 }
 protected function content()
 {
 $blockId = 'YOurBlock_Id';
 $html = '';
 if ($blockId) {
 $storeId = $this->_storeManager->getStore()->getId();
 /** @var \Magento\Cms\Model\Block $block */
 $block = $this->_blockFactory->create();
 $block->setStoreId($storeId)->load($blockId);
 $html = $this->_filterProvider->getBlockFilter()->setStoreId($storeId)->filter($block->getContent());
 }
 return $html;
 }
}
?>

Try this hope it will help.

Wakar Ahamad
83511 silver badges38 bronze badges
answered Jul 31, 2018 at 8:29
1
  • @Naveed, can we get the HTML of static block and store into some php file? Commented Jan 30, 2019 at 4:48

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.