Magento changes layout dynamically via system variable
Asked Answered
B

3

5

Is there a way we could changes the layout of a Magento page (let's say a product category page) dynamically by using system variable which have been set on our own module? I want to be able to set my category page's default layout via my own module admin config panel. So that I don't have to deal with those confusing XML layout file each time I want to change my default layout for a certain magento page.

I know, on a phtml file, we could simply call our own module's system variable by calling Mage::getStoreConfig('module/scope/...') to use that system variable. but what if we want to use that system variable to change the whole layout which is set on the XML layout file by default.

I don't see any ways to pull that system variable value on the XML Layout file.

But I'm pretty sure there must be a right way to do that. So far, this is the closest clue that I've got

Magento - xml layouts, specify value for ifconfig?

But, still, I couldn't find any direct answer for what I really want to achieve

this is the content of my config.xml

<config>
    <modules>
        <Prem_Spectra>
            <version>0.1.0</version>
        </Prem_Spectra>
    </modules>

    <global>
        <models>
            <spectra>
                 <class>Prem_Spectra_Model</class>
            </spectra>
        </models>

        <helpers>
            <prem_spectra>
                <class>Prem_Spectra_Helper</class>
            </prem_spectra>
        </helpers>

    </global>
</config>
Brout answered 21/7, 2012 at 15:15 Comment(0)
C
10

This can be very easily achieved using layout xml and a simple method in your helper. I don't see any requirement for an observer here or anything else overly elaborate.

So, based on your requirements to change all category page layouts from your own modules store config value you will require the following in your layout xml:

<catalog_category_view>
    <reference name="root">
        <action method="setTemplate">
            <template helper="yourmodule/switchTemplate" />                  
        </action>
    </reference>
</catalog_category_view>

And the following in your modules default helper:

public function switchTemplate()
{
    $template = Mage::getStoreConfig('path_to/yourmodule/config');
    return $template;
}
Crisper answered 21/7, 2012 at 17:25 Comment(6)
Thanks drew... I'm trying it out... but I got this error: Fatal error: Class 'Mage_Prem_Spectra_Helper_Data' not found in D:\xampp\htdocs\magento17\app\Mage.php on line 546. I have add the function to my default helper. and set <template helper="Prem_spectra/switchTemplate">. I'm totally newbie with magento... could you please tell me what's wrong with my implementation?Brout
still doesn't. :( it output this error: Fatal error: Class 'Mage_Spectra_Helper_Data' not found in D:\xampp\htdocs\magento17\app\Mage.php on line 546Brout
Can you post the contents of your modules config.xml it may be that you are not declaring your helper nodeCrisper
I've edit my post and put it there... I'm pretty sure I have my helper working fine. Coz I have several other function that works well from that helperBrout
nevermind... I got it working with "prem_spectra/switchTemplate", this must be case sensitivity issue... anyway, thanks a lot drew.. :)Brout
you define in your config.xml the <helpers><yourModuleName>My_Module_Helper</... if you use the short-syntax from magento to call a helper, you have to use this node-name from the config.xml, in this case yourModuleName, if you don't do it, Mageento tries to find the helper in the Mage namespaceAccentuate
A
-1

we are talking about the template of the root-element, so 3columns, 2columns, etc? correct?

Implement an observer, listen to the event controller_action_layout_generate_blocks_before and then get the block in the observer and set the template

Mage::app()->getLayout()->getBlock('root')->setTemplate($myFancyTemplatePath);

This should do it.

Other idea, try the event controller_action_layout_load_before, but I think this is too early.

Accentuate answered 21/7, 2012 at 16:26 Comment(0)
I
-2

In addition to Fabian's answer:

You could perhaps extend the functionality of the category 'display modes'. Using the controller_action_layout_load_before event and then retrieve the display mode of the category and create a XML update handle for it.

    $category = Mage::registry('current_category');
    $handle = 'category_displaymode_' . strtolower($category->getDisplayMode());

    $layout = $observer->getEvent()->getLayout();
    $layout->getUpdate()->addHandle($handle);

This way you can pre-define all kinds of layouts in your local.xml and easily switch between them by adjusting the 'display mode' dropdown on the category edit page in the admin.

With some tweaking in the admin you can add additional display modes to the dropdown to make more types of custom display mode xml update handles available.

Infatuated answered 21/7, 2012 at 17:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.