Categories


Archives


Recent Posts


Categories


Programmatically Finding the Parent Theme in Magento 1.9

astorm

Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

One of the things Magento 1.9 brings to the table is unlimited theme fallback via a parent/child theme mechanism. If you ever need to find the current theme’s parent programmatically, here’s what you’ll want to do.

$design     = Mage::getDesign();
$config     = Mage::getSingleton('core/design_config');
$area       = $design->getArea();
$package    = $design->getPackageName();
$theme      = $design->getTheme('');            

$parent     = (string)$config->getNode($area . '/' . $package . '/' . $theme . '/parent');

var_dump($parent);

The core/design_config model/singleton is a new service model that’s responsible for reading the new theme.xml files. Then we fetch the area, design package, and current theme. Then we use this information to query the config’s XML directly. Unfortunately, there’s no clean method for grabbing this information — direct XML reading is how it’s handled in the core code as well.

#File: app/code/core/Mage/Core/Model/Design/Fallback.php
protected function _isInheritanceDefined($area, $package, $theme)
{
    $path = $area . '/' . $package . '/' . $theme . '/parent';
    return $this->_config->getNode($path) !== false;
}
//...
protected function _getFallbackScheme($area, $package, $theme)
{
    //...
    while ($parent = (string)$this->_config->getNode($area . '/' . $package . '/' . $theme . '/parent')) {
        //...
    }
}

Not how I would have done, but then I wasn’t doing it.

Copyright © Alan Storm 1975 – 2017 All Rights Reserved

Originally Posted: 19th May 2014