1 Comment

Change block type programmatically from controller

jquery not defined error in magento 2
Magento 2 - Error javascript and icon when clear cache?

How i can change block type programmatically from controller.

Related Posts

  • How to search product programatically in magento 2? (1)
    I want to search product in custom page so how search products programmatically in custom controller ? Below Magento 1.x search programmatically $queryText = Mage::helper('catalogSearch')->getQueryText(); $collection = Mage::getResourceModel('catalogsearch/query_collection') […]
  • How to Custom Comment History In Admin Dashboard Sales Order? (1)
    How to Custom Comment History In Page Sales Order And programmatically Comment History In Admin Order ? I've added Config.xml, <layout version="0.1.0"> <adminhtml_sales_order_view> <reference name="head"> <action […]
  • How to Custom Comment History In Admin Dashboard Sales Order? (1)
    How to Custom Comment History In Page Sales Order And programmatically Comment History In Admin Order ? I've added Config.xml, <layout version="0.1.0"> <adminhtml_sales_order_view> <reference name="head"> <action […]
  • Magento2: Adding the same custom attribute to all products of a certain Type? (1)
    How can I add the same custom option (checkbox) to all products of the same Type? In my case, certain products are of type "Event Ticket Product", due to changes from a 3rd party extension (Magenest_Tickets). I wish to add the product options programmatically rather than manually. […]
  • programatically inserting a .phtml on all product pages (0)
    I am making an extension have a .phtml file (which adds social media buttons below product price, and also does some manipulation with the product price etc). It needs to be programatically inserted into the product page, without actually modifying the view.phtml file in any way. […]
  • Magento 2 Load – Block (CMS Static Block in Magento 1 terms) (1)
    I am attempting to load a static block from my controller - but am having trouble locating the right syntax for doing this. What is the proper syntax to load a CMS block by identifier or block id and what factory supports this type of loading?
  • Magento 2 – load cms page (1)
    I am attempting to load a CMS Page from my controller. The community here was kind enough to help me with a CMS Static Block - and now I need a pointer in the right direction for a cms Page. What is the proper syntax to load a CMS Page by identifier or block id and what factory […]
  • How to create a custom block from billing.phtml (2)
    I would like to add a new block in the customer registration, edit and billing forms. This block will contain a dropdown control where the customer can select if he wants to register as a company or as an individual. At first, I edited billing.phtml and added the following: <li […]
  • how to update price of bundle product fixed price type programmatically? (1)
    I have created bundle product programmatically I want to update bundle product price for fixed price type.suggest me some solution.
  • How to save a customer custom_attribute value programmatically in Magento2? (1)
    I have setup the custom attribute according to this post. However I created a plugin of type <type name="MagentoCustomerApiCustomerRepositoryInterface"> to save my example custom_attribute. My plugin looks like this: public function aroundSave(CustomerRepository […]
jquery not defined error in magento 2
Magento 2 - Error javascript and icon when clear cache?

1 Comment on this article

  • User November 12, 2016

    Here i change login form url without change .phtml and layout xml file.

    Original login handler is

    <customer_account_login translate="label">
        <label>Customer Account Login Form</label>
        <!-- Mage_Customer -->
        <remove name="right"/>
        <remove name="left"/>
    
        <reference name="root">
            <action method="setTemplate"><template>page/1column.phtml</template></action>
        </reference>
        <reference name="content">
            <block type="customer/form_login" name="customer_form_login" template="customer/form/login.phtml" />
        </reference>
    </customer_account_login>
    

    I going to change “customer_form_login” block block type

    Create Controller which extend Mage_Customer_AccountController which contain loginAction()

    class Namespace_ModuleName_AccountController extends Mage_Customer_AccountController{
    public function loginAction()   {
      if ($this->_getSession()->isLoggedIn()) {
            $this->_redirect('*/*/');
            return;
        }
        $this->getResponse()->setHeader('Login-Required', 'true');
        $this->loadLayout();
    
        $update = $this->getLayout()->getUpdate();
        $update->addHandle('customer_account_login');
        $this->loadLayoutUpdates();
        $this->generateLayoutXml();
        $this->generateLayoutBlocks();
        $this->_isLayoutLoaded = true;
    
        $blockList = $this->getLayout()->getBlock('customer_form_login');
        $template = $blockList->getTemplate();
        $blockChildBlock = $blockList->getChild();
    
        $this->getLayout()->unsetBlock('customer_form_login');
        $parentBlock = $this->getLayout()->createBlock('blockname/customer_form_login','customer_form_login')->setTemplate($template);
    
        if($blockChildBlock){
            foreach($blockChildBlock as $childBlock){
                $this->getChildBlockLogin($parentBlock,$childBlock);
            }
        }
        $this->_initLayoutMessages('customer/session');
        $this->_initLayoutMessages('catalog/session');
        $this->renderLayout();   
     }
    }
    
    public function getChildBlockLogin($parentBlock,$childBlock){
    
        /*echo "ChildB: ".$childBlock->getNameInLayout()."<br/>";
        echo "Alias: ".$childBlock->getBlockAlias()."<br/>";
        echo "Child: ".$childBlock->countChildren()."<br/>";
        echo "Type: ".$childBlock->getType()."<br/>";
        echo "Template: ".$childBlock->getTemplate()."<br/>";*/
        $childName = $childBlock->getNameInLayout();
    
        if($childBlock->getBlockAlias()!=''){
            $childName = $childBlock->getBlockAlias();
        }
        if($childBlock->getTemplate()!=''){
            //$child = $this->getLayout()->createBlock($childBlock->getType(),$childName)->setTemplate($childBlock->getTemplate());
            $parentBlock->setChild($childName,$childBlock);
        }
        else{
            //$child = $this->getLayout()->createBlock($childBlock->getType(),$childName);
            $parentBlock->setChild($childName,$childBlock);
        }
        $parentBlockData = $this->getLayout()->getBlock($childBlock->getNameInLayout());
        $blockChildBlockData = $parentBlockData->getChild();
        if($blockChildBlockData){
            foreach($blockChildBlockData as $childBlockData){
                $this->getChildBlockLogin($parentBlockData,$childBlockData);
            }
        }       
     }
    }
    

    Create block “appcodelocalNamespaceModuleNameBlockCustomerFormLogin.php”

    class ModuleName_ModuleName_Block_Customer_Form_Login extends Mage_Customer_Block_Form_Login{
      public function getPostActionUrl(){
         return Mage::getUrl('modulefrontname/account/loginPost');
     }
    }
    

    From this code you can also copy original block child block in new created block. Try below code to test child blocks

    <customer_account_login translate="label">
        <label>Customer Account Login Form</label>
        <!-- Mage_Customer -->
        <remove name="right"/>
        <remove name="left"/>
    
        <reference name="root">
            <action method="setTemplate"><template>page/1column.phtml</template></action>
        </reference>
        <reference name="content">
            <block type="customer/form_login" name="customer_form_login" template="customer/form/login.phtml">
                <block type="core/template" name="test.block" template="customer/form/test.phtml" >
                    <block type="core/template" name="test.block1" as="testB1" template="customer/form/test1.phtml" />
                </block>
            </block>
        </reference>
    </customer_account_login>
    

    Create file in “appdesignfrontendbasedefaulttemplatecustomerformtest.phtml”

    Testing Test Message<br/>
    <?php echo $this->getChildHtml('testB1')?>
    

    Create file in “appdesignfrontendbasedefaulttemplatecustomerformtest1.phtml”

    Child Block<br/>
    

    This code return error while enable cache from admin. To fixed that issue we have to comment below code

    /*$update = $this->getLayout()->getUpdate();
    $update->addHandle('customer_account_login');
    $this->loadLayoutUpdates();
    $this->generateLayoutXml();
    $this->generateLayoutBlocks();
    $this->_isLayoutLoaded = true;*/
    

    and created layout file in “appdesignfrontendbasedefaultlayoutlayoutname.xml “

    <?xml version="1.0"?>
    <layout version="0.1.0">
        <layout_handler translate="label">
            <update handle="customer_account_login"/>
        </handler>
    </layout>
    

    This is not best solution for me because i did not want to use .xml file. Please suggest me if any one get good solution for this issue.

    Reply

Add a comment

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax