I am setting error message using below code.

$this->_messageManager->addError(__('you must login to continue.'));

Is it possible to load the message from the static block content.

for example i created static block with some html content. that content i need to show in error message. Is that can be done?


you need to try this code.

For phtml file you need to used this code.

$customerror = $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('block_identifier')->toHtml();

For block file you need to do code like this.

class Yourclass
    protected $cmsblock;

    public function __construct(\Magento\Cms\Block\Block $cmsblock)
       $this->cmsblock = $cmsblock;

    public function Yourfunction()
      $customerror = $this->cmsblock->setBlockId('block_identifier')->toHtml();

Replace 'block_identifier' with your block identifier.

| improve this answer | |
  • this shows with html? – jafar pinjar Sep 26 '18 at 14:37
  • how to call it in block file – jafar pinjar Sep 26 '18 at 16:02
  • you need to call static block content as error message, right? – Dhiren Vasoya Sep 27 '18 at 4:50
  • yes, in phtml file its working fine, but how to call it in Block files? that is php files? – jafar pinjar Sep 27 '18 at 5:56
  • I update answer, Kindly check into that I specify, How you can call CMS block content into Block php file. – Dhiren Vasoya Sep 27 '18 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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