Conditionally displaying block
Question
I have a block that replaces the default Magento login form. I would like to conditionally display it, or else revert to the default login form. I am using ifconfig
to display the block if a certain configuration variable is set:
<block class="Namespace\Module\Block\Form\LoginFormClass" name="module_form_login" template="Module::login_page.phtml" ifconfig="module_config/login/enable">
<arguments>
<argument name="display" xsi:type="string">embed</argument>
</arguments>
</block>
However, what this does is just hide or show the block. When the configuration is disabled, the block is hidden, but the default Magento login form does not appear. The customer_account_login.xml
has additional elements (referenceContainer/referenceBlock etc.) that don't have a valid ifconfig
parameter.
Is it possible to only activate the entire override conditionally, and display the default login form if the condition is not met?
Solution
You need to use this code.
<?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>
<referenceBlock name="customer_form_login">
<action method="setTemplate" ifconfig="module_config/login/enable">
<argument name="text" translate="true" xsi:type="string">Vendor_Module::login_page.phtml</argument>
</action>
</referenceBlock>
</body>
</page>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange