0

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?

1 Answer 1

1

you need to try this code.

For phtml file you need to used this code.

<?php 
$customerror = $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('block_identifier')->toHtml();
$this->_messageManager->addError(__($customerror));
?>

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();
      $this->_messageManager->addError(__($customerror));
    }
}

Replace 'block_identifier' with your block identifier.

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

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.