Overrule Magento template - magento

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>

Related

render my block of custom field on the customer account edit page in magento

I want to add some custom fields using my block.
Problem is that block is not rendering inside the form tag or before the submit button(see the screenshot 1).
This is the code of rendering the block on Account edit page:
<?xml version="1.0" encoding="UTF-8"?>
<layout version="1.0.0">
<customer_account_edit>
<reference name="my.account.wrapper">
<block type="customfield/Register" name="customfield_registrationnnn" template="vss_customfield/register.phtml" />
</reference>
</customer_account_edit>
</layout>
Just use before =" module name of magento".
You can insert your code before magento
Actually there is no hook/handle to insert new/custom fields in account edit form.
Either you can overwrite the customer/form/edit.phtml in your module's layout file.
1) Put below snippet in your module's layout XML.
<customer_account_edit>
<reference name="customer_edit">
<action method="setTemplate">
<template>yourModule/customer/form/edit.phtml</template>
</action>
</reference>
</customer_account_edit>
2) Copy your theme's
customer/form/edit.phtml
to
yourModule/customer/form/edit.phtml
3) Now in your new file, you can put your custom fields.

Config block in 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.

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.

Magento - use an alternate "price.phtml" (in addition to the existing one)

I am looking for a way to have an alternate template/catalog/product/price.phml used in one specific location, and to continue using the existing price.phtml file in all other locations.
To explain further, I need to display the regular price, and then another special price right below it - but only on the product page (for the main product being displayed). This special price is not a price that can be calculated by the catalog price rules, so I wrote my own module to do the calculation. So, everywhere that I am displaying prices I want to display with the regular ol' template/catalog/product/price.phtml file... but for the product page (the main product - not the related, upsells, etc) I want to use my own custom template/catalog/product/price-custom.phtml template file. Can anybody help?
Normally I just look in the layout xml files (for example catalog.xml) to find these types of things, but price.phtml is kinda special - it isn't that simple. And for the life of me I can't figure out if there is an easy way to swap it out conditionally on the page being viewed. I am aware that I can just update price.phtml to always print out this extra price, and then use css to hide the price everywhere, but I would rather not do that if possible.
(Also you may want to know that I only have simple products.)
This can be done in a layout XML file:
<layout>
<PRODUCT_TYPE_simple>
<reference name="product.clone_prices">
<action method="setTemplate">
<template>catalog/product/price-custom.phtml</template>
</action>
</reference>
</PRODUCT_TYPE_simple>
</layout>
Create a local.xml file, put it in app/frontend/default/YOURTEMPLATE/layout
In the local.xml file, add:
<?xml version="1.0" encoding="UTF-8"?>
<layout>
<!-- Override price template on product view page -->
<PRODUCT_TYPE_simple>
<reference name="product.info.simple">
<action method="setTemplate">
<template>catalog/product/price_product_page.phtml</template>
</action>
</reference>
</PRODUCT_TYPE_simple>
<!-- /Override price template on product view page -->
</layout>
Create a copy of catalog/product/price.phtml and put it in YOURTEMPLATE/templates/product/product_price_page.phtml
This will override the price.phtml in the template, and replace it with product_price_page.phtml
Or in your php block.
See example here :
Mage_Catalog_Block_Product_Abstract
protected $_priceBlockDefaultTemplate = 'catalog/product/price.phtml';
protected $_tierPriceDefaultTemplate = 'catalog/product/view/tierprices.phtml';
I had a similar requirement recently, where a different price template for the product page was the preferred solution.
The price block appears to be something of a special case in Magento (in the RWD theme at least), it's defined in catalog.xml as simply a block type and name within the <default/> handle:
<block type="catalog/product_price_template" name="catalog_product_price_template" />
If you look around at how some core layout files set the price template, you'll find examples like this (from bundle.xml):
<reference name="catalog_product_price_template">
<action method="addPriceBlockType">
<type>bundle</type>
<block>bundle/catalog_product_price</block>
<template>bundle/catalog/product/price.phtml</template>
</action>
</reference>
They call a method called addPriceBlockType which you can find in Mage_Catalog_Block_Product_Abstract
Based on this and after a little experimentation, I found the following solution worked for me:
<catalog_product_view>
<reference name="product.info">
<action method="addPriceBlockType">
<type>simple</type>
<block>catalog/product_price</block>
<template>catalog/product/price_product_page.phtml</template>
</action>
<action method="addPriceBlockType">
<type>configurable</type>
<block>catalog/product_price</block>
<template>catalog/product/price_product_page.phtml</template>
</action>
<!-- Set for each product type as necessary e.g. bundled, virtual etc... -->
</reference>
</catalog_product_view>
The proper way to achieve it :
<PRODUCT_TYPE_simple>
<reference name="product.info.simple">
<action method="addPriceBlockType"><type>simple</type><block>catalog/product_price</block><template>catalog/product/price-product-page.phtml</template></action>
</reference>
</PRODUCT_TYPE_simple>
<PRODUCT_TYPE_configurable>
<reference name="product.info.configurable">
<action method="addPriceBlockType"><type>configurable</type><block>catalog/product_price</block><template>catalog/product/price-product-page.phtml</template></action>
</reference>
</PRODUCT_TYPE_configurable>
...

