How can extensions show / hide blocks conditionally in Magento 2
In Magento 2 there are several ways of showing and hiding blocks conditionally. If its visibility depends on a configuration settings value, use the
1 2 3 4 5 6 7 8 9 |
<referenceContainer name='form.buttons'> <block class='Dfe\Google\Block\Backend\Template' name='Dfe_Google_Backend_Buttons' template='buttons.phtml' after='adminhtml_auth_login_buttons' ifconfig='dfe_google/login/enable' /> </referenceContainer> |
In case of a more complex visibility condition, show and hide a block by overriding the
The block won’t be shown If you return the empty string from the method.
1 2 3 4 5 6 7 8 9 10 |
<?php namespace Dfe\Facebook\Block; class Login extends \Magento\Framework\View\Element\Html\Link { /** * @override * @see \Magento\Framework\View\Element\Html\Link::toHtml() * @return string */ public function toHtml() {return !rm_customer_logged_in() ? parent::toHtml() : '';} } |
The above example illustrates how the block can be hidden or shown for anonymous or authenticated visitors. The