0

So i have a dropdown i created that fires a request to change a theme by saving the selected theme in the current session. I set up an event/observer that changes the theme based on that session variable:

    <events>
        <controller_action_layout_generate_blocks_before>
            <observers>
                <themesplayground>
                    <type>singleton</type>
                    <class>Rusman_ThemesPlayground_Model_Observer</class>
                    <method>switchTheme</method>
                </themesplayground>
            </observers>
        </controller_action_layout_generate_blocks_before>
    </events>

And here is observer method:

public function switchTheme($observer){
    Mage::log(__METHOD__,null,null,true);

    if( Mage::getSingleton('core/session')->getCurrentTheme() ){
        Mage::log(Mage::getSingleton('core/session')->getCurrentTheme(),null,null,true);

        foreach (array('layout', 'template', 'skin', 'locale') as $type) {
            Mage::getDesign()->setTheme($type, Mage::getSingleton('core/session')->getCurrentTheme());
        }

    }
}

The set up above works for the template files just fine, but the xml layout files(i only have local.xml for now) for the new theme don't seem to be getting loaded in. Is there another event i should observing? Or am i forgetting to update something?

6
  • 1
    you probably need to refresh the cache Mage::app()->cleanCache();
    – pzirkind
    Commented Mar 28, 2014 at 16:37
  • Caching is turned off. I added this to the end of method, still the same.
    – russjman
    Commented Mar 28, 2014 at 16:44
  • does it help if you manually delete the contents of 'var/cache'
    – pzirkind
    Commented Mar 28, 2014 at 16:50
  • It does not. Same thing.
    – russjman
    Commented Mar 28, 2014 at 16:52
  • Check this out: magento.stackexchange.com/questions/8079/… not sure if it will help you (note the event it looks at)
    – pzirkind
    Commented Mar 28, 2014 at 16:59

2 Answers 2

5

Let's say for example you have a basic magento setup and want to change the layout from the normal one to the modern one. The event controller_action_layout_load_before is the one for you as this is done early enough.

<frontend>
    <events>
        <controller_action_layout_load_before>
            <observers>
                <magento_test>
                    <class>magento_test/observer</class>
                    <method>changeLayoutEvent</method>
                </magento_test>
            </observers>
        </controller_action_layout_load_before>
    </events>
</frontend>

Then in your observer you would have something like:

public function changeLayoutEvent($observer) {
    Mage::getDesign()->setArea('frontend')
        ->setPackageName('default')
        ->setTheme('modern');
}

Just to make sure I would always set the package and area as magento will not be able to load the theme if the package matches a real package but it does not have the selected theme.

1
  • It works! :) :) :)
    – Alex
    Commented Jun 22, 2021 at 6:43
2

I usually hop right to source on this like this. Some var_dump debugging in

#File: app/code/core/Mage/Core/Model/Design/Package.php
protected function _renderFilename($file, array $params)
{
    switch ($params['_type']) {
        case 'skin':
            $dir = $this->getSkinBaseDir($params);
            break;

        case 'locale':
            $dir = $this->getLocaleBasedir($params);
            break;

        default:
            $dir = $this->getBaseDir($params);
            break;
    }
    return $dir . DS . $file;
}

Or the subsequent call to getBaseDir

#File: app/code/core/Mage/Core/Model/Design/Package.php
public function getBaseDir(array $params)
{
    $this->updateParamDefaults($params);
    $baseDir = (empty($params['_relative']) ? Mage::getBaseDir('design').DS : '').
        $params['_area'].DS.$params['_package'].DS.$params['_theme'].DS.$params['_type'];
    return $baseDir;
}

Should tell you what file paths Magento's trying to load from before falling back to the defaults. This is often enough to point to the problem with your approach (not changing enough system information, changing it too late)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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