Magento – Ifconfig then show block

blockslayout

Im creating a module where in the admin i have created options to enable/disable a block in the header.

So i planned on using ifconfig to show the block or not depending on the admin value.

Ive declared the block and it shows as should but im struggling to get the ifconfig to work. I understand it can only be used on action tags.

So my question is how do i show my block depending on the config value?

Heres my current xml:

<reference name="header">
        <block name="untitled.social.footer.links" as="social_footer" template="untitled/social/links.phtml"/>
        <action method="setChild" ifconfig="untitled_social/platforms/footer_enabled">
            <alias>social_footer</alias>
            <block>untitled_social/platforms</block>
        </action>
</reference>

This doesnt work at the moment, so where am i going wrong?

Cheers

** EDIT **

Following Johnathon's suggestions below if have amended my code which now looks like this:

Layout XML

    <layout>
    <default>
        <reference name="footer">
            <action method="setChild" ifconfig="social/platforms/footer_enabled">
                <alias>social_footer</alias>
                <block>untitled_social/platforms</block>
            </action>
        </reference>
    </default>
</layout>

Construct in my block php file

public function __construct()
{
    $this->setTemplate('untitled/social/links.phtml');
}

Unfortunately it's still not showing. How can i check the returns value of the ifconfig?

Thanks

Best Answer

At the moment your layout XML isn't quite right. You are referencing the header block, and within that adding a block with the <block ... /> tag. After that you are essentially doing the same thing again by calling the setChild() method in the <action ...> tag. However when declaring <block ... /> you define a name, alias and template, but you don't set a block type which is required. Also unless you have reason to, you don't need to define an alias either. So instead do something like:

<block type="yourmodule/your_block" name="untitled.social.footer.links" template="untitled/social/links.phtml"/>

For the <action ...> call check the path for ifconfig, it's unusual to have an underscore in the first section of the path (untitled_social) but it depends on how you have defined the config entry in your system.xml. Bear in mind this should not be namespace_module, it needs to be according to the path you have defined in system.xml for that config value. This path may be the reason why it is not working correctly as if it is wrong it will always return false. Apart from that the <action ...> looks ok.

So if you want the block to show only according to the config value you need to remove the <block .../> declaration, otherwise remove <action ...> to have it always show. In both cases the header is not of block type core/text_list and so child blocks will not automatically render out. This means you need a call to the child block inside the header template to render out your template.

Related Topic