Louis B.
Louis B.

Reputation: 2368

Magento extension layout update: Not possible in config.xml?

When an extension has to do a layout update, the following doesn't work for me:

Namespace/Module/etc/config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<config> 
    <modules>
        <Namespace_Modulename>
            <version>0.0.1</version>
        </Namespace_Modulename>
    </modules>
    <frontend>
        <layout>
            <updates>
                <catalog_product_view>
                    <reference name="content">
                        <remove name="product.info.upsell" />
                    </reference>
                </catalog_product_view>
            </updates>
        </layout>
    </frontend>
</config>

But this does work:

Namespace/Module/etc/config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<config> 
    <modules>
        <Namespace_Modulename>
            <version>0.0.1</version>
        </Namespace_Modulename>
    </modules>
    <frontend>
        <layout>
            <updates>
                <Namespace_Modulename>
                    <file>modulename.xml</file>
                </Namespace_Modulename>
            </updates>
        </layout>
    </frontend>
</config>

And then I just put the relevant layout update in: app/design/frontend/base/default/layout/modulename.xml

I could have sworn that I read about approach #1 in a tutorial some time ago, but now I can't find it anymore. Was that tutorial wrong and is approach #2 the correct way to do this? It just seems a bit hacky to put my layout update file into frontend/base... Any suggestions?

Upvotes: 0

Views: 1658

Answers (2)

benmarks
benmarks

Reputation: 23205

Mage_Core_Model_Layout_Update::getFileLayoutUpdatesXml() (link) is the method which will merge file-based layout updates for the purpose of configuring the view, and it does not possess logic for merging layout update directives from the configuration DOM.

Upvotes: 1

Alana Storm
Alana Storm

Reputation: 166046

If you read about it was either

  1. For an ancient version of Magento

  2. Blatantly incorrect

The Layout Update XML files are a separate system from the global config.xml configuration tree. Layout XML is loaded in

#File: app/code/core/Mage/Core/Model/Layout/Update.php
public function getFileLayoutUpdatesXml($area, $package, $theme, $storeId = null)
{
    if (null === $storeId) {
        $storeId = Mage::app()->getStore()->getId();
    }
    /* @var $design Mage_Core_Model_Design_Package */
    $design = Mage::getSingleton('core/design_package');
    $layoutXml = null;
    $elementClass = $this->getElementClass();
    $updatesRoot = Mage::app()->getConfig()->getNode($area.'/layout/updates');
    Mage::dispatchEvent('core_layout_update_updates_get_after', array('updates' => $updatesRoot));
    $updateFiles = array();
    foreach ($updatesRoot->children() as $updateNode) {
        if ($updateNode->file) {
            $module = $updateNode->getAttribute('module');
            if ($module && Mage::getStoreConfigFlag('advanced/modules_disable_output/' . $module, $storeId)) {
                continue;
            }
            $updateFiles[] = (string)$updateNode->file;
        }
    }
    // custom local layout updates file - load always last
    $updateFiles[] = 'local.xml';
    $layoutStr = '';
    foreach ($updateFiles as $file) {
        $filename = $design->getLayoutFilename($file, array(
            '_area'    => $area,
            '_package' => $package,
            '_theme'   => $theme
        ));
        if (!is_readable($filename)) {
            continue;
        }
        $fileStr = file_get_contents($filename);
        $fileStr = str_replace($this->_subst['from'], $this->_subst['to'], $fileStr);
        $fileXml = simplexml_load_string($fileStr, $elementClass);
        if (!$fileXml instanceof SimpleXMLElement) {
            continue;
        }
        $layoutStr .= $fileXml->innerXml();
    }
    $layoutXml = simplexml_load_string('<layouts>'.$layoutStr.'</layouts>', $elementClass);
    return $layoutXml;
}

While this code does get a reference to to the <updates/> node with

$updatesRoot = Mage::app()->getConfig()->getNode($area.'/layout/updates');

this is only used to pull together a list of file

foreach ($updatesRoot->children() as $updateNode) {
    if ($updateNode->file) {
        $module = $updateNode->getAttribute('module');
        if ($module && Mage::getStoreConfigFlag('advanced/modules_disable_output/' . $module, $storeId)) {
            continue;
        }
        $updateFiles[] = (string)$updateNode->file;
    }
}

Upvotes: 2

Related Questions