Magento - move block from right to left - magento

Not sure why this is so difficult. If I understand THIS correctly, I should be able to swiftly accomplish my goals... But no joy.
So - I'm building my first theme, and still getting my head around layout...
I'm working specifically on the Catalog Product View page, and am converting this page from a right column layout to a left column layout. I simply want to move the blocks from the right into the left.
In the default catalog.xml, product_list_related is defined:
</catalog_product_view>
//...
<reference name="right">
<block type="catalog/product_list_related" name="catalog.product.related" before="-" template="catalog/product/list/related.phtml"/>
</reference>
</catalog_product_view>
In my local.xml, I'm simply trying to move this block:
<layout>
// bunch other page directives
<catalog_product_view>
<reference name="root">
<action method="setTemplate"><template>page/2columns-left.phtml</template></action>
</reference>
<reference name="right">
<action method="unsetChild"><name>catalog.product.related</name></action>
</reference>
<reference name="left">
<action method="insert"><blockName>catalog.product.related</blockName></action>
// note that that "catalog.leftnav" gets inserted as expected
<block type="catalog/layer_view" name="catalog.leftnav" after="-" template="catalog/layer/view.phtml"/>
</reference>
</catalog_product_view>
</layout>
As noted - inserting catalog.leftnav works as expected, so I'm assuming everything else is set up correctly. The target block renders as expected if I leave the template and other directives unchanged, which tells me that the block should render once its been properly unset and inserted...
This is driving me nuts... but what else is new with Magento.
Cheers -
b[]x
UPDATE
Because I simply can not get a local.xml override to work, I'm just falling back on a modified catalog.xml. I'm a reasonably smart guy... it worries me that I can't get this to work (and that magento just silently fails, whatever the case ) - but I can't waste anymore time dicking around with this stupid issue.
Moving on.
UPDATE, again.
Spent some time, now, working in magento and getting more familiar with its complexities. I came back to this issue today as I need to get my local.xml working right.
I really don't know what I had wrong, but this set of directives finally worked.
<reference name="right">
<action method="unsetChild">
<alias>catalog.product.related</alias>
</action>
</reference>
<reference name="left">
<action method="insert">
<block>catalog.product.related</block>
</action>
</reference>
A key point I'll make note of for others dealing with this:
Layout xml directives call available methods within magento classes. In this case, Page.xmls "Left" block is of type Mage_Core_Block_Text, which inherits from Mage_Core_Block_Abstract which contains the methods unsetChild and insert.
from Mage_Core_Block_Abstract :
/**
* Unset child block
*
* #param string $alias
* #return Mage_Core_Block_Abstract
*/
public function unsetChild($alias)
{
if (isset($this->_children[$alias])) {
unset($this->_children[$alias]);
}
if (!empty($this->_sortedChildren)) {
$key = array_search($alias, $this->_sortedChildren);
if ($key !== false) {
unset($this->_sortedChildren[$key]);
}
}
return $this;
}
and
/**
* Insert child block
*
* #param Mage_Core_Block_Abstract|string $block
* #param string $siblingName
* #param boolean $after
* #param string $alias
* #return object $this
*/
public function insert($block, $siblingName = '', $after = false, $alias = '')
{
if (is_string($block)) {
$block = $this->getLayout()->getBlock($block);
}
if (!$block) {
/*
* if we don't have block - don't throw exception because
* block can simply removed using layout method remove
*/
//Mage::throwException(Mage::helper('core')->__('Invalid block name to set child %s: %s', $alias, $block));
return $this;
}
if ($block->getIsAnonymous()) {
$this->setChild('', $block);
$name = $block->getNameInLayout();
} elseif ('' != $alias) {
$this->setChild($alias, $block);
$name = $block->getNameInLayout();
} else {
$name = $block->getNameInLayout();
$this->setChild($name, $block);
}
if ($siblingName === '') {
if ($after) {
array_push($this->_sortedChildren, $name);
} else {
array_unshift($this->_sortedChildren, $name);
}
} else {
$key = array_search($siblingName, $this->_sortedChildren);
if (false !== $key) {
if ($after) {
$key++;
}
array_splice($this->_sortedChildren, $key, 0, $name);
} else {
if ($after) {
array_push($this->_sortedChildren, $name);
} else {
array_unshift($this->_sortedChildren, $name);
}
}
$this->_sortInstructions[$name] = array($siblingName, (bool)$after, false !== $key);
}
return $this;
}
Local xml parameters are important, then, not in name (specifically), but in order ie:
<reference name="left">
<action method="insert">
<block>catalog.product.related</block>
<siblingName>catalog.leftnav</siblingName>
<after>1</after>
<alias>catalog_product_related</alias>
</action>
</reference>
Ultimately, this makes local.xml a really powerful method of manipulating the system, but if you are unfamiliar with it and the magento system, get ready for weeks or months of work to really get your head around it.
Cheers
Yet another update
I've run into the problem several times now, where a block I want to move has been removed. This is a problem, as any block that has been removed from layout is nuked forever.
However, with Alan Storm's very handy Unremove Plugin you can undo whats been done:
<checkout_onepage_index>
<x-unremove name="left" />
<reference name="right">
<action method="unsetChild">
<alias>checkout.progress.wrapper</alias>
</action>
</reference>
<reference name="left">
<action method="insert">
<block>checkout.progress.wrapper</block>
</action>
</reference>
</checkout_onepage_index>
It manages this feat by watching the layout object, and building a list of removed blocks, which can be later referenced.
Nice!

