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

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.

Related

Magento how to define layout handle for all controller

Im looking for solution to define layout handle for whole controller insteat of controller_action. For example i want to define:
<mymodule_index></mymodule_index>
instead of
<mymodule_index_index></mymodule_index_index>
<mymodule_index_someaction></mumodule_index_someaction>
Thanks.
You can add a layout handle by running this :
$update = $this->getLayout()->getUpdate();
$update->addHandle('mymodule_index')
You can then add this piece of code to a protected "_initHandles" function within your controller that you would run for every action.
In my controller I've (re)implemented this method
public function loadLayout($handles = null, $generateBlocks = true, $generateXml = true)
{
return parent::loadLayout(array('default','mymodule_index'),$generateBlocks,$generateXml);
}
which will add an update handle called mymodule_index (not loosing the default one) for every controller's action.
You'll need to call loadLayout() in every controller's action, but that's how magento works...
please try below code....
public function indexAction(){
{
...
$update = $this->getLayout()->getUpdate();
$update->addHandle('mymodule_index');
$this->loadLayoutUpdates();
$this->generateLayoutXml()->generateLayoutBlocks();
$this->renderLayout();
}
public function samelocationAction(){
{
...
$update = $this->getLayout()->getUpdate();
$update->addHandle('mymodule_index');
$this->loadLayoutUpdates();
$this->generateLayoutXml()->generateLayoutBlocks();
$this->renderLayout();
}
I've found solution. In each next update you must provide <update handle="" /> like this:
<companies_catalog>
<label>Companies (All Pages)</label>
<reference name="root">
<action method="setTemplate"><template>page/2columns-left.phtml</template></action>
</reference>
<reference name="left">
<block type="businesscategory/companies_navigation" name="companies.navigation" before="-" template="businesscategory/companies/navigation.phtml" />
</reference>
</companies_catalog>
<companies_catalog_index>
<label>Companies (All Pages)</label>
<update handle="companies_catalog" />
<reference name="content">
<block type="businesscategory/companies_list" name="companies.list" template="businesscategory/companies/list.phtml" />
</reference>
</companies_catalog_index>
<companies_catalog_view>
<label>Company page</label>
<update handle="companies_catalog" />
<reference name="content">
<block type="businesscategory/companies_view" name="company.info" template="businesscategory/companies/view.phtml" />
</reference>
</companies_catalog_view>

where is this method doing in admin controller (Grid Serializer)?

<?php
class Excellence_Manager_Adminhtml_ManagerController extends Mage_Adminhtml_Controller_action
{
public function customerAction(){
$this->loadLayout();
$this->getLayout()->getBlock('customer.grid')
->setCustomers($this->getRequest()->getPost('customers', null));
$this->renderLayout();
}
I am following this grid serializer tute . I just can not understand where is this setCustomers coming from ?
Even in other tutes I saw that setClent(), setDog() etc. But where is get or it's not from the database either. not in the layout either I think. Please help thanks.
<manager_adminhtml_manager_customer>
<block type="core/text_list" name="root" output="toHtml">
<block type="manager/adminhtml_manager_edit_tab_grid" name="customer.grid"/>
<block type="adminhtml/widget_grid_serializer" name="grid_serializer">
<reference name="grid_serializer">
<action method="initSerializerBlock">
<grid_block_name>customer.grid</grid_block_name>
<data_callback>getSelectedCustomers</data_callback>
<hidden_input_name>links[customers]</hidden_input_name>
<reload_param_name>customers</reload_param_name>
</action>
<action method="addColumnInputName">
<input_name>position</input_name>
</action>
</reference>
</block>
</block>
I found it's not from hidden_input_name or reload_param_name.
__set() is run when writing data to inaccessible properties.
__get() is utilized for reading data from inaccessible properties.
Magento implements so called "Magic Methods" to set data in an object.
They implemented __set and __get which are called, when no method for e.g. setCustomers() is found, and use those methods to do setData('customers', "YOUR_VALUE");

Pager toolbar in custom module in magento

I use this tutorial and display products on the basis of multiple categories but now I am getting in issue is that the pager tool bar not working on that.
My block code is :
<reference name="content">
<block name="mymodule" type="mymodule/product_listcategories" template="catalog/product/list.phtml">
<action method="setCategories">
<ids>2,3,4</ids>
</action>
</block>
</reference>
I also add this code with above
<block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
<block type="page/html_pager" name="product_list_toolbar_pager"/>
</block>
<action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
</block>
It display toolbar but the toolbar is not working (limit,orderby).
My block code is
class Mymodule_Block_Product_Listcategories extends Mage_Catalog_Block_Product_List
{
protected function _getProductCollection()
{
$this->_productCollection = Mage::getModel('catalog/product')->getCollection();
$this->_productCollection->addAttributeToSelect('*');
if($this->getCategories()!="")
$this->_productCollection->addCategoriesFilter($this->getCategories());
return $this->_productCollection;
}
}
}
Does anyone know where is the problem? I think I'm missing some code for the pager? Thanks in advance
After search so much i got a solution.I didn't know whether it right or wrong way but it solve my problem.On my block i create intance of
$cpBlock = $this->getLayout()->getBlockSingleton('Mage_Catalog_Block_Product_List_Toolbar');
and access the pager core function like $this->_itemPerPage = $cpBlock->getLimit(); .In the above code itemperpage is the total no of items to be displayed on listing page.This code work if you create custom module and extend you block from list block.Thanks

Magento - move block from right to left

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>;

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')
);
}

Resources