Magento Store - Remove Block using Update XML - magento

I am using this code in my template file to display a static block in my left sidebar:
<?= $this->getLayout()->createBlock('cms/block')->setBlockId('leftSB1')->toHtml() ?>
I would like to exclude the block from one of my CMS pages. How do I do this?
I think it requires adding code to the 'Layout Update XML' section but I'm not sure what exactly.

Someone else can correct me here, but I'm fairly sure that you're going to have trouble trying to accomplish this given the way you called the block. Normal layout updates allow you to remove blocks, but those are blocks that were also created with the layout (e.g. the Layout object knows about them after you call loadLayout()).
In your case, you create the block on the fly and then immediately use it to echo some HTML. If you want to be able to delete it with layout updates, try moving it into the layout files first, then use the normal layout block removal method:
<reference name="your_parent_block_name">
<remove name="leftSB1"/>
</reference>
Otherwise, you could hide it either in the PHP (By setting some global variable and checking it before outputting the block. Poor form but it might work.) or in CSS. Let me know if any of these work for you.
Thanks,
Joe

Include the block in your layout instead:
<cms_page>
<reference name="left">
<block type="cms/block" name="leftSB1">
<action method="setBlockId"><id>leftSB1</id></action>
</block>
</reference>
</cms_page>
And then $this->getChildHtml('leftSB1') in your sidebar, if you're not including children automatically.
(and then remove it from the particular page as in the previous answer)

Related

How to give reference to magento register blocks?

Let us consider i am working with registration form and i want to add new field in it say "Company". here I know all the process of creating module and add field in /customer/form/register.phtml file, but i want to do it in different way.
Here is my query, i want to do this with the help of xml file in which i create my new xml file and give reference to customer_form_register block with new .phtml file (without editing /customer/form/register.phtml) with all back end process of creating module same.
Problem
I now understand your question is asking if you can insert additional form fields on the customer register form without editing the customer/form/register.phtml template. Unfortunately, in Magento’s layout I don’t believe there is currently a way to create a reference to a block inside of another reference except from within the same. So, while there is not a very clean way to do this using only layout XML, there is one way you could accomplish it...
A Possible Solution
In order to insert a new child block into the customer_form_register block, we need to override the layout definition for that block. That is not ideal because you replace any other definitions of that block, so you need to be careful to incorporate any other necessary layout updates into your new one as well. One to consider is that this block gets redefined in captcha.xml, so if you need that functionality you need to add in those updates to your new definition as well.
We will then be inserting a new block, customer.form.register.newsletter. This is because that child block name is already being invoked in the register.phtml template like this $this->getChildHtml('customer.form.register.newsletter'), but it does not seem to be used for anything else that I noticed. So once we have defined our new block with this name, it will be inserted into the page below the existing newsletter checkbox:
<customer_account_create>
<reference name="content">
<block type="customer/form_register" name="customer_form_register" template="customer/form/register.phtml">
<block type="page/html_wrapper" name="customer.form.register.fields.before" as="form_fields_before" translate="label">
<label>Form Fields Before</label>
</block>
<!-- This is our new block. -->
<block type="core/template" name="customer.form.register.newsletter" template="customer/form/custom_register.phtml"/>
</block>
</reference>
</customer_account_create>
Alternative
I think you may want to consider something like this extension instead, since it seems to make it easy to add custom registration fields.

Where can I find reference names to find out where to put my block?

