I have created a custom .phtml file that contains a modal popup.

I can successfully call this .phtml by adding it to default.xml in layout.

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">   
        <referenceContainer name="root">'
            <block class="Magento\Store\Block\Switcher" name="redirectPopup" template="Magento_Store::switch/redirectPopup.phtml" />

The code above working fine.

But I would like to call this template only if a certain condition is matched. That's why I want to call it from observer in my custom module.

I tried the following code in observer,

public function showRedirectPopup()
    $block = $this->layout->createBlock('Magento\Store\Block\Switcher')->setTemplate('Magento_Store::switch/redirectPopup.phtml')->toHtml();
    return $block;

I have made sure the function showRedirectPopup() is executed. But it doesn't seems .phtml is being called.

Did I miss anything?

Can we at all call a .phtml from a observer or it has to be called from layout(.xml)?

  • I found it out. The reason the I could not get any HTML output, is because in my showRedirectPopup() function, I used "return", and thus I need to echo the function, but instead of echo, I used return again to call the showRedirectPopup() function. Commented Jul 4, 2018 at 19:30

1 Answer 1


Could you check if your script injects

\Magento\Framework\View\LayoutFactory $layoutFactory

in the constructor.

After that in your execute function place this:

$layout = $this->layoutFactory->create();
$block = $layout->createBlock('Magento\Store\Block\Switcher')->setTemplate('Magento_Store::switch/redirectPopup.phtml')->toHtml();
return $block;

Now it should work


i see what i missed in my answer. Forgot to tell that you must set the response header and body, instead of returning the html directly. So it goes like this:

$block = $layout->createBlock('Magento\Store\Block\Switcher')->setTemplate('Magento_Store::switch/redirectPopup.phtml');
$response = $observer->getEvent()->getData('response');

Also make sure you pick an event that is fired later than controller_action_predispatch for example controller_front_send_response_before because the response object doesn't exist yet. Using this object the right response type can be set

  • I didn't use LayoutFactory. But I have now added LayoutFactory as you suggested, still no popup. Commented Jun 27, 2018 at 8:45
  • I tried the new code, now I get this PHP Fatal error: Uncaught Error: Call to undefined method getResponse(). Commented Jun 27, 2018 at 8:59
  • Do I need to created an event for this? Commented Jun 27, 2018 at 9:11
  • 1
    because it's an observer the getresponse doesn't exist. In my code i use this in a controller which does have that function. Take a look here: magento.stackexchange.com/questions/108579/…
    – Menno
    Commented Jun 27, 2018 at 9:39
  • I have edited my question. It seems if I use echo $block, then it will output the html from my custom .phtml. however the modal popup is not a popup now. it just being displayed on the page as plain html. Commented Jun 27, 2018 at 9:51

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.