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>