1

I've got plenty of CMS pages to add. Each of them should use '2 colums with right column' layout. The default is '1 column'.

How can I change the default setting?

Tried this in local.xml:

<cms_page>
    <label>CMS Pages (All)</label>
    <reference name="root">
        <action method="setTemplate"><template>page/2columns-right.phtml</template></action>
        <!-- Mark root page block that template is applied -->
        <action method="setIsHandle"><applied>1</applied></action>
    </reference>
</cms_page>

but it didn't work.

I know I can do this individually for every page, but I want to set the default setting. So I don't have to set it 100 times for every single page.

2

Simply adding this in config.xml solved the issue:

<config>
    <global>
        <page>
            <layouts>
                <one_column module="page" translate="label">
                    <is_default>0</is_default>
                </one_column>
                <!-- default page for new cms pages -->
                <two_columns_right module="page" translate="label">
                    <is_default>1</is_default>
                </two_columns_right>
            </layouts>
          </page>
  • cool answer.. +1 for this.. it would be nice if we add the link that helped you to resolve this issue. – Rajeev K Tomy Aug 11 '15 at 7:44
  • where is config.xml file located? – zekia Sep 18 '18 at 6:46
1

Magento has this facility built-in. You just want to use that. For this, login into admin section of your site, go to CMS > Pages > [Select your CMS Page] > Design tab and choose 2column-right layout from there.

If the CMS Page has any custom layout set in the above section, then layout update that you have mentioned is never going to apply, since your changes has overwritten by the in-built facility that I have described above. So use the default feature. Simple and elegant.

EDIT

Your layout update is not working because, all of your cms pages updates that you have done through the layout update handle cms_page are overwritten by it's higher levels1 updates. Most probably, all of your CMS Pages has their own root template which set via backend (you can find this setting at CMS > Pages >[any cms page] > Design > layout). If you want to overcome this higher level layout update, the only possible way is listening to a special event cms_page_render.

Event Registration

File : app\code\local\Namespace\Module\etc\config.xml

<config>
    <frontend>
        <events>
            <cms_page_render>
                <observers>
                    <namespace_module>
                        <class>rkt_seo/observer</class>
                        <method>changeCMSPageTemplate</method>
                    </namespace_module>
                </observers>
            </cms_page_render>
        </events>
    </frontend>
    <global>
        <models>
            <namespace_module>
                <class>Namespace_Module_Model</class>
            </namespace_module>
        </models>
    </global>
</config>

Observer

File : app\code\local\Namespace\Module\Model\Observer.php

<?php
class Namespace_Module_Model_Observer
{
    const PAGE_EMPTY       = 'empty';
    const ONE_COLUMN       = 'one_column';
    const TWO_COLUMN_RIGHT = 'two_columns_right';
    const TWO_COLUMN_LEFT  = 'two_columns_left';
    const THREE_COLUMN     = 'three_columns';

    public function changeCMSPageTemplate(Varien_Event_Observer $observer)
    {
        $action = $observer->getEvent()->getControllerAction();
        $update = $action->getLayout()->getUpdate();
        $helper = $action->getLayout()->helper('page/layout');

        $pageLayout = $helper->getCurrentPageLayout();
        if ($pageLayout !== false) {
            $update->removeHandle($pageLayout->getLayoutHandle());
        }
        $helper->applyHandle(self::TWO_COLUMN_RIGHT);

        return $this;
    }
}

Theory

cms_page_render is fired after applying CMS Page specific page layout handle. In your case page_one_column layout update handle will get added. What we need is, we need to remove this layout update handle and then add our own page layout update handle, ie page_two_column_right. Observer will doing that job here.

Note 1: This update will affect all CMS Pages irrespective of the value at the layout section of a cms page (backend) under Design tab.

Note 2 : Since this observer overwrite admin layout template functionality, the only way to overwrite the changes that we have done via observer is using custom layout xml section comes under Design tab of each cms page. That means if you need to use a special page layout other than 2 column right for a specific CMS Page, then you should use the above section to do that.

Note 3 : If you have further doubts on how to setup the module, then you can use this.


1 : Higher level may be a layout update handle which is process after cms_page (eg: cms_page_view or cms_index_index, page_one_column etc). It also includes root templates that we set via admin section (ie under CMS > Pages >[any cms page] > Design > layout). It can also be the specific layout updates that you have specified in the xml section of a CMS Page.

  • Rajeev, I know I can do this INDIVIDUALLY, but I want to set THE DEFAULT setting. So I don't have to set it 100 times for every single page. – mati Aug 10 '15 at 8:12
  • @mati current code is not working ? – Rajeev K Tomy Aug 10 '15 at 8:24
  • No, still "1 column" is used as default every time I create new CMS page :( – mati Aug 10 '15 at 8:39
  • @mati see my update – Rajeev K Tomy Aug 11 '15 at 4:41
  • Rajeev, thank you for your effort! This is great. I investigated more and found much simpler solution, so I will add it and mark it as accepted. But your edit is very valuable for telling the way Magento works! – mati Aug 11 '15 at 7:35
0

Don't forget you will need to update the layout in magento's back end. By default it will use your layout.xml to apply the updates, However if the cms page in the database has something different magento will use that as per its fallback.

You can change a CMS layout under CMS > Pages > CMS_PAGE > Design and choose the layout desired.

  • "you will need to update the layout in magento's back end" - I don't want to update this for every one of 100+ pages. How can I set new default, so no manual update for every page is needed? – mati Aug 10 '15 at 8:38
  • You could create an upgrade script to loop over the cms pages and set the templates – rob3000 Aug 10 '15 at 8:42
  • That could work too, but people who'll add articles in the future are beyound the concept of layout, so the script would need to be run many times... Can it be done like in that answer? I tried to locate the correct xml for this setting, but no luck. – mati Aug 10 '15 at 8:48
  • You could look at looping over them initially and then maybe look at using an observer such as 'cms_page_prepare_save` or $this->_eventPrefix.'_save_before to change the tempalte before it is saved. – rob3000 Aug 10 '15 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.