Magento custom module grid not displaying - magento

So I'm trying to get a grid to display in my custom module (displaying anything for the time being, I'll worry about the collection once it's working!).
The problem is that the _prepareCollection() and/or _prepareColumns() methods of my grid widget class never seem to get called and the grid never shows (nor do the buttons and header text). (The Magento admin header and footer and navigation display correctly. It's just blank in the middle!)
This is what I have so far:
app/code/local/MyNamespace/Mymodule/etc/config.xml
<?xml version="1.0" ?>
<config>
<modules>
<MyNamespace_Mymodule>
<version>0.0.1</version>
</MyNamespace_Mymodule>
</modules>
<!-- Define frontend and backend routers -->
<admin>
<routers>
<mymodule>
<use>admin</use>
<args>
<module>MyNamespace_Mymodule</module>
<frontName>mymodule</frontName>
</args>
</mymodule>
</routers>
</admin>
<!-- /Define frontend and backend routers -->
<global>
<helpers>
<mymodule>
<class>MyNamespace_Mymodule_Helper</class>
</mymodule>
</helpers>
<blocks>
<mymodule>
<class>MyNamespace_Mymodule_Block</class>
</mymodule>
</blocks>
</global>
<adminhtml>
<menu>
<mymodule module="mymodule">
<title>My Module</title>
<sort_order>80</sort_order>
<children>
<items module="mymodule">
<title>Manage My Module</title>
<sort_order>0</sort_order>
<action>mymodule/adminhtml_mymodule</action>
</items>
</children>
</mymodule>
</menu>
<!-- define layout updates -->
<layout>
<updates>
<mymodule>
<file>mymodule.xml</file>
</mymodule>
</updates>
</layout>
<!-- /define layout updates -->
</adminhtml>
</config>
Then my controller:
app/code/local/MyNamespace/Mymodule/controllers/Adminhtml/MymoduleController.php
<?php
class MyNamespace_Mymodule_Adminhtml_MymoduleController extends Mage_Adminhtml_Controller_action
{
public function indexAction() {
$this->getLayout()->createBlock('mymodule/adminhtml_mymodule');
$this->loadLayout();
$this->renderLayout();
}
}
Then in my grid container:
app/code/local/MyNamespace/Mymodule/Block/Adminhtml/Mymodule.php
<?php
class MyNamespace_Mymodule_Block_Adminhtml_Mymodule extends Mage_Adminhtml_Block_Widget_Grid_Container
{
public function __construct()
{
echo __METHOD__ . " (Line #" . __LINE__ . ")<br/>";
parent::__construct();
$this->_controller = 'adminhtml_mymodule';
$this->_blockGroup = 'mymodule';
$this->_headerText = Mage::helper('mymodule')->__('my header text'); // this is not rendered
$this->_addButtonLabel = Mage::helper('mymodule')->__('my button text'); // this is not rendered
}
protected function _prepareLayout()
{
$this->setChild( 'grid',
$this->getLayout()->createBlock( $this->_blockGroup.'/' . $this->_controller . '_grid',
$this->_controller . '.grid')->setSaveParametersInSession(true) );
return parent::_prepareLayout();
}
}
Then in my grid widget:
app/code/local/MyNamespace/Mymodule/Block/Adminhtml/Mymodule/Grid.php
<?php
class MyNamespace_Mymodule_Block_Adminhtml_Mymodule_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct()
{
parent::__construct();
$this->setId('mymoduleGrid');
$this->setDefaultSort('id');
$this->setDefaultDir('ASC');
$this->setSaveParametersInSession(true);
}
protected function _prepareCollection()
{
echo __METHOD__ . " (Line #" . __LINE__ . ")<br/>"; // This is never called
$collection = Mage::getModel('catalog/product')->getCollection(); // just a temp collection for the time being
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
echo __METHOD__ . " (Line #" . __LINE__ . ")<br/>"; // This is never called
$this->addColumn('id', array(
'header' => Mage::helper('mymodule')->__('ID'),
'align' =>'right',
'width' => '10px',
'index' => 'id',
));
return parent::_prepareColumns();
}
}
And finally my layout xml:
app/design/adminhtml/default/default/layout/mymodule.xml
<?xml version="1.0"?>
<layout version="0.1.0">
<adminhtml_mymodule_index>
<reference name="content">
<block type="mymodule/adminhtml_mymodule" name="mymodule" />
</reference>
</adminhtml_mymodule_index>
</layout>
There are no errors being shown in the logs and I'm now a bit stumpped and other SO answers don't seem to fit.
Anyone shed any light on why my grid (even an empty one) isn't showing?
Thank you.
EDIT Noticed that some classes had the wrong case (Mynamespace should be MyNamespace). Changed them but no difference