This is a dusty thread now, but for the record, this is the final answer I went with.
<reference name="right">
<action method="unsetChild">
<alias>checkout.progress.wrapper</alias>
</action>
</reference>
<reference name="left">
<action method="insert">
<block>checkout.progress.wrapper</block>
</action>
</reference>
Magento recommends including this in your local.xml, and it has proved an effective technique.

An alternative approach is to rename the "right" block to "left" rather than move blocks from right to left. However, this will not work if the "right" block is being removed in the first place as I suggested might be the case in my other answer.
<remove name="left" />
<reference name="right">
<action method="setNameInLayout"><name>left</name></action>
</reference>
<reference name="root">
<action method="setChild"><alias>left</alias><name>left</name></action>
<action method="unsetChild"><alias>right</alias></action>
</reference>
<!-- make sure nothing referencing "right" comes after this! -->

If you are just looking to change the output from 2col-right, to 2col-left, it would have been much, much easier to just change
<reference name="right" ...
to
<reference name="left" ...
There is no need to unset or insert any children or re-declare anything. You are overcomplicating things and duplicating code needlessly.
If you are making your own very besoke design too, I would suggest starting copying the entire ./app/design/frontend/base directory to ./app/design/frontend/mypackage - then work from the mypackage/default directory to re-skin your site. Its cleaner and easier. Others may comment on the potential issues of upgrade-ability if you copy every file (rather than just the ones you intend to modify), but it is by far a better practice and much less prone to errors, difficulties and general maintenance.
Edit: To follow up on this in more detail - have a look https://magento.stackexchange.com/a/3794/361

The "right" block is probably being removed with a <remove name="right" /> at some point in the layout. If so, this will cause the "catalog.product.related" block to never be added to the layout in the first place so there is no block to insert into the "left" block.
I'm pretty sure this is the issue, but to confirm, add a Mage::log("Removed $blockName"); in Mage_Core_Model_Layout->generateXml() inside the for loop and check the log after loading the page.
If I'm right, simply copy the <block..> into your local.xml and remove the "unsetChild" and "insert" actions.

I would do it this way: In your local.xml under e.g.
<reference name="right"></reference>
you remove the blocks, e.g.:
<remove name="right.poll">
and then you add the blocks in
<reference name="left"></reference>

Copying base/default to a local theme is a horrible idea. Every update to a core theme file on a Magento version upgrade leads to the same vulnerability of modifying app/code/core files -- needing you to diff the files out on upgrades.
Your proper course of action is modifying a single local.xml with your additions or overrides. If that doesn't suffice, use the proper syntax to create a module in app/code/community with your layout definitions and custom layout XML files.

I think the best answer to modify Magento layouts is given on Classyllama.com
http://www.classyllama.com/development/magento-development/the-better-way-to-modify-magento-layout
When you use the remove tag, it removes any blocks with the specified name from the entire layout, regardless of the context. So, if I remove right.newsletter in the context and that name is used in say the context, then both blocks will be removed. Because remove operates on the global context, you can only remove an element once. Since is being called in catalogsearch.xml, we have to unset it, or else we'll get an error.
<action method="unsetChild"><name>right.newsletter</name></action>;

