23

Right now I'm exploring the internals of the admin section of Magento and I stumbled on this piece of XML:

File: app/design/adminhtml/default/default/layout/catalog.xml, around line 55

50            <block type="core/template" template="catalog/wysiwyg/js.phtml"/>
51        </reference>
52    </adminhtml_catalog_product_new>
53    
54    <adminhtml_catalog_product_edit>
55        <update handle="editor"/>
56        <reference name="content">
57            <block type="adminhtml/catalog_product_edit" name="product_edit"></block>
58        </reference>

What does the <update /> tag do?

TylerH
  • 20,799
  • 66
  • 75
  • 101
pancake
  • 1,923
  • 2
  • 21
  • 42

2 Answers2

60

The <update> basically pulls in another handle.

Assume you have this:

<layout>
   <foo>
      <reference name="header">
          <block type="cms/block" name="some_block" as="someBlock">
              <action method="setBlockId"><block_id>some_block</block_id></action>
          </block>
      </reference>
      <reference name="left">
          <block type="cms/block" name="some_totally_different_block" as="someTotallyDifferentBlock">
              <action method="setBlockId"><block_id>some_totally_different_block</block_id></action>
          </block>
      </reference>
   </foo>
   <bar>
      <update handle="foo" /> 
      <reference name="header">
          <block type="cms/block" name="some_other_block" as="someOtherBlock">
              <action method="setBlockId"><block_id>some_other_block</block_id></action>
          </block>
      </reference>
   </bar>
</layout>

The resulting XML for bar would be:

<layout>
   <bar>
      <reference name="header">
          <!-- Start of part pulled in from foo -->
          <block type="cms/block" name="some_block" as="someBlock">
              <action method="setBlockId"><block_id>some_block</block_id></action>
          </block>
          <!-- End of part pulled in from foo -->
          <block type="cms/block" name="some_other_block" as="someOtherBlock">
              <action method="setBlockId"><block_id>some_other_block</block_id></action>
          </block>
      </reference>
      <!-- Start of part pulled in from foo -->
      <reference name="left">
          <block type="cms/block" name="some_totally_different_block" as="someTotallyDifferentBlock">
              <action method="setBlockId"><block_id>some_totally_different_block</block_id></action>
          </block>
      </reference>
      <!-- End of part pulled in from foo -->
   </bar>
</layout>

tl;dr: The update handle is basically a "merge this layout with my current layout".

vzwick
  • 11,008
  • 5
  • 43
  • 63
8

This handle is used for merging existing layout handles to your current layout. In your example <update handle="editor"/> will add to the <adminhtml_catalog_product_edit> following content:

<editor>
    <reference name="head">
        <action method="setCanLoadExtJs"><flag>1</flag></action>
        <action method="addJs"><script>mage/adminhtml/variables.js</script></action>
        <action method="addJs"><script>mage/adminhtml/wysiwyg/widget.js</script></action>
        <action method="addJs"><script>lib/flex.js</script></action>
        <action method="addJs"><script>lib/FABridge.js</script></action>
        <action method="addJs"><script>mage/adminhtml/flexuploader.js</script></action>
        <action method="addJs"><script>mage/adminhtml/browser.js</script></action>
        <action method="addJs"><script>prototype/window.js</script></action>
        <action method="addItem"><type>js_css</type><name>prototype/windows/themes/default.css</name></action>
        <action method="addItem"><type>js_css</type><name>prototype/windows/themes/magento.css</name></action>
    </reference>
</editor>

("editor" handle is defined in app/design/adminhtml/default/default/layout/main.xml)

Roman Snitko
  • 3,655
  • 24
  • 29
  • 2
    How will Magento know that handle is inside main.xml ? What if another custom xml file contains handle? Will that also be merged ? – Hashid Hameed Oct 30 '14 at 08:37
  • I think Magento will merge those two handles. – Roman Snitko Oct 30 '14 at 10:54
  • 1
    Hmmm does that mean Magento will search all the xml files in layout folder for the handles ? Sorry but not quite understanding it. – Hashid Hameed Oct 31 '14 at 10:36
  • @HashidHameed XML files are all loaded and parsed already at this point. – doc_id May 26 '15 at 05:46
  • @rahmanisback How is it possible that all XML files are parsed at the time executing update handler, since that itself is an xml element which is yet to be parsed ? – Hashid Hameed May 26 '15 at 09:00
  • When the app starts the controller action, the tree of layouts has already been loaded then when magento loads the handles to build the XML layout to the view and find an `update` node, it searches in the tree the handle referenced by this node and uses its configuration. – Victor Aguilar Jun 26 '17 at 16:19