Question

I'm building a custom theme, using base/default as a, well, basis. I want to use Magento's built in Newsletter Signup functionality, but I don't want it to appear in the left block. For most pages, I want it to appear at the bottom of the content block, but for the Home Page, I want it somewhere in the middle of the content block. I can't figure out the best way to position the block where I want it when re-adding it with layout XML.

To do this, I started in my theme's local.xml to unset the left.newsletter block from the left block:

<layout version="0.1.0">
...
    <default>
    ...
        <reference name="left">
            <action method="unsetChild"><alias>left.newsletter</alias></action>
        </reference>
    ...
    </default>
...
</layout>

Then, since most of the site should have the Newsletter Signup form at the end of the content block, again in local.xml, below the above code but still within the default layout handle, I added:

        <reference name="content">
            <action method="insert">
                <block>left.newsletter</block>
                <siblingName></siblingName>
                <after>true</after>
            </action>
        </reference>

Hmm. I expected this to position the Newsletter Signup form at the end of the content block (based on my tracing of Mage_Core_Block_Abstract::insert()), but it placed it at the beginning instead! So this is one problem--why isn't the form being rendered at the bottom of the content block?

Another problem I have is that only for the Home Page, I want to move that Newsletter Signup block to a specific place within the content block, and, I'd like to be able to do this all from Magento's CMS Page Editor so that light editing and layout adjustment maintenance is easy.

Here's my Home Page content (entered in the page's Content tab in the CMS Page editor):

{{block type="cms/block" block_id="home_page_marquee"}}

<!-- PUT NEWSLETTER HERE -->

<div class="inner_container">
    {{widget type="my_namespace/slider_customproduct"}}
    {{widget type="my_namespace/slider_featuredproduct"}}
    {{widget type="my_namespace/slider_blogpost"}}
    <div id="about">
        {{block type="cms/block" block_id="home_page_about_us"}}
    </div>
    <div id="team">
        {{block type="cms/block" block_id="home_page_team"}}
    </div>
</div>

On this page, I'd like to get the Newsletter Signup block where that comment is.

I've tried a couple things. First, I tried using the CMS Page Editor's Design tab to enter some custom layout updates:

<reference name="content">
    <!-- Re-unset the newsletter block -->
    <action method="unsetChild"><alias>left.newsletter</alias></action>

    <!-- Re-re-insert the newsletter block -->
    <action method="insert">
        <block>left.newsletter</block>
        <siblingName>home_page_marquee</siblingName>
        <after>true</after>
    </action>
</reference>

This successfully re-removes the Newsletter Signup block from the bottom of the Home Page, but inserts it, ironically, at the end of the content block. I've tried a couple of leads on fixing this, such as giving my static CMS blocks a name attribute, and using the name in <siblingName>, but that didn't work.

I've also tried to add the Newsletter Signup block using a {{block}} tag. Back in the Home Page's Content:

{{block type="cms/block" block_id="home_page_marquee"}}

{{block type="newsletter/subscribe" name="left.newsletter" template="newsletter/subscribe.phtml"}}

<div class="inner_container">
    {{widget type="my_namespace/slider_customproduct"}}
    {{widget type="my_namespace/slider_featuredproduct"}}
...

This does work--the Newsletter Signup form appears where I want it--but I've got a feeling it's not re-inserting the unset newsletter.left block, but rather creating another newsletter.left block and leaving the unset one hanging.

So, my issues are two: 1. What's wrong with the way I'm trying to position the Newsletter Signup block for site-wide placement at the end of the content block? 2. What's the cleanest way I can override the site-wide positioning of the Newsletter Signup block specifically for the Home Page, and place it where I want it, via the page's layout XML in Magento's CMS Page Editor?

And if I'm going about it wrong, please feel free to let me know. Magento never fails to let me know it's more complicated than I know.

Was it helpful?

Solution

You are on the right track. Moving an existing block is best done with unsetChild and insert, but it's also okay to recreate the block as you have done it in the CMS. As you used the same name, the previously created block instance is replaced, and nothing has been rendered yet at this time. But you still can optimize a bit, even though it's not really necessary IMHO: Remove the block from the layout before it is instantiated and recreate it with a different name.

<remove name="left.newsletter" />
<block type="newsletter/subscribe" name="content.newsletter" template="newsletter/subscribe.phtml"/>

This leaves the positioning issue. The problem is that the default handle is processed before any page specific handles and that's where the actual content is added. So at the time where insert() is called, the block is inserted at the end, but at the end of an empty list.

I don't see an easy universal way to defer adding the block or adjust the sorting, so I'd suggest to add a new container after content in the page templates (1column.phtml etc.) and append the newsletter block there.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top