Set a template for a block if there are blocks with the same name

I am trying to change the template for the "form" block in a layout like this:

<adminhtml_sales_order_create_index>
...
    <reference name="root">
            <block type="adminhtml/sales_order_create" name="content">
                <block type="adminhtml/sales_order_create_form" template="sales/order/create/form.phtml" name="form">
...
                        <block type="adminhtml/sales_order_create_shipping_method" template="sales/order/create/abstract.phtml" name="shipping_method">
                            <block type="adminhtml/sales_order_create_shipping_method_form" template="sales/order/create/shipping/method/form.phtml" name="form" />
                        </block>
                        <block type="adminhtml/sales_order_create_billing_method" template="sales/order/create/abstract.phtml" name="billing_method">
                            <block type="adminhtml/sales_order_create_billing_method_form" template="sales/order/create/billing/method/form.phtml" name="form" />
                        </block>
                        <block type="adminhtml/sales_order_create_newsletter" template="sales/order/create/abstract.phtml" name="newsletter">
                            <block type="adminhtml/sales_order_create_newsletter_form" template="sales/order/create/newsletter/form.phtml" name="form" />
                        </block>
...

I do

<adminhtml_sales_order_create_index>
    <reference name="form">
        <action method="setTemplate">
            <template>my_module/sales/order/create/form.phtml</template>
        </action>
    </reference>
</adminhtml_sales_order_create_index>

But that does not work. I think because the block with the name form exists in several places after. I just want to change it in the block type = "adminhtml / sales_order_create_form". How can i do this?

+4
source share
4 answers

@input

Block rewriting is not the best application. It’s better to watch the "core_block_abstract_to_html_before" event and change the template there, I think. How:

if ($observer->getBlock() instanceof Mage_Adminhtml_Block_Sales_Order_Create_Form) {
    $observer->getBlock()->setTemplate('my_module/newtemplate.phtml');
}

This works, and better, because you will not run into module conflicts if someone else rewrites this block. But I thought it was possible at the layout level.

+3
source

, .

, , , - setTemplate , , ,

class My_Module_Block_Adminhtml_Sales_Order_Create_Form extends Mage_Adminhtml_Block_Sales_Order_Create_Form
{
    public function _toHtml()
    {
        $this->setTemplate('my_module/sales/order/create/form.phtml');

        return parent::_toHtml();
    }
}

    <blocks>
        <adminhtml>
            <rewrite>
                <sales_order_create_form>My_Module_Block_Adminhtml_Sales_Order_Create_Form</sales_order_create_form>
            </rewrite>
        </adminhtml>
    </blocks>

, , .

+2

, . - .

, , :

1 : sales.xml .

2 : "" "Mage_Adminhtml_Block_Sales_Order_Create_Form"

Option 3 : subscribe to "adminhtml_block_html_before" and listen to the right block and then change the template.

However, if you are writing a community module, then these options are really good. if this is your own project, I would go for option 1

hope that helps

0
source

To overwrite the form file formname form.phtml from billing_method, you must do the following:

In config.xml:

<core_block_abstract_to_html_before>
   <observers>
       <mymodule_core_block_abstract_to_html_before>
              <class>MyModule_Model_MyPath</class>
              <method>setTemplateFile</method>
       </mymodule_core_block_abstract_to_html_before>
   </observers>
</core_block_abstract_to_html_before>

and in your observer:

 public function setTemplateFile(Varien_Event_Observer $observer)
 {
        if ($observer->getBlock() instanceof Mage_Adminhtml_Block_Sales_Order_Create_Billing_Method_Form) {
            $observer->getBlock()->setTemplate('mymodule/sales/order/create/billing/method/form.phtml');
        }
 }
0
source

Source: https://habr.com/ru/post/1526969/


All Articles