For example, I want to put my block on the product page before the description block. How would I find these reference names?
If I understand correctly, based on your question and subsequent comments, you are looking for a way to position blocks before other blocks in the layout?
If so, the layout system provides you with before and after block attributes, for this exact purpose.
The general use is to add a before or after attribute containing the block you would like to position - before or after.
Before Example
<block type="yourmodule/block_type" name="yourblock" before="the_block_name_to_position_before" />
After Example
<block type="yourmodule/block_type" name="yourblock" after="the_block_name_to_position_before" />
Positioned before product description
In your particular scenario, where you would like a block to be positioned before the product description block, you need a little extra xml due to how the product description block is being included into the layout:
<!-- file: app/design/frontend/your_package/your_theme/layout/local.xml -->
<catalog_product_view>
<reference name="product.info">
<block type="yourmodule/block_type" name="yourblock" template="yourmodule/template.phtml" before="product.description">
<action method="addToParentGroup"><group>detailed_info</group></action>
</block>
</reference>
</catalog_product_view>
The important thing to note here though, in terms of you original question, is the use of the before attribute for positioning.
In the Magento Control Panel set your Current Configuration Scope to a specific website. Then, under System -> Configuration -> Advanced -> Developer -> Debug` turn on Template Path Hints and Add Block Names to Hints.
After that, when you reload the page it will tell you what blocks to override to get the desired effect.

CMS page add block magento

I have in CMS->page "home page" file. In content i am writing line like this:
{{block type="myfolder/newfile" template="myfolder/newfile.phtml"}}
I want to render in content file newfile.phtml. What i am doing wrong ?
My new file is under: app\design\frontend\default\themeas\template\myfolder\newfile.phtml
You need to give your block a name. That's how Magento will reference the block. Also, your block type must be valid in order for the block to render. For default blocks try using type="core/template"
Your new code should look like this:
{{block type="core/template" name="my.block.name" template="myfolder/newfile.phtml"}}
Another note about the type attribute, its not actually a directory/file structure, rather, it's a URI that is mapped with the Magento autoloader. "Core" relates back to the Mage_Core_Block_Core class (under the app/code/core/Mage/Core directory) and then the info after the slash relates to the folders inside of that directory. So type="core/template" resolves to this class Mage_Core_Block_Core_Template which is located at app/code/core/Mage/Core/Block/Template.php. All the type attribute is doing is telling Magento which methods you need to load inside of your block.
A couple other block types you can try are:
For Product Lists: catalog/product_list
For Text Lists (blocks that automatically render out child blocks): core/text_list
For Category Blocks: catalog/category_view
There are plenty more, a good way to find new ones is to look at a block that does a similar action to what you are trying to do, and find where it is defined in the XML.
If you want to pass variables to the block, you can do something like:
{{block type="core/template" name="my.block.name" myvariable="5" template="myfolder/newfile.phtml"}}
Since Magento 1.9.2.2, or equvivalent patch you also need to grant permissions to the new block. You do this in the backend:
System | permissions | blocks
I.e if you wish to show:
{{block type="catalog/product_bestseller" name="krillo.bestseller" template="catalog/product/bestseller.phtml"}}
Add your block name "catalog/product_bestseller" and set the status to "allowed"
I'd like to offer an alternative:
The above answers work fine, however it's my personal preference to not insert blocks in the content of a CMS page as clients can (and have) deleted this crucial line when trying to edit text and content using the WYSIWYG.
You could add the following in the the Layout > Layout update XML section of a CMS page:
<reference name="content">
<block after="-" type="your/block_type" name="block.name" template="your/block/template/file.phtml"/>
<action method="insert" ifconfig="your/block_type">
<block>block.name</block>
</action>
</reference>
This way, clients are less likely to edit this tab!
Hope this helps anyone else with this issue!

Set Magento block template in layout xml

Having trouble setting a block template in Magento's layout xml. I'm attempting to set the template of a child block, not the entire page layout (almost all docs out there explain how to set template of the layout).
Background: I'm updating a layout handle in my custom action, using the <update /> tag in my module's layout xml.
Essentially, I want to reuse the layout and blocks of the built in product view action, but provide custom templates for a few blocks. (Not just overrides, these need to be brand new templates that are only triggered on my custom action and are themselves overrideable).
My layout html:
<?xml version="1.0"?>
<layout version="0.1.0">
<mymodule_product_index>
<update handle="catalog_product_view" />
<reference name="content">
<block type="catalog/product_view"
name="product.info" output="toHtml" template="mymodule/product.phtml" />
</reference>
<reference name="product.info.bundle">
<action method="setTemplate"><template>mymodule/customtemplate.phtml</template></action>
</reference>
</mymodule_product_index>
</layout>
The setTemplate on product.info.bundle never works; it doesn't seem to affect layout at all. I've tried wrapping the <reference> in other <reference> nodes from parent blocks with no effect. Is it possible to replace block templates in this way? I feel that my problem stems from the fact I'm using an <update />.
By the way, I know my layout xml is being loaded and there are no errors, the rest of the file is working fine, caching is disabled, have cleared cache anyway, etc.
Your approach is almost correct.
Two things:
1. Set a new template instead of instantiating a new block
Instead of just assigning a different template to the product.info block, you are creating a new instance with the same name, replacing the original instance, and then the new template is set on that. Instead use this:
<mymodule_product_index>
<update handle="catalog_product_view" />
<reference name="product.info">
<action method="setTemplate">
<template>mymodule/product.phtml</template>
</action>
</reference>
</mymodule_product_index>
That should take care of the product view template in a clean way.
2. Handle processing order
If you look at where the view block product.info.bundle for the bundled products is declared, you will see it happens in the bundle.xml file, in a layout update handle called <PRODUCT_TYPE_bundle>.
Your code is referencing the block from the <[route]_[controller]_[action]> layout handle, i.e. <mymodule_product_index>.
The thing to be aware of here is the processing order of layout handles.
Roughly it is:
<default>
<[route]_[controller]_[action]>
<custom_handles>
The <PRODUCT_TYPE_bundle> handle belongs to the third type of layout handles, which means it is processed after the <mymodule_product_index> handle.
In essence, you are referencing the block product.info.bundle before it has been declared.
To fix this you will need to use the <PRODUCT_TYPE_bundle> handle as well. Of course this will effect every bundled product display. Using layout XML only there is no clean way around that.
Here are a few suggestions how to solve that problem.
You could create a separate route in your module to show the bundled products, and then include the <PRODUCT_TYPE_bundle> handle using an update directive for that page, too.
In your custom action controller, you could add another layout update handle that is processed after <PRODUCT_TYPE_bundle>.
You could use an event observer to set the template on the product.info.bundle block if it is instantiated. One possibility would be the event controller_action_layout_generate_blocks_after.
You get the idea, there are many ways to work around this, but they require PHP.

In Magento, can I add a static block to the header by xml only?

I am trying to customize a theme using only local.xml whenever possible. I want to add a static block to the header without modifying header.phtml. This code works fine for the content area, but not for the header:
<default>
<reference name="content">
<block type="cms/block" name="how-it-works-button">
<action method="setBlockId"><block_id>how-it-works</block_id></action>
</block>
</reference>
</default>
Anybody know why? I thought that all I would need is to change “content” to “header”, but nothing shows up when I do.
Thanks for your help!
The content block is a special block known as a core/text_list block (PHP class Mage_Core_Block_Text_List). These blocks will automatically render out any child blocks that are added to them.
The header block, on the other hand, is a page/html_header block (PHP class Mage_Page_Block_Html_Header). This block class inherits from Mage_Core_Block_Template, making it a core/template block. Template blocks will only render sub-blocks if their corresponding phtml template requests the block. So, by adding your block to the header, you're only doing half the work you need to. You'll need to create a custom phtml template.
The simplest way to do this (post 1.4.1.1 is to, in your own theme, create a file at
template/page/html/header.phtml
And then at the end of this file add
<?php echo $this->getChildHtml('how-it-works-button'); ?>
Assuming you've added a block to header block via layout xml, this should render your template.
Please Try this
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('how-it-works')->toHtml() ?>
And this code in header.phtml
add output="toHtml" in the block tag. I think it is only that.

Resources