Magento - removing wishlist link in 1.4.2?

Previously in Magento, the wishlist link was added using the following (in wishlist.xml):
<action method="addWishlistLink"></action>
And you could override that and remove it using the following (in your local.xml):
<remove name="wishlist_link"/>
However, in the newest Magento, 1.4.2, they've changed how the wishlist link is added to the following:
<action method="addLinkBlock"><blockName>wishlist_link</blockName></action>
Anyone know how to remove the wishlist link now they’ve changed how it’s added?
It appears there's no publicly available way to reliably remove the wishlist link block from the layout. (you can skip to the end for a workaround)
The addLinkBlock assumes the presence of the block that's been passed, so using remove in the way you describe results in a fatal error being thrown
Fatal error: Call to a member function getPosition() on a non-object in /Users/alanstorm/Sites/magento1point4.2.dev/app/code/core/Mage/Page/Block/Template/Links.php on line 112
Here's the core code that causes that error
app/code/core/Mage/Page/Block/Template/Links.php
public function addLinkBlock($blockName)
{
$block = $this->getLayout()->getBlock($blockName);
$this->_links[$this->_getNewPosition((int)$block->getPosition())] = $block;
return $this;
}
This method assumes its going to be able to pull out a block by whatever name gets passed, so we can't just remove the wishlist_link block as we could in previous versions.
The only mechanism for removing a link appears to be the following method on the same block class
app/code/core/Mage/Page/Block/Template/Links.php
public function removeLinkByUrl($url)
{
foreach ($this->_links as $k => $v) {
if ($v->getUrl() == $url) {
unset($this->_links[$k]);
}
}
return $this;
}
However, this is done using string comparison, and there's no reliable way (that I know of) to generate a URL Object from a layout file, cast it as a string, and pass it into the method (this would be required, as there are numerous configuration settings that can change what the final string URL will be). That makes this method not helpful for our needs.
So, what we can do it modify the existing wishlist_link block to use a blank or non-existant template. This way the block still renders, but it renders as an empty string. The end result is we avoid the fatal error mentioned above, but still manage to remove the link from our selected pages.
The following would remove the link from all the pages using the default handle.
<!-- file: local.xml -->
<layout>
<default>
<reference name="wishlist_link">
<action method="setTemplate"><template>blank-link.phtml</template></action>
</reference>
</default>
</layout>
In your local.xml file,
<?xml version="1.0"?>
<layout version="0.1.0">
<default>
<reference name="root">
<reference name="top.links">
<!-- Remove wishlist link in magento 1.4.x and newer -->
<remove name="wishlist_link"/>
</reference>
</reference>
</default>
</layout>
You can remove the wishlist link from the admin panel System > Configuration > Wishlist > Enabled = "No"
Add the following to your local.xml file.
<reference name="top.links">
<remove name="wishlist_link"/>
</reference>
This works! I have removed Wishlink from Toplinks and wanted to add it back into another block but that doesn't seem possible when you remove it in this way. Sadly.
I know I'm years late here, but for all of those people who are still looking for answers to this.
I have a way to work around this issue that is only a bit of extra work but it's not hacky and it gives you FULL control of your top.links block.
Simply unset the top.links block and re-create it, it will be empty (no more wishlist_link block) and all you have to do is add whichever links you want inside of it! (Do all of this in your theme/layout/local.xml file of course).
<layout version="0.1.0">
<default>
<!-- HEADER -->
<reference name="header">
<!-- Unsetting the already existing top links block -->
<action method="unsetChild">
<name>topLinks</name>
</action>
<!-- Re-creating a new top links block -->
<block type="page/template_links" name="top.links" as="topLinks">
<!-- EXAMPLE: Account Dashboard Link -->
<action method="addLink" translate="label title" module="catalog">
<label>Account Dashboard</label>
<url helper="customer/getAccountUrl"/>
<title>Account Dashboard</title>
</action>
<!-- You can add any other links that you want -->
</block>
</reference>
</default>
</layout>
Also remember that for some links like Sign In and Log Out you will need to reference your top.links block inside the appropriate <customer_logged_out> and <customer_logged_in> handles instead of inside of <default> as a guide for this you can look at Magento's customer.xml file.
IMPORTANT: If there are any modules included in your project that add links to the top.links block, those links won't show up because local.xml is processed last, so just keep that in mind when using this method :)
I am a Certified Magento Front End Developer with over 3 years of experience and I have overcome LOTS of layout XML headaches to the point where we became best friends.

Resources