Reintroduce Customer Layout Handles in Magento 2

Many developers, including myself, found it useful whe customising Magento, to add layout updates based on whether customers were logged in or out. For example, we could add a template which included a popup containing a discount code customers could use if they created an account. This template would only show for logged out customers of course.

In order to do this, layout updates were made in customer_logged_in.xml and customer_logged_out.xml layout XML files. These have unfortunately disappeared in Magento 2, however, they can be reintroduced. To do this, a certified Magento developer should create a custom module to contain any customisations.

Our Magento development should start with adding a custom module, including a registration.php and module.xml file.

// file: app/code/[Vendor]/[Module]/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    '[Vendor]_[Module]',
    __DIR__
);
// file: app/code/[Vendor]/[Module]/etc/module.xml
<?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="[Vendor]_[Module]" setup_version="1.0.0" />
</config>

Be sure to run the module enable and database upgrade commands:

$ /path/to/php bin/magento module:enable [Vendor]_[Module]
$ /path/to/php bin/magento setup:upgrade

Now, layout handles can be added in Magento by adding an observer that observes the layout_load_before event.

Therefore, an events.xml file needs to be created in order to define the observer and the event it will observe.

// file: app/code/[Vendor]/[Module]/etc/frontend/events.xml
<?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_customer_handles" instance="[Vendor]\[Module]\Observer\AddCustomerHandlesObserver" />
    </event>
</config>

And finally, the observer itself

// file: app/code/[Vendor]/[Module]/Observer/AddCustomerHandlesObserver.php
<?php
namespace [Vendor]\[Module]\Observer;

use Magento\Customer\Model\Session;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class AddCustomerHandlesObserver implements ObserverInterface
{
    /**
     * @var Session
     */
    private $customerSession;

    /**
     * AddCustomerHandlesObserver constructor.
     *
     * @param Session $customerSession
     */
    public function __construct(
        Session $customerSession
    )
    {
        $this->customerSession = $customerSession;
    }

    /**
     * @param Observer $observer
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function execute(Observer $observer)
    {
        $layout = $observer->getEvent()->getLayout();

        if ($this->customerSession->isLoggedIn()) {
            $layout->getUpdate()->addHandle('customer_logged_in');
        } else {
            $layout->getUpdate()->addHandle('customer_logged_out');
        }
    }
}

Pretty simple in terms of how custom modules go. Now a developer will be able to create a customer_logged_in.xml and customer_logged_out.xml file that can hold layout updates depending on the login status of the customer.

As a freelance Magento 2 developer, I find it beneficial to share my knowledge with others. That's why I've created the module available for download found here: https://github.com/ScottParsons/module-customerhandles

Feel free to use the module and make changes as you see fit. If you've got any feedback or would like some help implementing custom functionality on your Magento 2 website, please contact me by clicking here to use this contact form.