Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento: Add content block at the end of the structual block "content"

I'm trying to add a content block to Magento, which should be visible on every side below the main content. I want to archive this with a custom extension, so I can copy this extension and it workes without touching core design files. My extension includes the following layout update:

<default>
    <reference name="content">
        <block type="mymod/blockname" name="myblockname" after="-" template="mymod/block.phtml"/>
    </reference>
</default>

My problem is, that the attribute after="-" is not working. The block always showes up at the top of the content block. Seems before and after have no consequence. If I move the block to i.e. footer, the attributes before and after are working fine.

How can I place my block at the bottom of block "content"

like image 837
ChristianWahler Avatar asked Jul 20 '12 10:07

ChristianWahler


1 Answers

As far as I can see the problem is that you specify your block in the "default" layout handle while most of the content in the "content" block is added by other layout handles which are applied later. That's why the added dependencies in your XML registration file (mentioned by Fabian) are not helping.

Please consider these two options depending on your needs:

1. If you really want to include your block on all frontend pages

In your XML layout file (local.xml or a custom one), add a new layout handle:

<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">

    <!-- your other adjustments for default, category_product_view and so on go here -->

    <add_my_block>
        <reference name="content">
            <block type="mymod/blockname" name="myblockname" after="-" template="mymod/block.phtml"/>
        </reference>
    </add_my_block>
</layout>

Now you create an event observer to inject your layout handle into your layout:

    <?php

    class YourCompany_YourExtension_Model_Observer
    {
        /**
         * Adds a block at the end of the content block.
         * 
         * Uses the event 'controller_action_layout_load_before'.
         * 
         * @param Varien_Event_Observer $observer
         * @return YourCompany_YourExtension_Model_Observer
         */
        public function addBlockAtEndOfMainContent(Varien_Event_Observer $observer)
        {
            $layout = $observer->getEvent()->getLayout()->getUpdate();
            $layout->addHandle('add_my_block');
            return $this;
        }
    }

Then you register the event observer in your XML extension configuration file (config.xml):

<?xml version="1.0" encoding="UTF-8" ?>
<config>
    <modules>
        <YourCompany_YourExtension>
            <version>0.0.1</version>
        </YourCompany_YourExtension>
    </modules>

    <frontend>
        <events>
            <controller_action_layout_load_before>
                <observers>
                    <mymod_add_block_at_end_of_main_content>
                        <type>singleton</type>
                        <class>mymod/observer</class>
                        <method>addBlockAtEndOfMainContent</method>
                    </mymod_add_block_at_end_of_main_content>
                </observers>
            </controller_action_layout_load_before>
        </events>
        <!-- declaring your layout xml etc. -->
    </frontend>

    <global>
        <!-- declaring your block classes etc. -->
        <models>
            <mymod>
                <class>YourCompany_YourExtension_Model</class>
            </mymod>
        </models>
    </global>
</config>

Now your block should end up below the other blocks. I tested this successfully for the homepage, customer login page and category view page. If you have to exclude your block on a few pages, you can check in your event observer if the block should be excluded on that certain page.

2. If you want to include your block only on some pages

Add a layout handle to your XML layout file just as we did before but instead of creating and registering an event observer, just tell your XML layout file to use the custom layout handle in some areas:

<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">

    <catalog_category_default>
        <update handle="add_my_block" />
    </catalog_category_default>

    <catalog_category_layered>
        <update handle="add_my_block" />
    </catalog_category_layered>

    <cms_page>
        <update handle="add_my_block" />
    </cms_page>

    <!-- and so on -->

    <add_my_block>
        <reference name="content">
            <block type="mymod/blockname" name="myblockname" after="-" template="mymod/block.phtml"/>
        </reference>
    </add_my_block>

</layout>
like image 184
Matthias Zeis Avatar answered Nov 12 '22 06:11

Matthias Zeis