Hi,
I am writing a Magento module. For that I want to call a core block inside my module's handler. I don't want to modify or extend the core block. I just want to call it inside my layout handler. Any suggestions?
The block I want to insert is located in
adminhtml/sales/order/view/history.php
Following handler is in the sales.xml which contains the above Histrory.php block
<adminhtml_sales_order_addcomment>
<block type="adminhtml/sales_order_view_history" name="order_history" template="sales/order/view/history.phtml" output="toHtml"/>
</adminhtml_sales_order_addcomment>
This is my layout.xml
<orderadmin_adminhtml_orderadmin_search>
<update handle="orderadmin_orderadmin_search" />
<reference name="content">
<!-- I want to insert the following block -->
<block type="adminhtml/sales_order_view_history" name="order_history" template="sales/order/view/history.phtml" output="toHtml"/>
</reference>
</orderadmin_adminhtml_orderadmin_search>
But it causes the following error.
Fatal error: Call to a member function getId() on a non-object in \app\code\core\Mage\Adminhtml\Block\Sales\Order\View\History.php on line 79
The issue is nothing to do the with xml layout, that is infact correct and will work as it is.
the issue is because this block is expecting an order to be in the registry, to enable it to grab the history.
You should set an order (the order you wish to use for viewing history) in the registry inside either your controller, or your modules block before the history block is rendered.
// load your order here..
Mage::register('sales_order', $order);
You have to do like this in your code :
<!-- this is my handler -->
<orderadmin_adminhtml_orderadmin_search>
<update handle="orderadmin_orderadmin_search" />
<reference name="content">
<block type="orderadmin/adminhtml_search" name="search_order" />
<!-- I want to call the core block here -->
Pick up from core layouts the block you wanted and paste here as it
is, it will get rendered
</reference>
</orderadmin_adminhtml_orderadmin_search>
Related
I would like to add a shopping cart block a cms page, but whenever I try, nothing happens...not even an error.
I've tried following this tutorial, http://www.magento.cc/how-to-use-php-on-a-cms-page.html.
So I created the new folders in app/code/local and then a Test.php file, yet when I try to include
{{block type="YourModule_Custom/test" my_param1="value 1" another_param="value 2"}}
in the cms page, nothing appears.
Here's my code in the Test.php page:
<?php
class YourModule_Custom_Block_Test extends Mage_Core_Block_Abstract
{
protected function _toHtml()
{
echo 'TEST';
$this->getChildHtml('header');
return $html;
}
}
You can do a local.xml update. And place your content as .phtml file
<cms_index_index>
<reference name="content">
<block type="Your_custom/Block" name="home_main" as="home_main" template="cms/default/home.phtml">
</block>
</reference>
</cms_index_index>
Create a file named home.phtml in cms/default/ Then you can use your own block type to use your custom module / functions
Then add your home page content in there.
I don't know what are you trying to achieve. But from your question, I have a strong feeling that, you are trying to set a template of your own through CMS page. If that is the case, let us analyze why your block didn't show any output.
Your block definition is like this
{{block type="YourModule_Custom/test" my_param1="value 1" another_param="value 2"}}
There is no problem with this defintion. But it is good, if you add name to your block. If you set a template along with that, then you don't need any backend code for setting a template for your block. That is your block should look like this
{{block type="YourModule_Custom/test" name="test.block" template="test.phtml"}}
Now when magento encounter this, it will find your block, set described name to that block (for later reference this name will be used. However it is not relevant in this case), set template specified to your block and then render the content in that template.
So you should have a block of type that you specified. You had it right now(you dont need that _toHtml() inside that). Along with that you need a template file test.phtml and it should be in the location app/design/frontend/<your_package>/<your_theme>/template/test.phtml. You dont have that file right now. So create it and add this content inside that file
<p><?php echo "I am here. Can you see me ?"; ?></p>
Now check your CMS page output. You can see that content. Isn't it ?
So right now what you are trying to do is, instead of setting a template along with that block definition, you are trying to set it through your block. Is it wrong ? Obviously, NO. There are some cases, where we need to go with that way. I assume you really need it. So again let us redifine our block in this form.
{{block type="YourModule_Custom/test" name="test.block"}}
Hmm. Here we didn't set a template to this block right now. You can hence set it through your block definition. You used _toHtml() method.
<?php
class YourModule_Custom_Block_Test extends Mage_Core_Block_Abstract
{
protected function _toHtml()
{
echo 'TEST';
$this->getChildHtml('header');
return $html;
}
}
?>
This method is using to set a template and then renders the content. So you are in the right track. But the problem here is, you are not setting any template!! plus your method returns a variable $html which holds nothing. So what should we return through a _toHtml() ? The answer lies in Mage_Core_Block_Template. Let us look on _toHtml() definition
protected function _toHtml()
{
if (!$this->getTemplate()) {
return '';
}
$html = $this->renderView();
return $html;
}
Basically what this does is, it checks whether a template is set , if not return nothing. If it does renders it. That means, it is obvious that we need to set a template. So your block should look like this.
<?php
class YourModule_Custom_Block_Test extends Mage_Core_Block_Template
{
protected function _toHtml()
{
$this->setTemplate('test.phtml');
$html = parent::_toHtml();
return $html;
}
}
Note that your block extends from Mage_Core_Block_Template rather from Mage_Core_Bock_Abstract. This is because setTemplate() method is defined in Mage_Core_Block_Template class. In _toHtml(), we are setting our template and then leave rest to our parent block. Now check whether content in test.phtml is showing in your CMS page. It does right ?
I don't understand exact what do you want. but by your question my understanding is you want to display shopping cart block into any CMS page like Home page, About us page etc.
If my understanding is right then here is one solution for you.
You can insert this code into your cms page from admin.
Admin -> CMS -> Pages -> Select any page on which you want to display block -> Click on design tab from left navigation ->Under Page Layout section insert the below code in "Layout Update XML" field. Click on save.
<reference name="content">
<block type="checkout/cart" name="checkout.cart">
<action method="setCartTemplate"><value>checkout/cart.phtml</value></action>
<action method="setEmptyTemplate"><value>checkout/cart/noItems.phtml</value></action>
<action method="chooseTemplate"/>
<action method="addItemRender"><type>simple</type><block>checkout/cart_item_renderer</block><template>checkout/cart/item/default.phtml</template></action>
<action method="addItemRender"><type>grouped</type><block>checkout/cart_item_renderer_grouped</block><template>checkout/cart/item/default.phtml</template></action>
<action method="addItemRender"><type>configurable</type><block>checkout/cart_item_renderer_configurable</block><template>checkout/cart/item/default.phtml</template></action>
<block type="core/text_list" name="checkout.cart.top_methods" as="top_methods" translate="label">
<label>Payment Methods Before Checkout Button</label>
<block type="checkout/onepage_link" name="checkout.cart.methods.onepage" template="checkout/onepage/link.phtml"/>
</block>
<block type="page/html_wrapper" name="checkout.cart.form.before" as="form_before" translate="label">
<label>Shopping Cart Form Before</label>
</block>
<block type="core/text_list" name="checkout.cart.methods" as="methods" translate="label">
<label>Payment Methods After Checkout Button</label>
<block type="checkout/onepage_link" name="checkout.cart.methods.onepage" template="checkout/onepage/link.phtml"/>
<block type="checkout/multishipping_link" name="checkout.cart.methods.multishipping" template="checkout/multishipping/link.phtml"/>
</block>
<block type="checkout/cart_coupon" name="checkout.cart.coupon" as="coupon" template="checkout/cart/coupon.phtml"/>
<block type="checkout/cart_shipping" name="checkout.cart.shipping" as="shipping" template="checkout/cart/shipping.phtml"/>
<block type="checkout/cart_crosssell" name="checkout.cart.crosssell" as="crosssell" template="checkout/cart/crosssell.phtml"/>
<block type="checkout/cart_totals" name="checkout.cart.totals" as="totals" template="checkout/cart/totals.phtml"/>
</block>
</reference>
After that you will see shopping cart block on your CMS page. If you don't need any block from them you can remove that blck from the above code.
if you try to call add to cart from cms page then add url like
checkout/cart/add?product=$id&qty=$qty
Example-
<a href="{{store url='checkout/cart/add?product=5&qty=3'}}">
<img src="{{skin url='images/addtocart.jpg'
}}" alt="product5" /></a>
I'm trying to add updateinfo block onto customer page.
I put this code inside customer.xml layout
<block type="core/text_list" name="updateinfo" as="updateinfo"/>
<reference name="updateinfo">
<block type="core/template" name="updateinfoBlock" template="customer/update_info.phtml" />
</reference>
and added update_info.phtml into customer folder.
also added
<?php echo $this->getChildHtml('updateinfo') ?>
to customer page
but I'm still not getting anything. Stuck.
Try like the below code
<block type="core/text_list" name="updateinfo" as="updateinfo">
<block type="core/template" name="updateinfoBlock" template="customer/update_info.phtml" />
</block>
to get to this block you can get like this
<?php echo $this->getChildHtml('updateinfo') ?>
Try:
getChildHtml('updateinfoBlock') ?>
instead of:
getChildHtml('updateinfo') ?>
If you want to use getChildHtml(), you want to set as attribute of your block along with name attribute. Name attribute is normally using to refer in the layout file itself.If you need to call a block through a template file, the value specified in as attribute is used. So you need your block definition something like this
<block type="core/template" name="updateinfoBlock" as="update.info.block" template="customer/update_info.phtml" />
and you need to call this block as
<?php $this->getChildHtml('update.info.block'); ?>
However the parent block of your core/template block is of type core/text_list. Children inside that type of block will render automatically. Mean you don't need to call those children by getChildHtml() method. Some examples are left, right, content blocks. Blocks inside them will render automatically.
So put your block of type core/template anywhere you need in your layout directly and call it as I shown above. You don't need to enclose it in another block as you did, unless it is essential.
I am trying to change Top links structure. I don't want to mess with xml files in default theme. I have only one xml file with everything inside. I don't want to create multiple xml files in my theme layout folder. I am trying to unset action inside top.links block.
This is the way I am trying to do:
<reference name="top.links">
<action method="unsetChild"><child>addLink</child></action>
<!-- <remove name="checkout_cart_link" /> -->
</reference>
When I put remove tag it works perfectly. But when I try to do unsetChild for addLink action it doesn't work. Do you know maybe how I should figure this out. I don't want separate xml files with the same names as in default theme.
The unsetChild function is from the Abstract block model and is used for removing a block that is a child of the current block. The block with the name top.links doesn't have a child block that is called addLink, hence the fact your configuration doesn't do anything. The action nodes with the method addLink call the addLink function on the Mage_Page_Block_Template_Links block. If the objective is to remove one of the links, you will need to use the action removeLinkByUrl.
<reference name="top.links">
<action method="removeLinkByUrl"><url>link/here</url></action>
</reference>
The exact arguments will obviously depend on what link you are attempting to remove.
I have a small custom module which is supposed to show on all product pages, but it doesn't. It only shows when setting its reference to "content", but I want it to display witin the already existing product view divs, thus, display it within the "product.info" reference.
Here's my code:
app/design/frontend/default/company/layout/company/socialbuttons.xml
<?xml version="1.0"?>
<layout>
<catalog_product_view>
<reference name="product.info">
<block type="core/template" name="company_socialbuttons" template="company/socialbuttons.phtml" />
</reference>
</catalog_product_view>
</layout>
I've spent hours finding the error, without success and if I can't find a solution soon I will do it the ugly way (calling the phtml inside the parent phtml). What am I doing wrong?
product.info handle is an instance of catalog/product_view block, and content is an instance of core/text_list block. The latter block is rendering every child block, even if there is no call to getChildHtml() method in the template.
So, if you want to add your block to product.info, you should call getChildHtml() method in the template.
Or, you can try reference of another blocks in product.info block: alert.urls or product.info.extrahint
If directly referencing the product.info block then you will have to use a call to getChildHtml() in your product view template:
echo $this->getChildHtml('company_socialbuttons');
I asked this question yesterday Static block on home page in Magento, which answered my question about hooking a cms/block to an existing block (content, in that example).
But now I would like to know how to create my own block.
I have this in my .phtml template:
<?php echo $this->getChildHtml('home_flash') ?>
And this in my cms.xml file
<reference name="home_flash">
<block type="cms/block" name="home-page-flash" before="content">
<action method="setBlockId"><block_id>home-page-flash</block_id></action>
</block>
</reference>
But this is not working.
I have also tried to create my own block type, (by copying the breadcrumbs declaration) in the page.xml file:
<block type="page/html_home_block" name="home_block" as="home_block" template="page/template/home_block.phtml"/>
That file exists but isn't being rendered.
However when I reference the block like this:
<block type="page/html_breadcrumbs" name="home_block" as="home_block" template="page/template/home_block.phtml"/>
It renders my home block template, but the original cms/block is not attached to it.
Hope all the different cases show what is happening and highlight the gap in my knowledge well enough for someone to answer, do I have to "register" my new "home_block" type somewhere?
There are many different blocks available that you can use without creating your own. In this case I think core/text_list would be suitable because it doesn't require a template and can have as many child blocks within it as you need.
<?xml version="1.0"?>
<layout version="0.1.0"><!-- All layout files start with this -->
<cms_index_index><!-- Index directive is the same as "home" page -->
<reference name="root"><!-- For more blocks that can be referenced see "default" directive -->
<block type="core/text_list" name="home_flash">
<block type="cms/block" name="home-page-flash">
<action method="setBlockId"><block_id>home-page-flash</block_id></action>
</block>
</block>
</reference>
</cms_index_index>
<!-- More directives might go here -->
</layout>
Other useful block types worth knowing are core/text and core/template which correspond to Mage_Core_Block_Text and Mage_Core_Block_Template respectively. They get used the most.
Your home made block type of page/html_home_block didn't have any PHP class with a matching name, and if you were genuinely creating your own you wouldn't be able to use the page prefix since Magento already does.
To create a block you only need a <block> tag in the layout file.
To create a block type you need to write a PHP class, give it a namespace and declare it as part of a module.
To add to an existing block is the time when you use a <reference> tag.
There are many fine articles at Magento Knowledge Base including some on Theming & Design.