I was working with a frontend developer who was complaining that Magento 2.0 no longer had the customer_logged_in and customer_logged_out layout handles that were extremely useful in Magento 1.
So I did some googling around and came across this stack overflow question where it became clear that other developers were also upset that this layout handle no longer existed. So I thought i’d be a nice chap and write a module to add them in.
Stack Overflow : http://magento.stackexchange.com/questions/106134/magento2-layout-handle-customer-logged-in
So this is how you do it. I called my module CustomHandles because I intend to reuse it to add any other custom layout handles that i require.
Start of with your usual registration.php, composer.json and module.xml files
Fc/CustomHandles/registration.php
1 2 3 4 5 6 7 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Fc_CustomHandles', __DIR__ ); |
Fc/CustomHandles/composer.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
{ "name": "fc/custom_handles", "description": "Space for adding custom layout handles for frontend originally to add customer_logged_in and customer_logged_out handles", "type": "magento2-module", "version": "1.0.0", "license": [ "OSL-3.0", "AFL-3.0" ], "require": { "php": "~5.5.0|~5.6.0", "magento/magento-composer-installer": "*" }, "extra": { "map": [ [ "*", "Fc/CustomHandles" ] ] } } |
Fc/CustomHandles/etc/module.xml
1 2 3 4 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Fc_CustomHandles" setup_version="1.0.0"/> </config> |
Now the simple setup side of things is done, we need to register an observer that is triggered before our layout is loaded. This enables us to check if the customer is logged in or logged out and add the appropriate layout handle. There are two elements involved in this. Declaring the observer in our frontend/events.xml and the actual observer itself.
Fc/CustomHandles/etc/frontend/events.xml
1 2 3 4 5 6 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="layout_load_before"> <observer name="add_layout_handles" instance="Fc\CustomHandles\Observer\AddHandles" /> </event> </config> |
Fc/CustomHandles/Observer/AddHandles.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php namespace Fc\CustomHandles\Observer; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Customer\Model\Session as CustomerSession; class AddHandles implements ObserverInterface { protected $customerSession; public function __construct( CustomerSession $customerSession ) { $this->customerSession = $customerSession; } public function execute(\Magento\Framework\Event\Observer $observer) { $layout = $observer->getEvent()->getLayout(); if ($this->customerSession->isLoggedIn()) { $layout->getUpdate()->addHandle('customer_logged_in'); } else { $layout->getUpdate()->addHandle('customer_logged_out'); } } } |
As you can see the observer itself is very simple, we add the customer session to our constructor and then add handles to the layout. If you want to test this you can add another observer for layout_generate_blocks_after and do observer->getEvent()->getLayout()->getUpdate()->getHandles(); to see your new handle added in the list of layout handles.
As an example of how to use the new handles i’ve created two xml files, one for logged in an done for logged out that will load a different phtml file into the page.top of your Magento 2 installation dependent on the customers state.
Fc/CustomHandles/view/frontend/layout/customer_logged_in.xml
1 2 3 4 5 6 7 8 |
<?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="page.top"> <block class="Fc\CustomHandles\Block\LoggedIn" template="logged-in.phtml"/> </referenceContainer> </body> </page> |
Fc/CustomHandles/view/frontend/layout/customer_logged_out.xml
1 2 3 4 5 6 7 8 |
<?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="page.top"> <block class="Fc\CustomHandles\Block\LoggedOut" template="logged-out.phtml"/> </referenceContainer> </body> </page> |
Be sure to create your two template files : logged-in.phtml and logged-out.phtml
You may have also noticed that i’ve used a custom block class. You don’t have to do this, you can use Magento2’s standard block class : Magento\Framework\View\Element\Template but I thought since I don’t know what other layout handles I may want to add or what I may want in my template files I thought i’d create a block class that can be used later.
Fc/CustomHandles/Block/LoggedIn.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php namespace Fc\CustomHandles\Block; class LoggedIn extends \Magento\Framework\View\Element\Template { protected $storeManager; protected $coreHelper; /** * Construct * * @param \Magento\Framework\View\Element\Template\Context $context * @param array $data */ public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Store\Model\StoreManagerInterface $storeManager, array $data = [] ) { parent::__construct($context, $data); } public function getLoggedInContent() { return ''; } } |
Fc/CustomHandles/Block/LoggedOut.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php namespace Fc\CustomHandles\Block; class LoggedOut extends \Magento\Framework\View\Element\Template { protected $storeManager; protected $coreHelper; /** * Construct * * @param \Magento\Framework\View\Element\Template\Context $context * @param array $data */ public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Store\Model\StoreManagerInterface $storeManager, array $data = [] ) { parent::__construct($context, $data); } public function getLoggedOutContent() { return ''; } } |
You can download the full module on git here : https://github.com/TheFrankman/magento2-customhandles