Understanding layout.xml syntax in Magento
2010-08-27 17:10
351 查看
I write this article about my understanding of layout.xml up to now. I use it to show you
How to add buttons in order/invoice/shipment/creditmemo without override Block class?
How to define an array as a parameter in layout.xml using <action method=”addButton”><param>array</param></action>?
How to assign any type of variables in layout.xml?
How to avoid overriding controller class but still get the result you want on a specific url?
as well.
All my understanding was inspired by the task: adding buttons in order view at backend to offer administrators extra order process facilities.
I love Magento class overriding architecture,but as I have learned the downside of overriding (Basically,there is only one chance of overriding for each class among all extension classes. Therefore,the more classes overridden,the greater chance of conflict between extensions.),I create a principle for my programming in Magento – do not override Magento class unless there is no other way around it.
So,back to the task. It would be very easy to add the button if I had overridden Mage_Adminhtml_Block_Sales_Order_View. It could be
However,I do not want to override Mage_Adminhtml_Block_Sales_Order_View. How about firing addButton method in layout.xml? I see an example of addTab at the left side
In the example,addTab method takes two parameters,one for tab name and the other for block name. Inside addTab method,it creates a block object from the block name,which is passed in as a string. In fact,the tag name <name> or <block> is not significant in layout.xml. It does not represent parameter name in method. But the position is significant – the first tag is passed into method as the first parameter,and ditto the followings.
Back to addButton method in Mage_Adminhtml_Block_Widget_Container. It takes at least two parameters,one as string and the second as associated array. So the layout.xml can be something like this:
The difficult part is in <onclick> – it requires a variable of current order id. After some digging,I find another example:
So,I create a helper method to return url for new button and modify layout.xml to
Unfortunately,it did not work. I assume the helper callback does not work if it is in the array. So I change the helper method to return an associated array directly as the second parameter for layout.xml and the final layout.xml is
It works perfectly. I am very proud of myself.
Some while ago,I overrode Mage_Catalog_ProductController just to register a variable in viewAction
Now I reviewing that approach stupid. I could register a variable using layout.xml without any class overriding!
How to add buttons in order/invoice/shipment/creditmemo without override Block class?
How to define an array as a parameter in layout.xml using <action method=”addButton”><param>array</param></action>?
How to assign any type of variables in layout.xml?
How to avoid overriding controller class but still get the result you want on a specific url?
as well.
All my understanding was inspired by the task: adding buttons in order view at backend to offer administrators extra order process facilities.
I love Magento class overriding architecture,but as I have learned the downside of overriding (Basically,there is only one chance of overriding for each class among all extension classes. Therefore,the more classes overridden,the greater chance of conflict between extensions.),I create a principle for my programming in Magento – do not override Magento class unless there is no other way around it.
So,back to the task. It would be very easy to add the button if I had overridden Mage_Adminhtml_Block_Sales_Order_View. It could be
1.
public
function
__construct() {
2.
parent::__construct();
3.
$this
->_addButton(
'new_button'
,
array
(
4.
'label'
=>Mage::helper(
'sales'
)->__(
'New Button'
),
5.
'onclick'
=>
"window.open('{$this->getUrlForNewButton()}')"
,
6.
))
7.
;
8.
}
However,I do not want to override Mage_Adminhtml_Block_Sales_Order_View. How about firing addButton method in layout.xml? I see an example of addTab at the left side
1.
<
block
type
=
"adminhtml/sales_order_view_tabs"
name
=
"sales_order_tabs"
>
2.
<
action
method
=
"addTab"
><
name
>order_info</
name
><
block
>order_tab_info</
block
></
action
>
3.
<
action
method
=
"addTab"
><
name
>order_invoices</
name
><
block
>adminhtml/sales_order_view_tab_invoices</
block
></
action
>
4.
<
action
method
=
"addTab"
><
name
>order_creditmemos</
name
><
block
>adminhtml/sales_order_view_tab_creditmemos</
block
></
action
>
5.
<
action
method
=
"addTab"
><
name
>order_shipments</
name
><
block
>adminhtml/sales_order_view_tab_shipments</
block
></
action
>
6.
<
action
method
=
"addTab"
><
name
>order_history</
name
><
block
>adminhtml/sales_order_view_tab_history</
block
></
action
>
7.
</
block
>
In the example,addTab method takes two parameters,one for tab name and the other for block name. Inside addTab method,it creates a block object from the block name,which is passed in as a string. In fact,the tag name <name> or <block> is not significant in layout.xml. It does not represent parameter name in method. But the position is significant – the first tag is passed into method as the first parameter,and ditto the followings.
Back to addButton method in Mage_Adminhtml_Block_Widget_Container. It takes at least two parameters,one as string and the second as associated array. So the layout.xml can be something like this:
01.
<
reference
name
=
"sales_order_edit"
>
02.
<
action
method
=
"addButton"
translate
=
"label"
>
03.
<
name
>new_button</
name
>
04.
<
button_array
>
<!-- this tag name is not significant -->
05.
<
label
>New Button</
label
>
06.
<
onclick
>window.open('{$this->getUrlForNewButton()}')</
onclick
>
07.
</
button_array
>
08.
</
action
>
09.
</
reference
>
The difficult part is in <onclick> – it requires a variable of current order id. After some digging,I find another example:
1.
<
reference
name
=
"footer_links"
>
2.
<
action
method
=
"addLink"
translate
=
"label title"
module
=
"catalog"
ifconfig
=
"catalog/seo/site_map"
><
label
>Site Map</
label
><
url
helper
=
"catalog/map/getCategoryUrl"
/><
title
>Site Map</
title
></
action
>
3.
</
reference
>
So,I create a helper method to return url for new button and modify layout.xml to
01.
<
reference
name
=
"sales_order_edit"
>
02.
<
action
method
=
"addButton"
translate
=
"label"
>
03.
<
name
>new_button</
name
>
04.
<
button_array
>
<!-- this tag name is not significant -->
05.
<
label
>New Button</
label
>
06.
<
onclick
helper
=
"mymodule/getUrlForNewButton"
/>
07.
</
button_array
>
08.
</
action
>
09.
</
reference
>
Unfortunately,it did not work. I assume the helper callback does not work if it is in the array. So I change the helper method to return an associated array directly as the second parameter for layout.xml and the final layout.xml is
1.
<
reference
name
=
"sales_order_edit"
>
2.
<
action
method
=
"addButton"
translate
=
"label"
>
3.
<
name
>new_button</
name
>
4.
<
button
helper
=
"mymodule/getNewButtonSecondParam"
/>
5.
</
action
>
6.
</
reference
>
It works perfectly. I am very proud of myself.
Some while ago,I overrode Mage_Catalog_ProductController just to register a variable in viewAction
1.
public
function
viewAction() {
2.
Mage::register(
'my_var'
,
$value
);
3.
parent::viewAction();
4.
}
Now I reviewing that approach stupid. I could register a variable using layout.xml without any class overriding!
相关文章推荐
- Understanding layout.xml syntax in Magento
- Fix layout.xml caching causing blocks to render other blocks in Magento
- Is appending block by handle possible in Magento layout xml?
- magento layout xml 小结
- Android development - 'missing theme error' in eclipse ide for layout xml files
- XML in Data Management : Understanding and Applying Them Together
- In Depth Magento System Configuration(magento system.xml说明)
- findViewById() returns null for custom component in layout XML, not for other components
- 当在写android的xml文件中出现Unexpected text found in layout file: """的解决办法
- 探析magento的Layout xml Part 2
- Hive启动时,遇到java.net.URISyntaxException: Relative path in absolute URI
- URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs) in pom.xml
- [转载]官方视频教学《XML Support in SQL Server 2005》
- jeesite部署到Tomcat后,无法访问,cannot be resolved in either web.xml or the jar files deployed with this application
- XML中几个Layout的通俗解释
- Custom shipping method in Magento
- Qt QScrollArea and layout in code
- 【已解决】This absolute uri http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or th
- 'url' requires a non-empty first argument. The syntax changed in Django 1.5, see the docs
- The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml...