2

How can I use the Layout Update XML to modify a block that's been embedded in a CMS page. I'm embedding the Magento login block into a CMS page, which works fine, but it does not show the 'Remember Me' checkbox by default. I would like to add the 'Remember Me' checkbox using a Layout Update.

Block embedded in CMS page:

{{block 
  class="Magento\Customer\Block\Form\Login" 
  template="Magento_Customer::form/login.phtml" 
  name="customer.login.container"}}

Layout Update XML:

<referenceContainer name="form.additional.info">
    <block class="Magento\Persistent\Block\Form\Remember" 
           name="persistent.remember.me" 
           template="Magento_Persistent::remember_me.phtml" 
           before="-"/>
</referenceContainer>

The module-persistent customer_account_login.xml uses this same layout update to add the checkbox on the login page so I thought it would work in a CMS page too. I thought wrong.

Here's a snippet of the login.phtml where the block should render: Here's a snippet of the login.phtml where the block should render

I have tried placing the layout update in the CMS page and also in the cms_page_view.xml file, but the Remember block is never rendered.

1
  • 2
    You can achieve it by modifying template file or you want this to be done only using layout file? Commented Oct 29, 2021 at 7:43

1 Answer 1

1

You can do this by following the below steps:

Using XML Layout:

You have to create a layout file specifically for your CMS Page, such as in layout folder of your custom module,

cms_page_view_selectable_<CMS Page Identifier>_<Layout Update Name>.xml

for example if my CMS page identifier name is testing, I'll name it as cms_page_view_selectable_testing_loginform.xml and inside the file I can write code as below to get the login form and remember me option as well:

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <title>Customer Login</title>
    </head>
    <body>
        <referenceContainer name="content">            
            <block class="Magento\Customer\Block\Form\Login" name="customer_form_login" template="Magento_Customer::form/login.phtml">
                <container name="form.additional.info" as="form_additional_info">
                    <block class="Magento\Persistent\Block\Form\Remember" name="persistent.remember.me" template="Magento_Persistent::remember_me.phtml" before="-"/>
                </container>
            </block>
        </referenceContainer>
    </body>
</page>

And than go to your CMS page in admin and from there under design tab, select your option in Custom Layout Update, whatever you've named it, here I have named it loginform, so I'll select loginform inside my Custom Layout Update.


You can also define blocks as one after another, just saying as a possible way:

In your CMS page you have to define both blocks such as, in your CMS block:

{{block class="Magento\Customer\Block\Form\Login" template="Magento_Customer::form/login.phtml" name="customer.login.container"}}

{{block class="Magento\Persistent\Block\Form\Remember" template="Magento_Persistent::remember_me.phtml" name="persistent.remember.me"}}

Now, in your cms_page_view.xml file:

<move element="persistent.remember.me" destination="customer.login.container" before="-"/>

Another way:

In your CMS block where you're calling a login block, assign a new template file such as:

{{block 
  class="Magento\Customer\Block\Form\Login" 
  template="YourCustomVendorName_ModuleName::form/login.phtml" 
  name="customer.login.container"}}

Copy Magento_Customer::form/login.phtml file to YourCustomVendorName_ModuleName::form/login.phtml

And inside login.phtml file replace,

<?= $block->getChildHtml('form_additional_info') ?>

With

<?php
  echo $this->getLayout()
  ->createBlock('Magento\Persistent\Block\Form\Remember')
  ->setTemplate('Magento_Persistent::remember_me.phtml')
  ->toHtml();
  ?>

Now you should have Remember me same as login in your CMS page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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