Related

Magento second header

I have a Magento store with two separate headers. Let's say out of 20 pages 15 use header.phtml and 5 use headerGreen.phtml. I've already created my custom layout module however I can't seem to get the new header to appear on select pages. The new headerGreen.phtml file has been created as well. The issue lies when I try to call the new header in the newly created layout page.
<?php echo $this->getChildHtml('headerGreen') ?>
I dont think I've added it correctly to the local.xml and was hoping someone could help.
<default>
<reference name="header">
<block type="core/template" name="header" template="page/headerGreen.phtml" />
</reference>
</default>
The function getChildHtml is expecting you to send a name of a child in the layout as first parameter
app/code/core/Mage/Core/Block/Abstract.php defines it :
/**
* Retrieve child block HTML
*
* #param string $name
* #param boolean $useCache
* #param boolean $sorted
* #return string
*/
public function getChildHtml($name = '', $useCache = true, $sorted = false)
{
// the code is does not really matter here
}
So based on your layout you should actually do
<?php echo $this->getChildHtml('header') ?>
Because header is the name attribute in this xml node of your layout :
<block type="core/template" name="header" template="page/headerGreen.phtml" />
As said in the comments, your layout is also looking strange because you are actually telling that your new header is going to be a child of the current header.
Two thinks you can do :
Use the powerfulness of the action nodes :
<default>
<reference name="header">
<action method="setTemplate">
<template>page/headerGreen.phtml</template>
</action>
</reference>
</default>
Or redefine the whole header block
<default>
<reference name="root">
<block type="core/template" name="header" as="header" template="page/headerGreen.phtml" />
</reference>
</default>

Magento - Is it possible to specify multiple values for ifconfig?

I want to specify multiple values for ifconfig in layout xml.
<action method="setTemplate" ifconfig="mymodule/general/is_enabled">
<template>mymodule/test.phtml</template>
</action>
Is is possible to add below two conditions for one action?
ifconfig="mymodule/general/is_enabled"
ifconfig="mymodule/frontend/can_show"
Any suggestions would be most welcome.
You could use a helper method in your action parameter. Something like this
<action method="setTemplate">
<template helper="mymodule/myhelper/canShowIf"/>
</action>
will call setTemplate with the results of a call to
Mage::helper('mymodule/myhelper')->canShowIf();
And the following in your modules default helper:
public function canShowIf()
{
if($displayOnly = Mage::getStoreConfig('mymodule/general/is_enabled') == true)
// Do Something
}else if($displayOnly = Mage::getStoreConfig('mymodule/frontend/can_show') == true) {
// Do Something Else
}
return $displayOnly;
}
Implement your custom logic in canShowIf.
Define a function in Helper (Data.php)
<reference name="root">
<action method="setTemplate">
<template helper="modulename/getNewLayoutupdate"/>
</action>
</reference>
in helper function you can load template by conditions.
consider below scenario:
<catalog_category_default>
<reference name="product_list">
<action method="setTemplate" >
<template>mymodule/mytemplate.phtml</template>
</action>
</reference>
</catalog_category_default>
ifconfig : If return value is false, then it takes layout defined in base folder.
helper function : If return value is false, then does not take layout defined in base folder, and not template gets added. that's why empty block is shown.
ifconfig="mymodule/general/is_enabled"
ifconfig="mymodule/frontend/can_show"
why not create an additional config node
ifconfig="mymodule/frontend/is_enabled_can_show" and depending on this value proceed.

Searchresult wrong. Mage::registry('current_layer') not set

