Magento Adding a Block to an index Action Page - magento

I am creating a module that will have its own page at mysite.com/memymodule/index/index
I want to add functionality from a template file from at templates/me/template.phtml
I have an index controller like this:
<?php
class me_mymodule_IndexController extends Mage_Core_Controller_Front_Action {
public function indexAction() {
$this->loadLayout();
$this->renderLayout();
}
}
?>
I have a layout update mymodule.xml that looks like this:
<?xml version="1.0"?>
<layout version="0.1.0">
<mymodule_index_index>
<reference name="content">
<block type="core/template" name="me.mymodule" template="me/template.phtml" />
</reference>
</mymodule_index_index>
</layout>
The frontName of the module is mymodule
When the page renders the content block is completely empty and the content of template.phtml is completely ignored.
Help much appreciated :-)

Try this Code within your Controller's Action -
var_dump(Mage::getSingleton('core/layout')->getUpdate()->getHandles());
exit("Your Layout Path is ".__LINE__." in ".__FILE__);
This code tells you about the Tag which you need to create within Layout.xml.
Also check Config.xml that the Layout Update Section is correctly defined or not.
Hope it'll be beneficial for you.
THANKS

Related

what is the correct way to add block as a child of an another block in xml in magento?

I have two custom magento blocks called "exe2" and "example".
The exe2 block gets the contents of the example block using the getChildHtml function, but it keeps just returning empty strings to me and only the exe2 contents make it to the screen.
Here is my code my code:
exe2 xml file:
<layout version = "0.1.0">
<test_example_view>
<block type = "exemplum_mod2/exe2" name = "exemplum.mod2.exe2" template="exemplum/mod2/exe2.phtml">
<reference name = "exemplum.mod2.exe2">
<block type="exemplum/example" name="exemplum.example" template="exemplum/example1/example.phtml" />
</reference>
</block>
</test_example_view>
example xml file
<layout version="0.1.0">
<default>
<block type="exemplum/example" name="exemplum.example" template="exemplum/example1/example.phtml" />
</default>
Here is the exe2 phtml file that has the getChildHtml call in it:
<h1> 2nd </h1>
<?php
echo $this->getMessage();
echo $this->getChildHtml("exemplum.mod2.exe2");
?>
hello
example.phtml file:
<h1>Hello there</h1>
And finally heres the controller file that loads the blocks:
<?php
class exemplum_example1_ExampleController extends Mage_Core_Controller_Front_Action{
public function viewAction(){
$this->loadLayout();
$block = $this->getLayout()->createBlock('exemplum_mod2/exe2');
$block->setTemplate("exemplum/mod2/exe2.phtml");
$this->getLayout()->getBlock('content')->append($block);
$this->renderLayout();
}
}
What is the correct way to add one block as the child block of a another block in the xml file? Every solution that i found on google didn't seem to work in my case so what am i doing wrong here?

Magento how to remove a class from Layout

I have add a body class to default layout (local.xml) and I want to remove it in some specific area. Tried to use removeBodyClass but Magento 1.7.0.2 shows me an error "Invalid method Mage_Page_Block_Html::removeBodyClass(Array..."
How to solve that?
addBodyClass is working, removeBodyClass cause problems. Please give me a solution. Thanks in Advance
<layout>
<default>
<reference name="root">
<action method="addBodyClass"><classname>halloweenClass</classname></action>
</reference>
</default>
</layout>
There are no class exits on removeBodyClass in magento CE 1.7.
and if want to do this then rewrite Mage_Page_Block_Html class then add function removeBodyClass
public function removeBodyClass($className)
{
$className = preg_replace('#[^a-z0-9]+#', '-', strtolower($className));
$this->setBodyClass(str_replace($className,' ',$this->getBodyClass()). ' ' . $className);
return $this;
}

Set template for block if there are blocks with the same name

