8

I want to insert new block on login page in magento 2. Can anyone help me how can I add new block on login page?

  • what mean by block?CMS Block or Block? – Amit Bera Jun 9 '16 at 5:44
  • It is CMS Block. – MaYaNk Jun 9 '16 at 5:46
9

Add new block by creating a new module. for example :

Make a new directory in app/code/

In this create another folders Foo/Bar

For creating module, create module.xml in Foo/Bar/etc/module.xml.

And paste this code

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Foo_Bar" setup_version="2.0.1"/>
</config>

Run this php bin/magento setup:upgrade in your CLI for create module.

Create Foo/Bar/Block/Baz.php

and paste this code

 <?php 
   namespace Foo\Bar\Block; 
 class Baz
    extends \Magento\Framework\View\Element\Template
 {
    public function getTitle()
 {
    return "New Block";
  }
}

create Foo/Bar/view/frontend/layout/customer_account_login.xml

and paste this code

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
    <referenceContainer name="content">
        <block class="Foo\Bar\Block\Baz" template="Foo_Bar::default/baz.phtml"/>
    </referenceContainer>
</body>
</page>

create Foo/Bar/view/frontend/templates/default/baz.phtml

and paste this code

<?php
?>
  <h1tag><?php echo $block->getTitle(); ?></h1tag>

Remove cache and check your block by reloading login page.

8

In Admin Panel : go to content > widgets > Add Widget > then

enter image description here

click on the continue button

enter image description here

and select the block you want to add.

2

Add it via widget, Add a new widget of CMS static block type and than in widget options select your specific page.

  • What you mean by physically? via XML or PHP code. What code you have in your block and what you want to achieve. – Sourabh Modi Jun 9 '16 at 7:17
  • 1
    physically means programmatically. via PHP code – MaYaNk Jun 9 '16 at 7:29

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.