Magento - conditionals in layout files - magento

Is it possible in Magento to conditionally add blocks into a layout xml file?
Im thinking along the lines of having an admin config option checkbox - if checked then a block needs to be added to the page and vice versa if not checked.
I could think of a way of doing this via code but not the actualy layout file system itself.

The ifconfig paramater can be used to conditionally call an action method
<action method="someBlockMethod" ifconfig="path/to/config"><param1>value</param></action>
The path/to/config path is passed to Mage::getStoreConfigFlag() to return a boolean.
I'd try using this in combination with the insert method
<action method="insert" ifconfig="path/to/config"><param>block_name</param></action>
The block with the name or alias of block_name will need to be already inserted into the layout object by other PHP or XML, so you may need to take additional steps to unset it from its original blocks after inserting it into your new block.

you can try this (i haven't tried it myself):
<action ifconfig='your/extension/active'

Related

Magento - How to call a phtml template file from the URL

The question pretty much says it all. I have a store locator application. Which dynamically produces a URL like:
http://www.XXXX.com/storelocator/index/viewstore/id/XXX
And I have also the template file in right directory:
package/template/storelocator/view_store.phtml
Also the block in:
app/code/local/COMPANY/Storelocator
When I click on the ID that is generated by the Javascript, it returns a 404 error. So obviously the template file is not loading. But I am not sure what is causing the issue. Can someone give me some idea.
have you assign your template file in layout file if not try to assign your block in layout file.
<storelocator_index_viewstore>
<block type="Storelocator/Storelocator" template="storelocator/view_store.phtml"></block>
</storelocator_index_viewstore>
This way when your action is call magento render this block and call your phtml file

Set template in layout handle for Adminhtml

In the backend of my Magento Webshop I want to set a template for the blocks of System->Config. Therefore I use the layout handle of the Mage_Adminhtml_System_ConfigController which is adminhtml_system_config_index I think. The problem is, nothing happens. Even if want to remove blocks, nothing happens:
<adminhtml_system_config_index>
<reference name="left">
<action method="setTemplate">
<template>myModule/template.phtml</template>
</action>
</reference>
</adminhtml_system_config_index>
OR removing block:
<adminhtml_system_config_index>
<remove name="left" />
</adminhtml_system_config_index>
What am I doing wrong? I even tried system_config_index as layout handle.
You're doing three possible things wrong here.
First Tip: Are you editing the right XML file?
A lot of common layout problems come from the XML file you're editing not being the XML file that's loaded by the system. Switch on developer mode in your site, set display_errors to 1, and then deliberately introduce a non well formed error into your XML files and try to load the page.
<!-- note the trailing greater than sign at the end -->
<adminhtml_system_config_index>
...
</adminhtml_system_config_index> >
If the page loads without error, then you're not loading the XML file you think you are.
Second Tip: Are you using the correct layout handle?
I'm not sure you are. Despite the fact the default system configuration page uses the index action, this action actually forwards the page to the edit action.
#File: app/code/core/Mage/Adminhtml/controllers/System/ConfigController.php
public function indexAction()
{
$this->_forward('edit');
}
Forwarding means there's an internal redispatch, but no http redirect. That means your actual handle is
`adminhtml_system_config_edit`
This is why I use (disclaimer: and built, and sell) Commerce Bug. The Layout tab always displays the current handles, and I can avoid making assumptions that send me down the wrong path.
Third Tip: Are you confusing the block named left with the left hand column?
In general, Magento's container blocks (left, content, right, etc.) are not template blocks. Instead they're container blocks (text/list blocks to be precise) that hold and multiple template blocks.
This is another place I use Commerce Bug, specifically the new Graphviz feature. This will show you the block structure for any particular page. In a factory default system, you'd see something like this for the System Configuration page
As you can see, the left block has no template, so changing its template will have no effect. The block you probably want is left.child1.
Fourth Tip: Is the block you're trying to modify added by the layout XML.
Without getting too deeply into layout rendering (which would take a book), there are some block which are added after the layout generates all its blocks. If this is the case, your block will not be available in layout XML files.
If you look at the editAction method, you can see that the tabs block is added after loadLayout is called (look for adminhtml/system_config_tabs). This means it won't be available in layout xml files.
public function editAction()
{
//...
//the `loadLayout` method call creates blocks based on layout XML files
$this->loadLayout();
$this->_setActiveMenu('system/config');
$this->getLayout()->getBlock('menu')->setAdditionalCacheKeyInfo(array($current));
$this->_addBreadcrumb(Mage::helper('adminhtml')->__('System'), Mage::helper('adminhtml')->__('System'),
$this->getUrl('*/system'));
//this line add the `child1` block to the layout. (child one is the
//name chosen since the block has no explicit name
$this->getLayout()->getBlock('left')
->append($this->getLayout()->createBlock('adminhtml/system_config_tabs')->initTabs());
//...
This means you can't modify the template of this block from a layout XML file. You'll need to use a custom module that listens for an appropriate event and then makes your changes via PHP. I'd try the event controller_action_layout_render_before or controller_action_layout_render_before_adminhtml_system_config_edit with PHP code that looks something like
$block = Mage::getSingleton('core/layout')->getBlock('child1');
if($block)
{
$block->setTemplate('foo.phtml');
}

Magento: include custom block in view.phtml

I have created a block accrording to this tutorial: http://www.gravitywell.co.uk/blog/post/how-to-creating-your-own-custom-block-in-magento . Now I need to use my manufacturerblock.phtml on my pruduct view page (just somewhere closed to product description text).
In local.xml (under <reference name="header">) I added: (edit: I tried also under <reference name="product.info"> in <catalog_product_view>
<block type="aimitmanufacturerblock/manufacturerblock" name="manufacturerblock" as="manufacturerblock" template="aimitmanufacturerblock/manufacturerblock.phtml" />
and I used <?php echo $this->getChildHtml('manufacturerblock')?> in my view.phtml.
But it seems the phtml is not included.
Edit: Apologies, I should have followed the link to the tutorial you gave first, I see it's all about creating a new block type! However, I'll leave my answer for the moment - why do you need to create a new block type at all, there are loads of built in block types?. This answer from Clockworkgeek could be useful to you too;
Create a new Block in Magento
Original answer;
The issue is your block type - the block type isn't like a variable that you define yourself, there are specific built-in Magento block types which are used to instantiate the appropriate classes. This SO thread will be useful to you for further explanation;
Understanding Magento Block and Block Type
For another great explanation on block types read this one too;
What Block Type for Left Column in Magento Theme?
If you want to use the block in catalog/product/view.phtml, you need to reference product.info in the catalog_product_view handle.
And I can just assume that the module is activated, the class alias is declared, the class is created and the cache is flushed

layout xml conditional of $_request params

I understand it's in theory possible to use system config to determine the layout of a page (with the ifconfig option), is it possible to do something similar with variables passed by get or post? Or a cookie value for that matter?
This is not possible in standard Magento.
Depending on your use case there are some ways:
Implement an own XML attribute, for example <action method="foo" ifrequestvar="..."> The modle Aoe_LayoutConditions could give you a starting point:
Add an own layout handle. i.e. you check your condition that should be meet in normal PHP code (that works if the condition, i.e. the value of your $_REQUEST var is always the same) and then you can use this layout handle in the layout XML. You can add layout handles in an observer as shown in N98_CustomLayoutHandles.

Magento get which layout being used on phtml files

Is there a way I could get which layout being used on a certain phtml files?
Here in my case, I want to check what layout being used on catalog/list.phtml, I used that information to make conditional "if" on the product image grid size.
I've tried to google it out. But all the result is just explaining about xml layout things. The closest clue I got is this thread
Magento get layout for given page
which stated the use of this snippet
$left_block = $this->loadLayout()->getLayout()->getBlock('left');
but when I tried it on the phtml files, I got an exception error
UPDATE
joe's answer below has give me some more clue, the exception gone. But the behavior doesn't really what I need. That snippet of code seems to be just check whether the specified block is defined on the XML. What I really need is whether that block exist on a certain page.
In my case, I need to check what layout being used on catalog/product/list.phtml. if it's 3 columns, I'm gonna make the image resized smaller. If it 1 column, I'll make it bigger.
Is there any way I could do that??
If I read the question correctly, then try:
$this->getLayout()->getBlock('root')->getTemplate();
Remove loadLayout():
$left_block = $this->getLayout()->getBlock('left');
By the time you are in the PHTML file, the layout is already loaded.
In PHTML files, $this refers to the Mage_Core_Block_Template class (or a class that extends it). This class doesn't have a loadLayout() method defined, which is why you get an exception; instead, loadLayout() is part of Mage_Core_Controller_Varien_Action.

Resources