Add Layout to Custom Module’s Admin Area in Magento 1.9

adminadminhtmlmagento-1.9module

I have created a custom module in Magento.
The idea of the module is to provide the ability to report a product.

I have configured the front end and that is all working fine.
The basic idea is that you click on a link on a product page which then reports the product (it adds a row to a report table, containing the following columns: report_id, product_id & status).

I'm now onto creating the back end for the module.
For this, I have added a menu item under "Catalog > Reported Products".

When I click on the link, it goes to a blank page (it has the admin header and footer, so my controller is set up correctly).

What I'm stuck on is adding a block to manage the admin side of things.
For the front end, I have a block "Tbe/Report/Block/Report.php" and, it works perfectly. I just can't seem to do the same for the admin side of things.

I would then like to be able to actually add some kind of list to the admin area to display the products that have been reported.

Tutorials on this subject seem to be very hard to come by and I really have no idea of the best practices when it comes to creating a layout for the admin section.

Here's what I have so far:

**app/code/core/local/Tbe/Report/Block/Adminhtml/Report.php

class Tbe_Report_Block_Adminhtml_Report extends Mage_Adminhtml_Block_Template {

    public function __construct(){
        echo "BLOCK HAS BEEN INCLUDED!";
        die();
        parent::_construct();
    }

}

app/code/core/local/Tbe/Report/controllers/Adminhtml/ReportedController.php

class Tbe_Report_Adminhtml_ReportedController extends Mage_Adminhtml_Controller_Action {

    public function indexAction() {
        $this->loadLayout();
        $this->renderLayout();
    }

}

app/code/core/local/Tbe/Report/etc/config.xml

<config>

    ...

    <global>

        ...

        <blocks>
            <report>
                <class>Tbe_Report_Block</class>
            </report>
        </blocks>

        ...

    </global>

    ...

    <admin>

        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <Tbe_Report after="Mage_Adminhtml">Tbe_Report_Adminhtml</Tbe_Report>
                    </modules>
                </args>
            </adminhtml>
        </routers>

    </admin>


</config>

Like I said, tutorials on this appear to be in short, so if you know of any tutorials which could help, please let me know.

Cheers

UPDATE

I have changed config.xml to look like this:

<config>

    ...

    <adminhtml>
        <layout>
            <updates>
                <report>
                    <file>tbe_report.xml</file>
                </report>
            </updates>
        </layout>
    </adminhtml>


    <admin> 

        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <Tbe_Report after="Mage_Adminhtml">Tbe_Report_Adminhtml</Tbe_Report>
                    </modules>
                </args>
            </adminhtml>
        </routers>

    </admin>


</config>

I have also add the file "app/design/adminhtml/default/default/layout/tbe_report.xml"

<layout>
    <tbe_report_adminhtml> <!-- I have also tried "tbe_report_adminhtml_index" and "tbe_report_index" -->
        <reference name="head">
            <action method="addCss"><stylesheet>report/css/report1.css</stylesheet></action>
        </reference>
    </tbe_report_adminhtml>
</layout>

Am I right in thinking I should have an error in the console "File not found" for the stylesheet report/css/report1.css. I do not have any sort of error and neither is it in resources.

Best Answer

you need to add this in your config.xml under the config tag.

<adminhtml>
    <layout>
        <updates>
            <tbe_report>
                <file>tbe_report.xml</file>
            </tbe_report>
        </updates>
    </layout>
</adminhtml>

then create the file app/design/adminhtml/default/default/layout/tbe_report.xml and put your layout updates in there.

[EDIT]
your layout handle is determined from the url of the page.
Just replace admin with adminhtml and replace the slashes with underscores.
If the action is index and is not present in the url just add it in the layout handle.
Here are some examples to make it clearer.
If the url is admin/tbe_report/something then the layout handle is adminhtml_tb_report_something.
if the url is admin/tbe_report/ then the layout handle is adminhtml_tb_report_index.

Related Topic