It is the problem of your layout's handle tag.
It should be:
<?xml version="1.0"?>
<layout version="0.1.0">
<mymodule_adminhtml_mymodule_index>
<reference name="content">
<block type="mymodule/adminhtml_mymodule" name="mymodule" />
</reference>
</mymodule_adminhtml_mymodule_index>
</layout>
For the explanation and few tips, you can read this:
My layout isn't loading in my Magento admin view
= UPDATE =
You also need not to call $this->getLayout()->createBlock('mymodule/adminhtml_mymodule'); in your controller as is has been called in your mymodule.xml
OR
you can omit your mymodule.xml (no need to call it) by changing your controller's action into:
public function indexAction() {
$this->loadLayout();
$myblock = $this->getLayout()->createBlock('mymodule/adminhtml_mymodule');
$this->_addContent($myblock);
$this->renderLayout();
}
see the definition:
Mage_Adminhtml_Controller_Action
protected function _addContent(Mage_Core_Block_Abstract $block)
{
$this->getLayout()->getBlock('content')->append($block);
return $this;
}
those codes above do the same thing like the mymodule.xml, appending block 'mymodule/adminhtml_mymodule' to the content
It's all your choice!

Can you please make sure your index action is being called properly?
when you do this :
<?php
class Mynamespace_Mymodule_Adminhtml_MymoduleController extends Mage_Adminhtml_Controller_action
{
public function indexAction() {
echo "im here";exit; //<----does this display?
$this->getLayout()->createBlock('mymodule/adminhtml_mymodule');
$this->loadLayout();
$this->renderLayout();
}
}
and then finally its important to load your layout first. so in my opnion change your index action to this :
<?php
class Mynamespace_Mymodule_Adminhtml_MymoduleController extends Mage_Adminhtml_Controller_action
{
public function indexAction() {
$this->loadLayout(); // <---- This first
$this->getLayout()->createBlock('mymodule/adminhtml_mymodule');// <---- then this
$this->renderLayout();
}
}

I know this isn't strictly related to the answer, but hopefully this might help someone.
Just in case anyone is reading this, completely bereft of hope - make sure there's a layout file in app/design/adminhtml/default/default/layout.
I had no problems with my custom grid locally, but when I migrated it, it was displaying the symptoms above (blank screen etc)
It was due to me not copying that file over.

Related

How to change order of tabs in adminhtml catalog product edit in Magento

