Why changing the root template doesn't work
Both
Mage_Cms_IndexController::indexAction()
and
Mage_Cms_IndexController::viewAction()
which are responsible for displaying the default homepage and a CMS page respectively call a helper:
Mage::helper('cms/page')->renderPage($this, $pageId)
If you jump into the helper (located at app/code/core/Mage/Cms/Helper/Page.php) and follow renderPage()
to the protected method _renderPage()
you'll see that Magento is checking two times for a root template (Magento CE 1.7.0.2):
if ($page->getRootTemplate()) {
$handle = ($page->getCustomRootTemplate()
&& $page->getCustomRootTemplate() != 'empty'
&& $inRange) ? $page->getCustomRootTemplate() : $page->getRootTemplate();
$action->getLayout()->helper('page/layout')->applyHandle($handle);
}
and
if ($page->getRootTemplate()) {
$action->getLayout()->helper('page/layout')
->applyTemplate($page->getRootTemplate());
}
Both calls happen after layout handles like "cms_page" and the like are processed, so you are out of luck here.
What you can do to change the root template
There is an event cms_page_render
which you can use for adding your own XML layout handle on CMS pages. Create your own extension (I'll spare some details here) and configure the event observer in your config.xml
:
<?xml version="1.0"?>
<config>
<modules>
<Emzee_Cms>
<version>0.0.1</version>
</Emzee_Cms>
</modules>
<global>
<events>
<cms_page_render>
<observers>
<emzee_cms_page_render>
<class>emzee_cms/observer</class>
<method>cms_page_render</method>
</emzee_cms_page_render>
</observers>
</cms_page_render>
</events>
<models>
<emzee_cms>
<class>Emzee_Cms_Model</class>
</emzee_cms>
</models>
</global>
</config>
Add your event observer:
<?php
class Emzee_Cms_Model_Observer
{
public function cms_page_render(Varien_Event_Observer $observer)
{
$action = $observer->getEvent()->getControllerAction();
$actionName = strtolower($action->getFullActionName());
$action->getLayout()->getUpdate()
->addHandle($actionName . '_after');
return $this;
}
}
Finally, add your new layout XML handle (e.g. in your local.xml
):
<?xml version="1.0"?>
<layout version="0.1.0">
<cms_index_index_after>
<reference name="root">
<action method="setTemplate"><template>page/1column.phtml</template></action>
</reference>
</cms_index_index_after>
</layout>
You can use this method as well to add a cms_page_view_after
handle or create page specific handles as cms_page_render
passes the $page
object to your observer.
Why you can't add a block to the 'reference left'
Are you sure that the template you're using has a left column? This question may sound silly but the default "2 columns with right bar" layout for example only offers a 'content' and a 'right' area. I can add blocks to the right column using cms_page
without problems so this could be the problem.
In general, you can only easily add blocks to references and echo them if
- the chosen root template uses the block you're referencing (see
app/design/frontend/base/default/template/page/*.phtml
) and
- the block you're referencing either is of type
core/text_list
, calls $this->getChildhtml()
without arguments or does something else to echo all child blocks.
Without further details, I can't tell you why your blocks are not echo'd in the left or right column.
Ok I think I have cracked it.
The problem appears to be when you are using the wrapping block
<block type="core/text_list" name="root">
Once you have defined a block of name "root" then it will be overridden if another block with the same name comes along later. (Correct me if I am wrong)
There are two solutions you could try.
- Combine both your xml into one section
Which would make your xml as follows
<regionpage_adminhtml_regionpage_customgrid>
<block type="core/text_list" name="root">
<block type="regionpage/adminhtml_regionpage_edit_tab_productgrid" name="regionpage.productgrid"/>
<block type="adminhtml/widget_grid_serializer" name="regionpage_productgrid">
<reference name="regionpage_productgrid">
<action method="initSerializerBlock">
<grid_block_name>regionpage.productgrid</grid_block_name>
<data_callback>getSelectedProducts</data_callback>
<hidden_input_name>links[productgrid]</hidden_input_name>
<reload_param_name>products_grid</reload_param_name>
</action>
<action method="addColumnInputName">
<input_name>position</input_name>
</action>
</reference>
</block>
<block type="regionpage/adminhtml_regionpage_edit_tab_articlegrid" name="regionpage.articlegrid"/>
<block type="adminhtml/widget_grid_serializer" name="regionpage_articlegrid">
<reference name="regionpage_articlegrid">
<action method="initSerializerBlock">
<grid_block_name>regionpage.articlegrid</grid_block_name>
<data_callback>getSelectedArticles</data_callback>
<hidden_input_name>links[articlegrid]</hidden_input_name>
<reload_param_name>articles_grid</reload_param_name>
</action>
<action method="addColumnInputName">
<input_name>position</input_name>
</action>
</reference>
</block>
</block>
</regionpage_adminhtml_regionpage_customgrid>
Simple replace in your second section the follow section
With a reference node which would make your second section look like
<regionpage_adminhtml_regionpage_customgrid>
<reference name="root">
<block type="regionpage/adminhtml_regionpage_edit_tab_articlegrid" name="regionpage.articlegrid"/>
<block type="adminhtml/widget_grid_serializer" name="regionpage_articlegrid">
<reference name="regionpage_articlegrid">
<action method="initSerializerBlock">
<grid_block_name>regionpage.articlegrid</grid_block_name>
<data_callback>getSelectedArticles</data_callback>
<hidden_input_name>links[articlegrid]</hidden_input_name>
<reload_param_name>articles_grid</reload_param_name>
</action>
<action method="addColumnInputName">
<input_name>position</input_name>
</action>
</reference>
</block>
</reference>
</regionpage_adminhtml_regionpage_customgrid>
Hope this fixes it and is a good enough explanation :)
Best Answer
What you have is a widget and you should implement it as such. Widgets have a very handy layout selection interface already made for you.
If you want to implement this without using the widget interface, you need a block method which will set the parent block, e.g.:
You would then want to ensure that this method is called when the block is instantiated: