Config block in Magento - magento

I'm a newbie in Magento. I have a question. I config following the guide on this website
http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-4-magento-layouts-blocks-and-templates
<layout version="0.1.0">
<default>
<block type="page/html" name="root" output="toHtml" template="magentotutorial/helloworld/simple_page.phtml" />
</default>
</layout>
But it doesn't work. But I fix <default> to <helloworld_index_index>, it run.
I don't understand this issue. What is the diffrent?

<default> and <helloworld_index_index> are known as "Layout Handles".
If you study page.xml file in a layout folder of your theme or default theme, you will find that <default> layout handle has already been assigned with a "Block element" of name root. So, defining the root Block element again in the same layout handle doesn't make any sense to magento.
If you want to use a same Block element in the same layout handle then you should refer it using the Block element type <reference> as shown below:
<layout version="0.1.0">
<default>
<reference name="root">
<!-- Your changes here -->
</reference>
</default>
</layout>
Anyway, coming to the point, <helloworld_index_index> layout handle worked for you because it doesn't have a block element root already assigned somewhere else in layout xml files.

Related

How to hide a custom block on checkout Magento

I need to hide a custom block on all checkouts (onepage and cart).
How should I edit the checkout.xml to hide it?
I tried <remove name="footer_newsletter"/> but it doesn't work. The strange thing is that if I put <remove name="footer" /> all the footer is hidden.
So where is the problem? On blocks section the ID and name is footer_newsletter so it's not wrong.
First remove the changes you have done on checkout.xml for Add local.xml to your theme layout folder.
For example in default magento theme it would be in app/design/frontend/default/default/layout
To your theme's layout folder add local.xml. In this local.xml Write following code:
<?xml version="1.0"?>
<layout version="0.1.0">
<checkout_cart_index>
<reference name="footer">
<remove name="footer_newsletter"/>
</reference>
</checkout_cart_index>
<checkout_onepage_index>
<reference name="footer">
<remove name="footer_newsletter"/>
</reference>
</checkout_onepage_index>
</layout>
local.xml runs at the last after calling all design xml file. So it will override any changes done in any xml.
Try to put the block which you want to hide in Comment in the checkout.xml.
You may also try something like this in local.xml:
<your_desired_handle><!-- replace handle here -->
<reference name="parent_block_of_footer_newsletter"> <!-- replace with the name of the parent block that contains your footer_newsletter block -->
<action method="unsetChild"><alias>alias_of_footer_newsletter_block</alias></action> <!-- replace the alias with the value of "as" attribute of your footer_newsletter block, or it's name if it does not have an "as" attribute -->
</reference>
</your_desired_handle>

How to remove the side bar cart from all pages but keep on product listing page in magento using local.xml

I have following code in local.xml
<default>
<remove name="cart_sidebar" />
</default>
To remove the sidebar cart from all pages but I want to keep it on product listing page and product details page.
How can I do this?
To achieve this you need to use Unset / Remove method.
To move a child block from one parent to another, reference the direct parent, call unsetChild on it, and then use the insert/add block method to assign the block instance as child to a different parent.
Try below code in your local.xml
<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">
<default>
<reference name="right">
<action method="unsetChild"><name>cart_sidebar</name></action>
</reference>
</default>
<!--If you are not using layered navigation code start -->
<catalog_category_default>
<reference name="right">
<action method="insert"><block>cart_sidebar</block></action>
</reference>
</catalog_category_default>
<!--If you are not using layered navigation code end -->
<!--If you are using layered navigation code start -->
<catalog_category_layered>
<reference name="right">
<action method="insert"><block>cart_sidebar</block></action>
</reference>
</catalog_category_layered>
<!--If you are using layered navigation code end -->
<catalog_product_view>
<reference name="right">
<action method="insert"><block>cart_sidebar</block></action>
</reference>
</catalog_product_view>
</layout>

Overrule Magento template

I would like all my CMS pages (but not all pages) to use a custom template file, however when I use the setTemplate action in my local.xml file it's not changing the template. The block is rendering correctly but without the correct layout.
The XML I'm using right now is:
<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">
<cms_page_view>
<reference name="root">
<action method="setTemplate"><template>page/cms-page.phtml</template></action>
</reference>
<reference name="right">
<block type="catalog/navigation" name="default_page_view" template="navigation/game-menu.phtml"/>
</reference>
</cms_page_view>
</layout>
What am I doing wrong?
You aren't doing anything wrong - your directive is being overridden by the entity data. For the reason why, see Mage_Cms_Helper_Page::_renderPage():
protected function _renderPage(/*...*/)
{
//snip...
$action->getLayout()->getUpdate()
->addHandle('default')
->addHandle('cms_page');
$action->addActionLayoutHandles();
if ($page->getRootTemplate()) {
$handle = ($page->getCustomRootTemplate()
&& $page->getCustomRootTemplate() != 'empty'
&& $inRange) ? $page->getCustomRootTemplate() : $page->getRootTemplate();
$action->getLayout()->helper('page/layout')->applyHandle($handle);
}
//snip...
}
So, your directive is being processed under the full action name handle cms_page_view, which is added via the $action->addActionLayoutHandles(); call. Whereas CMS pages are practically always saved via the admin with a root_template value, this value will always override file-based directives.
While it would be possible to update the data, it would be at risk of being overwritten when In order to provide an alternate template which will be preserved when the page is edited via the admin, it's necessary to specify some configuration values and some corresponding layout XML. In your custom module's config XML (or in app/etc/local.xml if this is a non-distributed change):
<global>
<page>
<layouts>
<cms_page_custom>
<label>Empty</label>
<template>page/cms-page.phtml</template>
<layout_handle>cms_page_custom</layout_handle>
</cms_page_custom>
</layouts>
</page>
</global>
This will provide the option to the select input during CMS page administration. To complete this work, in your custom layout XML:
<cms_page_custom>
<reference name="root">
<action method="setTemplate"><template>page/cms-page.phtml</template></action>
<!-- Mark root page block that template is applied -->
<action method="setIsHandle"><applied>1</applied></action>
<action method="setLayoutCode"><name>empty</name></action>
</reference>
</cms_page_custom>

