10

Does Magento 2 have such handle as customer_logged_in? If yes how can I use it?

I tried to create

../view/frontend/layout/customer_logged_in.xml

and added there <referenceBlock name="content" remove="true" />

But content block wasn't removed. I renamed handle file to default and block was successfully removed.

What am I doing wrong?

9

Answer to your question is definitely no, there is no customer_logged_in handle anymore, I've digged into the code and could not find any layout handle that could do the trick.

On top of that under \dev\tests\static\testsuite\Magento\Test\Legacy\LayoutTest.php you can find the following code:

/**
 * List of obsolete nodes
 *
 * @var array
 */
protected $_obsoleteNodes = [
    'PRODUCT_TYPE_simple',
    'PRODUCT_TYPE_configurable',
    'PRODUCT_TYPE_grouped',
    'PRODUCT_TYPE_bundle',
    'PRODUCT_TYPE_virtual',
    'PRODUCT_TYPE_downloadable',
    'PRODUCT_TYPE_giftcard',
    'catalog_category_default',
    'catalog_category_layered',
    'catalog_category_layered_nochildren',
    'customer_logged_in',
    'customer_logged_out',
    'customer_logged_in_psc_handle',
    'customer_logged_out_psc_handle',
    'cms_page',
    'sku_failed_products_handle',
    'catalog_product_send',
    'reference',
];

Which to me, clearly tells us that the customer_logged_in layout handle is obsolete now.

See Fabian's answer for explanations about how it is done now in Magento 2.

  • 1
    those events are triggered when the customer logs in and when he logs out, not on every request. – Fabian Schmengler Mar 15 '16 at 12:33
8

As others have pointed out, these handles are now gone. If you look at the Magento_Customer module, you'll see that blocks like the registration link are always added but not rendered if the customer is logged in. It looks like this is now the preferred way to show elements conditionally depending on login state.

But this is not a solution for removing blocks based on their name, that were defined somewhere else. You could recreate the feature with an observer for layout_load_before that calls $layout->addHandle('customer_logged_in') if the customer is logged in.

  • Thanks for your response. Sure I thought about adding handle that in my custom module. But it could look like spike and I wanted to use more native way. Thanks for explanation and example – zhartaunik Mar 15 '16 at 12:44
7

If anyone is still struggling with this I wrote a quick module that adds the customer_logged_in and customer_logged_out layout handles for you to use just like in Magento 1

http://frankclark.xyz/modules/magento-2-get-customer_logged_in-and-customer_logged_out-layout-handles

Enjoy !

0

This is very stupid. I need to load block/template/resourse only customer have permission, not just loggin or loggout. But i need to write 1000 lines of code to check if user have permission to see this. Just beautiful.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.