I'm trying to change template for 'form' block in such layout:
<adminhtml_sales_order_create_index>
...
<reference name="root">
<block type="adminhtml/sales_order_create" name="content">
<block type="adminhtml/sales_order_create_form" template="sales/order/create/form.phtml" name="form">
...
<block type="adminhtml/sales_order_create_shipping_method" template="sales/order/create/abstract.phtml" name="shipping_method">
<block type="adminhtml/sales_order_create_shipping_method_form" template="sales/order/create/shipping/method/form.phtml" name="form" />
</block>
<block type="adminhtml/sales_order_create_billing_method" template="sales/order/create/abstract.phtml" name="billing_method">
<block type="adminhtml/sales_order_create_billing_method_form" template="sales/order/create/billing/method/form.phtml" name="form" />
</block>
<block type="adminhtml/sales_order_create_newsletter" template="sales/order/create/abstract.phtml" name="newsletter">
<block type="adminhtml/sales_order_create_newsletter_form" template="sales/order/create/newsletter/form.phtml" name="form" />
</block>
...
I do
<adminhtml_sales_order_create_index>
<reference name="form">
<action method="setTemplate">
<template>my_module/sales/order/create/form.phtml</template>
</action>
</reference>
</adminhtml_sales_order_create_index>
But this does not work. I think because block with name form exist in several places more after. I just want to change it in block type="adminhtml/sales_order_create_form". How can I do this?
#input
Rewrite of block is not the best appoach. It is better to observe 'core_block_abstract_to_html_before' event and change template there I think. Like:
if ($observer->getBlock() instanceof Mage_Adminhtml_Block_Sales_Order_Create_Form) {
$observer->getBlock()->setTemplate('my_module/newtemplate.phtml');
}
This works and is better because you will not get modules conflict if somebody else will rewrite this block. But I thought that may be possible on layout level..
Whilst not the prettiest solution I too do not know of a way of setting a template on a non unique form reference.
Warning this is pretty gross, and I am really not sure of the side affects and for some reason calling setTemplate in the constructor is not enough I guess this is being called at a earlier or later stage than the parents
class My_Module_Block_Adminhtml_Sales_Order_Create_Form extends Mage_Adminhtml_Block_Sales_Order_Create_Form
{
public function _toHtml()
{
$this->setTemplate('my_module/sales/order/create/form.phtml');
return parent::_toHtml();
}
}
Add the write to your Module
<blocks>
<adminhtml>
<rewrite>
<sales_order_create_form>My_Module_Block_Adminhtml_Sales_Order_Create_Form</sales_order_create_form>
</rewrite>
</adminhtml>
</blocks>
I really hope there is a better solution than this.
your right, thats the reason why its not working.
i can't think of a clean way because of this bad naming.
However there are several options that are not clean but will work:
1 option: copy the whole sales.xml into your own admin layout folder and change the template there directly.
2 option: "rewriting" "Mage_Adminhtml_Block_Sales_Order_Create_Form" and set the template from the code
3 option: subscribe to "adminhtml_block_html_before" and listen for the right block and change the template then.
However if you write a community module non of these options are really good.
if it is your own project i would go for option 1
hope that helps
For overwriting the form name form.phtml file from the billing_method you should do the following:
In the config.xml:
<core_block_abstract_to_html_before>
<observers>
<mymodule_core_block_abstract_to_html_before>
<class>MyModule_Model_MyPath</class>
<method>setTemplateFile</method>
</mymodule_core_block_abstract_to_html_before>
</observers>
</core_block_abstract_to_html_before>
and in your observer:
public function setTemplateFile(Varien_Event_Observer $observer)
{
if ($observer->getBlock() instanceof Mage_Adminhtml_Block_Sales_Order_Create_Billing_Method_Form) {
$observer->getBlock()->setTemplate('mymodule/sales/order/create/billing/method/form.phtml');
}
}

getData doesn't work in Mage_Core_Block_Template custom block

I defined a custom block this way:
<frontend>
<layout>
<updates>
<categorylist module="mymodule">
<file>mymodule.xml</file>
</categorylist>
</updates>
</layout>
</frontend>
<global>
<blocks>
<categorylist>
<class>Alias_CategoryList_Block</class>
</categorylist>
</blocks>
</global>
Then I defined the block class this way
class Alias_CategoryList_Block_List extends Mage_Core_Block_Template
{
public $idCategory = NULL;
// Contructor
public function __construct()
{
echo $this->getData('categoryid');
}
}
and the layout this way:
<default translate="label">
<block type="categorylist/list" name="categorylist.list" output="toHtml" after="-" template="mymodule.phtml"/>
I put the block in a CMS this way:
{{block type="categorylist/list" categoryid="10"}}
But sadly the $this->getData('categoryid'); retrieves nothing.
Cannot figure out what's wrong ?
I tryed even $this->getCategoryid; but even nothing.
Anyone can help?
I'm using Magento 1.7
Would it be silly to add a view (phtml) template file that has
<?php echo $this->getCategoryId(); ?>
instead of trying to accomplish that in the constructor? Also then you wouldn't need your own code behind file you could just use core/template.
So your cms would be
{{block type="core/template" template="awesome.phtml" cateogryid="10"}}
The problem was that I assumed that the layout updates configuration will extend the code in the CMS but the code in the CMS should not have block name and template parameters for my purposes. So I fixed it declearing the template in the contructor and removing the layout updates in configuration (since I don't need the block to override existing blocks):
// Contructor
public function __construct()
{
$this->setTemplate('mymodule.xml');
}

Magento custom module admin block display

struggling getting access to an admin block ive created.
Ive created a module...it has many elements, all working. Ive got header includes added to certain admin pages no problem, using my adminhtml layout update xml file.
The issue seems to be it cant access/see my block...so muct be referencing wrong, even though ive been following the 'module creator' extension files.
Another silly issue i think, been at this too long! :)
First the code:
Mworkz/MyModuleName/Block/Adminhtml/MyBlock.php
class Mworkz_MyModuleName_Block_Adminhtml_MyModuleName extends Mage_Adminhtml_Block_Widget_Grid_Container
{
public function __construct()
{
var_dump('WE ARE IN THE ADMIN BLOCK!');exit;
$this->_controller = 'adminhtml_mymodulename';
$this->_blockGroup = 'mymodulename';
$this->_headerText = Mage::helper('mymodulename')->__('Item Manager');
$this->_addButtonLabel = Mage::helper('mymodulename')->__('Add Item');
parent::__construct();
}
}
My layout xml (this file works, and is referenced right, as my admin header includes work)
Should point out i have a custom tab and controller...all working.
<?xml version="1.0"?>
<layout version="0.1.0">
<mymodulename_adminhtml_mymodulename_index>
<reference name="head">
<action method="addJs"><script>Mworkz/MyModuleName.js</script></action>
</reference>
<reference name="content">
<block type="mymodulename/adminhtml_mymodulename" name="mymodulename" ></block>
</reference>
</mymodulename_adminhtml_mymodulename_index>
</layout>
I expect to see the var_dump stmt ive inserted....but it doesnt display.
Thanks in advance...
file naming! Simple caps issue...
My block file was called '...Adminhtml/MyModuleName.php',
My block identifier inside the file was '...Adminhtml_Mymodulename {'
Another set of working code snippets for adminhtml block users i suppose!
Thanks

Resources