2

I followed this solution to add the user login and user create form in my template file but this not work. Can you notice when I did an error ? (I have my block showed bu not the content of the 2 login and register form).

First, I create this block :

<?php
namespace Test\Account\Block\Form;

class MyAccount extends \Magento\Framework\View\Element\Template
{
    /**
    * @var \Magento\Customer\Model\Session
    */
    protected $_sessionManager;

    /**
    * @param \Magento\Backend\Block\Template\Context $context
    * @param \Magento\Customer\Model\Session $sessionManager
    */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Customer\Model\Session $sessionManager,
        array $data = []
    ) {
        $this->_sessionManager = $sessionManager;
        parent::__construct($context, $data);
    }

    public function customerIsLoggedIn() {
        return $this->_sessionManager->isLoggedIn();
    }
}

Next, I checked the location of login and register html and block : The html and block location of login/register block

I create this .phtml

<!-- Modal Trigger -->
<a class="btn" href="#loginRegister"><?php echo __('MY ACCOUNT'); ?></a>
<!-- Modal Structure -->
<div id="loginRegister" class="modal">
    <div class="modal-header right-align">
        <a href="#!" class="modal-action modal-close waves-effect waves-green btn-flat">x</a>
    </div>
    <div class="modal-content">
        <?php if ($this->customerIsLoggedIn()): ?>
            <p>CUSTOMER IS LOGIN</p>
        <?php else: ?>
            <p>CUSTOMER IS NOT LOGIN</p>
            <?php
            echo $this->getLayout()
            ->createBlock('Magento\Customer\Block\Form\Login\Info')
            ->setTemplate('Magento_Customer::newcustomer.phtm')
            ->toHtml();
            echo $this->getLayout()
            ->createBlock('Magento\Customer\Block\Form\Login')
            ->setTemplate('Magento_Customer::form/login.phtm')
            ->toHtml();
            ?>
        <?php endif; ?>
    </div>
</div>
<script type="text/javascript">
requirejs(['jquery', 'jquery.materialize'], function ($, jQueryMaterialize) {
    $('#loginRegister').modal();
});
</script>

And add my template by Test/Account/view/frontend/layout/default.xml

<?xml version="1.0" encoding="UTF-8"?>

<page xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='urn:magento:framework:View/Layout/etc/page_configuration.xsd'>
    <body>
        <!-- HEADER -->
        <referenceContainer name="shortcut.links">
            <block class="Test\Account\Block\Form\MyAccount" name="header.account-link" template="Test_Account::form/my_account.phtml"/>
        </referenceContainer>
    </body>
</page>
  • can you share you default.xml code as well. – Dhiren Vasoya Feb 15 '17 at 13:13
2

try like this

<?php echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('header-support')->toHtml();?>

header-support is identifier

in your case

  <?php echo $block->getLayout()->createBlock('Test\Account\Block\Form\MyAccount')->setBlockId('header.account-link')->toHtml();?>

like the way you can do it.

  • What it the identifier ? When I did a var_dump($block->getLayout()->createBlock('Magento\Customer\Block\Form\Login')); die(); the page shows me nothing. – Kpone Feb 15 '17 at 13:29
  • <block class="Lapisbard\Catalog\Block\Product\View\Extra" name="product.view.extra" template="Lapisbard_Catalog::product/view/extra.phtml" after="product.info.overview"> here product.view.extra is the identifier – Bojjaiah Feb 15 '17 at 13:33
  • Thanks, how I can get the identifier of the block which contain the login form then ? – Kpone Feb 15 '17 at 13:37
  • @Kpone you have layout file? in your module – Bojjaiah Feb 15 '17 at 13:45
  • The only layout file I defined Is default.xml to include my block in a part of my header (look at my post I updated it). – Kpone Feb 15 '17 at 13:46
2

I found the solution... my bad. I just saw now what it the error with this part :

echo $this->getLayout()
            ->createBlock('Magento\Customer\Block\Form\Login\Info')
            ->setTemplate('Magento_Customer::newcustomer.phtm')
            ->toHtml();
            echo $this->getLayout()
            ->createBlock('Magento\Customer\Block\Form\Login')
            ->setTemplate('Magento_Customer::form/login.phtm')
            ->toHtml();

I forget to put the "l" letter alert ".pthm". Lost 2 hours for this...

Your Answer

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

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