We redesign a magento shop. That shop had a very costumized searchresult page.
Now, after the redesign, the search does not work anymore.
I broke down the problem to the List.php (Mage\Catalog\Block\Product\List.php) where the function _getProductCollection() is called to get the results. This function gets a layer $layer = $this->getLayer(); and there is the main difference to the old version:
public function getLayer()
{
$layer = Mage::registry('current_layer');
if ($layer) {
return $layer;
}
return Mage::getSingleton('catalog/layer');
}
In the old version the Mage::registry('current_layer'); is set. In the new version its not set! And when it is not set it returns a new one return Mage::getSingleton('catalog/layer'); with all the products in it.
I can't think any further. How could I find the place where this layer is set in the old version?
What else could I do?
The search layer model is registered by the catalogsearch/layer block, which is usually added to the search page by the catalogsearch.xml layout file :
<catalogsearch_result_index translate="label">
<label>Quick Search Form</label>
<reference name="root">
<action method="setTemplate"><template>page/3columns.phtml</template></action>
</reference>
<reference name="left">
<block type="catalogsearch/layer" name="catalogsearch.leftnav" after="currency" template="catalog/layer/view.phtml"/>
</reference>
<!-- ... -->
</catalogsearch_result_index>
As it has certainly gone missing, you should add it back to your layout.

Unable to prevent Magento from Caching a Block

I'm working on a Magento 1.6 site, which has the following xml inside the home page's CMS "Layout Update XML" field:
<reference name="content">
<block type="catalog/navigation" name="catalog.category.home" as="homecategory" template="catalog/category/homecategory.phtml" />
</reference>
As the template shows randomized categories, I would like to disable caching for this block.
To do so, I attempted using getChildHtml('sub-block-template', false) with the following:
(homecategory has $this->getChildHtml('random_categories', false) in its template)
<reference name="content">
<block type="catalog/navigation" name="catalog.category.home" as="homecategory" useCache="false" template="catalog/category/homecategory.phtml">
<block type="catalog/navigation" name="catalog.category.home.randcats" as="random_categories" useCache="false" template="catalog/category/random.phtml" />
</block>
</reference>
So now I'm stuck, wondering why I can't prevent caching of that block, despite using the 'false' argument.
I had this same problem. I beleive it has to do something with the block type of type="catalog/navigation". I've seen this disabling of caching work on other types of blocks. Here is a fix for this block type and this problem:
phtml file change: make sure the second param is false
echo $this->getChildHtml('topCategoriesList',false);
xml file change:
Add these actions to the block
<block type="catalog/navigation" name="topCategoriesList" as="topCategoriesList" template="catalog/navigation/categorylist.phtml">
<action method="unsetData"><key>cache_lifetime</key></action>
<action method="unsetData"><key>cache_tags</key></action>
</block>
Have you tried forcing it by creating a new custom block type and overloading the caching functions? Extend the Mage_Catalog_Block_Product_List_Random class and create an empty pseudo-constructor:
protected function _construct() {}
This will prevent inheriting adding cache tags, lifetime, and other metadata to the block object. Then you can overload the cache key info as well so that it doesn't use any existing (or enabled) cache blocks. For example:
public function getCacheKeyInfo()
{
return array(
'MY_CACHE_TAG',
Mage::app()->getStore()->getId(),
(int)Mage::app()->getStore()->isCurrentlySecure(),
Mage::getDesign()->getPackageName(),
Mage::getDesign()->getTheme('template')
);
}

Add a link to Magento's My Account Page Conditionally

I would like to create a link on the My Account page that only get displays under certain conditions.
Right now I have the link display all the time by adding the following entry to my layout XML file:
<customer_account>
<reference name="customer_account_navigation">
<action method="addLink" translate="label" module="nie"><name>nie</name><path>nie</path><label>NIE Admin</label></action>
</reference>
</customer_account>
I am assuming there is a way to code this so that it only displays under certain circumstances.
The cart & checkout links already do something similar so their method can be copied.
Create a block. It won't be displaying directly so can be descended from the boring Mage_Core_Block_Abstract.
Give it a method where the conditional logic will go.
public function addNieLink()
{
if (($parentBlock = $this->getParentBlock()) && (CONDITION-GOES-HERE)) {
$parentBlock->addLink($this->_('NIE Admin'), 'nie', $this->_('NIE Admin'), true, array(), 50, null, 'class="top-link-cart"');
// see Mage_Page_Block_Template_Links::addLink()
}
}
protected function _prepareLayout()
{
// Add the special link automatically
$this->addNieLink();
return parent::_prepareLayout();
}
Put your check in place of CONDITION-GOES-HERE.
Add your block to the links block.
<customer_account>
<reference name="customer_account_navigation">
<block type="yourmodule/link" name="yourmodule.link" />
</reference>
</customer_account>
(Correct the block type here to your newly created link block)
The important bit is it calls getParentBlock() to find out where the link is to go.

Resources