0

How can I call a .phtml file in an Observer?

I want to show a popup window when event Observer fires after customer login so I can call a .phtml file that contains HTML popup?

So after a customer has logged-in a popup appears on the screen that contains the string text.

  • which observer you want to call . – Amit Bera May 23 '18 at 7:55
  • i want to call .phtml file from customerLoggedin observer @AmitBera – user4536 May 23 '18 at 7:57
0

Add DI in observer's construct function like below :

public function __contruct(\Magento\Framework\View\Element\Context $context)
{
   $this->_layout = $context->getLayout();
}

In execute function, add this code:

if(condition)
{
  $this->_layout->createBlock('Vendor\Module\Block\Ex')->setTemplate('Vendor_Module::path/to/template.phtml')->toHtml();
}

This may work, not sure though.

0

The use of customer_login event for the block/template is not the proper idea.

The best idea in my view,

  • Create a cookie variable on event customer_login.
  • Then render your block HTML output if that you have that cookie variable after redirecting from loginpost.

Observer class:

<?php
namespace {Vendor}\{Modulename};

use Magento\Framework\Event\ObserverInterface;
class AssigncookieVariable  implements ObserverInterface
{
    /**
     * Cookie key 
     */
    const COOKIE_NAME = 'customer-just-login';
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager;

    /**
     * @var \Magento\Framework\Stdlib\CookieManagerInterface
     */
    protected $cookieManager;

    /**
     * @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory
     */
    protected $cookieMetadataFactory;

    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
        \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory,
    )
    {
        $this->storeManager = $storeManager;
        $this->cookieManager = $cookieManager;
        $this->cookieMetadataFactory = $cookieMetadataFactory;
    }
  public function execute(\Magento\Framework\Event\Observer $observer)
  {

        $metadata = $this->cookieMetadataFactory
            ->createPublicCookieMetadata()
            ->setPath(self::COOKIE_PATH)
            ->setHttpOnly(false);
        $this->cookieManager->setPublicCookie(self::COOKIE_NAME, $cookieValue, $metadata);
    }

}    

Stop block from output render when cookie value does not exit

<?php
namespace {Vendor}\{Modulename}\BlocK;


class {YourBlockclass} ....
{
    /**
     * Cookie key 
     */
    const COOKIE_NAME = 'customer-just-login';
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager;

    /**
     * @var \Magento\Framework\Stdlib\CookieManagerInterface
     */
    protected $cookieManager;

    /**
     * @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory
     */
    protected $cookieMetadataFactory;

    public function __construct(
       ......
        \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
        \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory,
        ....
    )
    {
        ....
        $this->cookieManager = $cookieManager;
        $this->cookieMetadataFactory = $cookieMetadataFactory;
    }

    protected function _toHtml() {
         if(!{CookieDoesnotExits}){
            return '';
        }
        return parent::_toHtml();
    }

}

Call block via default.xml

<?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">
    <body>
        <referenceContainer name="{referenceBlockname}">
            <block class="{YourBlockClass}" name="{blocknameIn Layout}" 
            cacheable = "false"
            template="{template}"/>
        </referenceContainer>
   </body>
</page>

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.