Change Position of Newsletter Sidebox in Magento

Thank you for any help in advance.
I am trying to change the order in which sideboxes appear in magento. Such as the: My Cart, Newsletter, Poll, Recently Viewed Products.
Currently I am trying to make the Newsletter box display last.
In app/design/frontend/default/MY_TEMPLATE/layout/newsletter.xml
I have the following code:
<default>
<!-- Mage_Newsletter -->
<reference name="left">
<block type="newsletter/subscribe" name="newsletter" after="-" template="newsletter/subscribe.phtml" />
</reference>
</default>
Yet even with after="-" as shown in the code, the newsletter is still showing up 1st.
I have flushed all caches, and have re-indexed every time I make a change to no avail.
EDIT:
Changing
<reference name="left">
TO
<reference name="right">
Moves it to the bottom of the list. However, the column is actually on the left. Any ideas why this works?
It's all in the order of left's sortedChildren entries. Try getting rid of the custom newsletter.xml, creating a local.xml file in your custom theme, and adding the following:
<?xml version="1.0"?>
<layout>
<default>
<action method="unsetChild" block="left">
<block>left.newsletter</block>
</action>
<action method="insert" block="left">
<block>left.newsletter</block>
<sib />
<after>1</after>
</action>
</default>
</layout>
What this does is remove the block reference from the list of sorted children blocks and then adds it again at the end. Ref. Mage_Core_Block_Abstract::insert() [link].
I've employed an unused but valid block attribute for the <action /> tag rather than wrap it in <reference />. I believe the effect is the same.
Check the block names(in layout xml files) which is displaying in your left/right sidebar.
You can find something like this
<reference name="right">
<block type="catalog/product_compare_sidebar" after="cart_sidebar" name="catalog.compare.sidebar" template="catalog/product/compare/sidebar.phtml"/>
</reference>
The above code will display cart sidebar then compare sidebar. Consider this is your last block in your sidebar. If you want to newsletter after this block you have to specify
<reference name="right">
<block type="newsletter/subscribe" name="newsletter" after="catalog.compare.sidebar" template="newsletter/subscribe.phtml" />
</reference>
You have to specify block names in after/before element tag. Find your left/right sidebars last block name and use that name in your newsletter block.

Magento: Update block position with before/after attribute from local.xml layout reference

I need to append the before attribute to a block via a layout update reference call.
This is my local.xml file:
<?xml version="1.0"?>
<layout version="0.1.0">
<default>
<reference name="content">
<block type="page/html_wrapper" name="content.footer" as="contentFooter" translate="label" after="-">
<label>Content Footer</label>
<action method="setElementId"><value>content-footer</value></action>
<action method="setElementClass"><value>clear</value></action>
</block>
</reference>
</default>
<catalog_category_default>
<reference name="breadcrumbs.container">
<action method="unsetChild"><name>category.title</name></action>
</reference>
<reference name="content">
<block type="catalog/category_view" name="category.title" template="catalog/category/title.phtml" before="content.footer"/>
</reference>
</catalog_category_default>
</layout>
My problem is, on the content block I create a content.footer block that you can assign widgets to in admin. I use after="-" on the content.footer block so in my mind, should put it ALWAYS at the bottom of the content block but this is not the case.
When you view a catalog category and it inserts the category.products block in to the content block, it displays underneath the content.footer block. The only way to make it work is if I redefine it in my local.xml and include all the child blocks in category.products, and set before before="content.footer".
So I thought why can't I use a reference to category.products in the catalog_category_default layout and set the block's before attribute, I tried the following:
<reference name="category.products">
<action method="setBefore"><value>content.footer</value></action>
</reference>
Which had no affect.
I also noticed the setAttribute() function in Mage_Core_Block_Abstract which saw it's just a wrapper for setData() but thought I would try it anyway, still nothing:
<reference name="category.products">
<action method="setAttribute"><key>before</key><value>content.footer</value></action>
</reference>
Is it possible to do what I want? Does before/after only apply to blocks in the same reference?
Layout updates are processed in order of layout update handles. Your block is being added last to content but only for the default LUH. Other handles (catalog_product_view, catalog_category_layered, etc) are processed after this handle.
If you truly need a content footer everywhere and want to make sure that it is the last thing inside of the content div, you should add your block to the root node in the default handle and customize the root templates (directly under page/, e.g. page/1column.phtml) by adding a getChildHtml('your.block') call after the getChildHtml('content') call. This will ensure that your block is always immediately at the end of the content blocks.

Resources