0

I try to show a block in the header which shows different content, depending on whether the user is logged in or not.

If the user is logged in, then I show:

enter image description here

If the user is NOT logged in, then I like to show:

enter image description here

So to make this possible, I need access to methods of multiple block classes.

A) I need the method customerLoggedIn()in vendor\magento\module-customer\Block\Account\Customer.php [Magento\Customer\Block\Account\Customer] to check if the user is logged in or not.

B) ... the method getHref() in vendor\magento\module-customer\Block\Account\RegisterLink.php [Magento\Customer\Block\Account\RegisterLink], to get register link.

C) ... the method getHref() in vendor\magento\module-customer\Block\Account\Link.php [Magento\Customer\Block\Account\Link] to get the account link.

D) ... the method getHref() of vendor\magento\module-customer\Block\Account\AuthorizationLink.php [Magento\Customer\Block\Account\AuthorizationLink] to get login/logout link.

This is my current layout file:

<container name="user-actions-row" label="User Actions" htmlTag="div" htmlId="user-actions-row">
    <container name="user-actions-col-a" htmlTag="div" htmlId="user-actions-col-a">
        <!-- show "login" if user is NOT logged in -->
        <!-- show "account" if user is logged in -->
        <block class="Magento\Customer\Block\Account\Customer"
               name="user-actions-col-a-block"
               template="Magento_Customer::header/user-actions-col-a.phtml"
        />
    </container>

    <container name="user-actions-col-b" htmlTag="div" htmlId="user-actions-col-b">
        <!-- show "register" if user is NOT logged in -->
        <!-- show "log-out" if user is logged in -->
        <block class="Magento\Customer\Block\Account\Customer"
               name="user-actions-col-b-block"
               template="Magento_Customer::header/user-actions-col-b.phtml"
        />
    </container>
</container>

How do I have to setup my layout?

Template MY_THEME\Magento_Customer\templates\header\user-actions-col-a.phtml (not working):

<?php if ( ! $block->customerLoggedIn()): ?>
    ???
<?php else: ?>
    ???
<?php endif; ?>
1
  • I think I need to create a custom extension, then create a new block class with all the methods inside and use that block... Seems to be the only way. – Black Aug 24 '20 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.