I create tab Opinion in adminhtml catalog product page.
I want that my tab 'Opinion' was first in list of tabs.
app/code/local/Fishpig/Customtabs/Block/Adminhtml/Catalog/Product/Tab.php
class Fishpig_Customtabs_Block_Adminhtml_Catalog_Product_Tab
extends Mage_Adminhtml_Block_Template
implements Mage_Adminhtml_Block_Widget_Tab_Interface {
public function _construct()
{
parent::_construct();
$this->setTemplate('customtabs/catalog/product/tab.phtml');
}
public function getTabLabel()
{
return $this->__('Opinion');
}
public function getTabTitle()
{
return $this->__('Click here to view your custom tab content');
}
public function canShowTab()
{
return true;
}
public function isHidden()
{
return false;
}
app/design/adminhtml/default/default/layout/customtabs.xml
*
<layout>
<adminhtml_catalog_product_edit>
<reference name="product_tabs">
<action method="addTab">
<name>opinion</name>
<block>customtabs/adminhtml_catalog_product_tab</block>
</action>enter code here
</reference>
</adminhtml_catalog_product_edit>
</layout>
*
If you want your tab at the first in Admin Catalog Product Add Or Edit page
then you need to do these Steps
1: Override this File
<global>
<blocks>
<adminhtml>
<rewrite>
<catalog_product_edit_tabs>Namespace_Modulename_Block_Adminhtml_Catalog_Product_Edit_Tabs</catalog_product_edit_tabs>
</rewrite>
</adminhtml>
</blocks>
</global>
Step 2 : in the file
there is one function named : _prepareLayout
override that in your module file
and add the tab after this code
$product = $this->getProduct();
if (!($setId = $product->getAttributeSetId())) {
$setId = $this->getRequest()->getParam('set', null);
}
add this code
$this->addTab('opinion', array(
'label' => Mage::helper('core')->__('Opinion'),
'url' => $this->getUrl('module/controller/opinion', array('_current' => true)),
'class' => 'ajax',
));
Step 3 : in controller create one function opinion and set your template file there with $this->setTemplate('customtabs/catalog/product/tab.phtml');
let me know if you need more help
Try using the addTabAfter method instead
<action method="addTabAfter">
<name>opinion</name>
<block>customtabs/adminhtml_catalog_product_tab</block>
<after></after>
</action>
To determine after which tab you want add you cutom tab you need in your file
app/code/local/Fishpig/Customtabs/Block/Adminhtml/Catalog/Product/Tab.php
add such method:
public function getAfter()
{
return 'group_32'; // Name of tab after which you want add your tab
}
group_32 it's name of General tab in my case.

Magento Custom Router Lost Layout Object

It seems like everything is being properly configured and I can get the output that I want but I would prefer not echoing the layout object directly from my controller:
Here is what Im working with
config.xml
<config>
<modules>
<ALS_Bestselling>
<version>0.1.0</version>
</ALS_Bestselling>
</modules>
<global>
<models>
<bestselling>
<class>ALS_Bestselling_Model</class>
</bestselling>
</models>
<blocks>
<bestselling>
<class>ALS_Bestselling_Block</class>
</bestselling>
</blocks>
<helpers>
<bestselling>
<class>ALS_Bestselling_Helper</class>
</bestselling>
</helpers>
</global>
<frontend>
<layout>
<updates>
<als_bestselling>
<file>bestselling.xml</file>
</als_bestselling>
</updates>
</layout>
</frontend>
<default>
<web>
<routers>
<bestselling_router>
<area>frontend</area>
<class>ALS_Bestselling_Controller_Router</class>
</bestselling_router>
</routers>
</web>
<shorturls>
</shorturls>
</default>
</config>
and
#File: Controller/Router.php
<?php
class ALS_Bestselling_Controller_Router extends Mage_Core_Controller_Varien_Router_Abstract
{
private static $_module = 'bestsellers';
private static $_realModule = 'ALS_Bestselling';
private static $_controller = 'index';
private static $_controllerClass = 'ALS_Bestselling_Controller_Index';
private static $_action = 'view';
public function initControllerRouters($observer)
{
$front = $observer->getEvent()->getFront();
$front->addRouter('bestselling', $this);
}
public function collectRoutes()
{
// nothing to do here
}
public function match(Zend_Controller_Request_Http $request)
{
$this->_request = $request;
$front = $this->getFront();
$identifier = trim($request->getPathInfo(), '/');
if(!substr($identifier,0,strlen('bestsellers')) == 'bestsellers'){
return false;
}else{
//$rewrite = Mage::getModel('core/url_rewrite');
$route_params = str_replace ( "bestsellers/" , "" , $identifier );
$rewrite = Mage::getModel('core/url_rewrite');
$rewrite->setStoreId(1);
$rewrite->loadByRequestPath($route_params);
$category_route = $rewrite->getIdPath();
//If no route exists for category send to a different router
if(!$category_route != ""){
return false;
}//Otherwise send the parameters to the request
else{
$id = str_replace ( "category/" , "" , $category_route );
$this->_request->setParam('id',$id);
}
$this->_setRequestRoute();
$this->_dispatch();
return true;
}
}
protected function _setRequestRoute()
{
$this->_request->setModuleName(self::$_module);
$this->_request->setControllerName(self::$_controller);
$this->_request->setActionName(self::$_action);
$this->_request->setControllerModule(self::$_realModule);
}
protected function _dispatch()
{
$this->_request->setDispatched(true);
$controller = Mage::getControllerInstance(self::$_controllerClass, $this->_request, $this->_response);
$controller->dispatch(self::$_action);
}
}
and
File: Controller/Index.php
class ALS_Bestselling_Controller_Index extends Mage_Core_Controller_Front_Action{
public function viewAction(){
$layout = Mage::app()->getLayout();
$layout->generateXml()->generateBlocks();
$render = $layout->getBlock('root')->toHtml();
echo $render;
}
}
The previous works but the following:
$update = $this->getLayout()->getUpdate();
$update->addHandle('default');
$this->renderLayout();
throws an error Call to a member function appendBody() on a non-object.
Is this how I am suppose to do this or is there something missing from the recipe?
Custom routing classes are on the fringe of what most developers do with Magento — the standard approach would be to setup a standard module controller with a frontname of bestselling, or create your feature in a non-seo friendly way and then create a rewrite entity/object to SEO-ify it.
I, however, have a soft spot for custom routing objects, even if there's little in the way of best community practices behind them. Without a line number, it sounds like your exception
Call to a member function appendBody() on a non-object
comes from the following code
#File: app/code/core/Mage/Core/Controller/Varien/Action.php
$this->getResponse()->appendBody($output);
Given the definition for getResponse
public function getResponse()
{
return $this->_response;
}
It sounds like your controller object doesn't have a proper response object set.
Looking at your controller instantiation code
$controller = Mage::getControllerInstance(self::$_controllerClass, $this->_request, $this->_response);
You're referencing a $this->_response property — but your router class and the abstract router class don't have this property. It's impossible to say based on what you've posted, but this is probably your problem. Take a look at how the standard router's match method does it.
#File: app/code/core/Mage/Core/Controller/Varien/Router/Standard.php
//...
$controllerInstance = Mage::getControllerInstance($controllerClassName, $request, $front->getResponse());
So, use the front-controller object to grab the response object, pass that into the getCongrollerInstance factory method, and you should be good to go (or at least onto the next problem)

Add button to catalog category in magento

I want to add new button to category page on admin side with delete category and save category.I try the way to override the block Catalog_Category_Edit_Form but didn't work.My xml code is :
<adminhtml>
<rewrite>
<Catalog_Category_Edit_Form>Mymodule_Block_Rewrite_Editcate</Catalog_Category_Edit_Form>
</rewrite>
</adminhtml>
While my block code is
<?php
class Mymodule_Block_Rewrite_Editcate extends Mage_Adminhtml_Block_Catalog_Category_Edit_Form
{
private $parent;
protected function _prepareLayout()
{
// Delete button
$this->parent = parent::_prepareLayout();
$this->removeButton('delete_button');
return $this->parent;
}
}.
Can anyone help me the right way ? In above code i try to remove button to check my code works .
Sorry guys i find a solution .My new xml is look like
<adminhtml>
<rewrite>
<catalog_category_edit_form>Mymodule_Block_Adminhtml_Catalog_Category_Edit_Form</catalog_category_edit_form>
</rewrite>
</adminhtml>
And my block code is :
<?php
class Mymodule_Block_Adminhtml_Catalog_Category_Edit_Form extends Mage_Adminhtml_Block_Catalog_Category_Edit_Form
{
protected function _prepareLayout()
{
parent::_prepareLayout();
$this->addAdditionalButton('update_button', array('name' => 'update_button','title'=>'Copy Category','type'=>"button",'label'=> Mage::helper('catalog')->__('Copy Category') ));
return parent::_prepareLayout();
}
}.
This is it.Mian function is addAdditionButton.Only pass parameter to it.I have override it.

Magento block output gives empty string

I am trying to troubleshoot an issue where the output of my block is an empty string.
I traced it down to the point where I could see PHP statements being evaluated in the template file, but inside toHtml() of class Mage_Core_Block_Abstract, $html = $this->_toHtml(); assigns empty string to $html.
I dig it further, and found that inside fetchView(), $html = ob_get_clean(); assigns empty string to it, even when the template was included above this line, and I could see it evaluating with the use of debugger.
From here I am clueless on how to debug this, may be I am missing something wrong in my module (I am a beginner in Magento).
Here is the concerned code from the module:
app/code/local/AnattaDesign/AbandonedCarts/etc/config.xml
<config>
<global>
<blocks>
<anattadesign_abandonedcarts>
<class>AnattaDesign_AbandonedCarts_Block</class>
</anattadesign_abandonedcarts>
</blocks>
</global>
<adminhtml>
<layout>
<updates>
<anattadesign_abandonedcarts>
<file>layout.xml</file>
</anattadesign_abandonedcarts>
</updates>
</layout>
</adminhtml>
app/design/adminhtml/base/default/layout/layout.xml
<?xml version="1.0" encoding="utf-8"?>
<layout>
<adminhtml_dashboard_index>
<reference name="head">
<action method="addCss">
<stylesheet>anattadesign/abandonedcarts/css/style.css</stylesheet>
</action>
<action method="addJs">
<script>anattadesign/abandonedcarts/zepto.js</script>
</action>
<action method="addJs">
<script>anattadesign/abandonedcarts/adminhack.js</script>
</action>
</reference>
</adminhtml_dashboard_index>
</layout>
app/code/local/AnattaDesign/AbandonedCarts/controllers/WidgetController.php
<?php
class AnattaDesign_AbandonedCarts_WidgetController extends Mage_Adminhtml_Controller_Action {
public function indexAction() {
echo "index action of widget controller";
die();
}
public function renderAction() {
$html = $this->getLayout()->createBlock( 'anattadesign_abandonedcarts/widget', 'root' )->setTemplate( 'anattadesign/abandonedcarts/widget.phtml' )->toHtml();
$this->getResponse()->setBody( $html );
die();
}
}
app/code/local/AnattaDesign/AbandonedCarts/Block/Widget.php
<?php
class AnattaDesign_AbandonedCarts_Block_Widget extends Mage_Core_Block_Template {
}
I am running this by an admin controller and making the renderAction() fire.
Remove the die() in method renderAction()
class AnattaDesign_AbandonedCarts_WidgetController extends Mage_Adminhtml_Controller_Action {
....
public function renderAction() {
$html = $this->getLayout()->createBlock( 'anattadesign_abandonedcarts/widget')
->setTemplate( 'coming.phtml' )
->toHtml();
$this->getResponse()->setBody( $html );
}
}

Magento Sales > Orders > Add print action

I'm trying to create a print action on the magento orders page(printscreen: sbx.mujjo.com/media/images/action.png). Right now 'Print Labels' is a url to a html page. I'm trying to get it to open a .pdf instead (just like 'Print invoice'. But I can't find the right code.
The code that create the url:
class AquiveMedia_Orderlabel_Model_Observer {
public function add_action($observer) {
$block = $observer->getEvent()->getBlock();
if ($block instanceof Mage_Adminhtml_Block_Widget_Grid_Massaction) {
if ($block->getParentBlock() instanceof Mage_Adminhtml_Block_Sales_Order_Grid) {
$block->addItem('print_labels', array(
'label' => Mage::helper('sales')->__('Print Labels'),
'url' => $block->getUrl('orderlabel/adminhtml_orderlabel/massprint')
)
);
}
}
}
}
Since the link already exists just use the URL for yourself. Here is how to override.
In your module's config.xml file,
<config>
...
<admin>
<routers>
<adminhtml>
<args>
<modules>
<aquivemedia_orderlabel before="Mage_Adminhtml">
AquiveMedia_Orderlabel_Adminhtml
</aquivemedia_orderlabel>
</modules>
</args>
</adminhtml>
</routers>
</admin>
</config>
Now you can make a controller and it will be called first.
app/code/local/AquiveMedia/Orderlabel/controllers/Adminhtml/Sales/Order/InvoiceController.php:
class AquiveMedia_Orderlabel_Adminhtml_Sales_Order_InvoiceController
extends Mage_Adminhtml_Controller_Action
{
public function printAction()
{
// this is called instead of the path "index.php/admin/sales_order_invoice/print"
}
}

Resources