Magento 1.9 Blocks – Fix Can’t Load Block via Controller

blocksformsmagento-1.9

here is all my file

app\code\local\Sample\Bigcom\etc\config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Sample_Bigcom>
            <version>1.0.0</version>
        </Sample_Bigcom>
    </modules>
    <global>
    <blocks>
            <sample_bigcom>
                <class>Sample_Bigcom_Block</class>
            </sample_bigcom>
        </blocks>
        <helpers>
    <sample_bigcom>
      <class>Sample_Bigcom_Helper</class>
    </sample_bigcom>
  </helpers>
        <helpers>
            <sample_bigcom>
                <!-- Helper definition needed by Magento -->
                <class>Mage_Core_Helper</class>
            </sample_bigcom>
        </helpers>
    </global>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <Sample_Bigcom before="Mage_Adminhtml">Sample_Bigcom_Adminhtml</Sample_Bigcom>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
    <layout>
    <updates>
        <sample_bigcom>
            <file>sample_bigcom.xml</file>
        </sample_bigcom>
    </updates>
</layout>
</config>

app\code\local\Sample\Bigcom\etc\adminhtml.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <menu>
        <mycustomtab module="sample_bigcom" translate="title">
            <title>My Custom Tab</title>
            <sort_order>100</sort_order>
            <children>
                <index module="sample_bigcom" translate="title">
                    <title>Index Action</title>
                    <sort_order>10</sort_order>
                    <action>adminhtml/bigcom</action>
                </index>
            </children>
        </mycustomtab>
    </menu>
    <acl>
        <resources>
            <all>
                <title>Allow Everything</title>
            </all>
            <admin>
                <children>
                    <sample>
                        <title>My Custom Tab</title>
                        <sort_order>100</sort_order>
                        <children>
                            <index>
                                <title>Index Action</title>
                                <sort_order>10</sort_order>
                            </index>
                        </children>
                    </sample>
                </children>
            </admin>
        </resources>
    </acl>
</config>

app\code\local\Sample\Bigcom\Helper\Data.php

<?php
/**
 * Adminhtml base helper
 *
 * @category   Mageworks
 * @package    Mageworks_Import
 * @author     mageworks kumar <mageworksnsscoe@gmail.com>
 */
class Sample_Bigcom_Helper_Data extends Mage_Core_Helper_Abstract
{
}

app\code\local\Sample\Bigcom\Block\Adminhtml\Form\Edit.php

<?php

class Sample_Bigcom_Block_Adminhtml_Form_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{

    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct();

        $this->_objectId = 'edit_form';
        $this->_blockGroup = 'sample_bigcom';
        $this->_controller = 'adminhtml_form';

        $this->_updateButton('save', 'label', Mage::helper('sample_bigcom')->__('Save'));
        $this->_updateButton('delete', 'label', Mage::helper('sample_bigcom')->__('Delete'));

    }

    public function getHeaderText()
    {
        return Mage::helper('sample_bigcom')->__('My Form Container');
    }

}

app\code\local\Sample\Bigcom\Block\Adminhtml\Form\Edit\Form.php

<?php

class Sample_Bigcom_Block_Adminhtml_Form_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
    /**
     * Preparing form
     *
     * @return Mage_Adminhtml_Block_Widget_Form
     */
    protected function _prepareForm()
    {
        $form = new Varien_Data_Form(
            array(
                'id'     => 'edit_form',
                'action' => $this->getUrl('*/*/save'),
                'method' => 'post',
            )
        );

        $form->setUseContainer(true);
        $this->setForm($form);
        return parent::_prepareForm();
    }
}

app\code\local\Sample\Bigcom\Block\Adminhtml\Form\Edit\Tabs.php

<?php
class Sample_Bigcom_Block_Adminhtml_Form_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs
{

  public function __construct()
  {
      parent::__construct();
      $this->setId('form_tabs');
      $this->setDestElementId('edit_form'); // this should be same as the form id define above
      $this->setTitle(Mage::helper('sample_bigcom')->__('Product Information'));
  }

  protected function _beforeToHtml()
  {
      $this->addTab('form_section', array(
          'label'     => Mage::helper('sample_bigcom')->__('Item Information'),
          'title'     => Mage::helper('sample_bigcom')->__('Item Information'),
          'content'   => $this->getLayout()->createBlock('sample_bigcom/adminhtml_form_edit_tab_form')->toHtml(),
      ));

      return parent::_beforeToHtml();
  }
}

this is controller file
app\code\local\Sample\Bigcom\controllers\Adminhtml\BigcomController.php

  <?php

class Sample_Bigcom_Adminhtml_BigcomController extends Mage_Adminhtml_Controller_Action
{


    /**
     * View form action
     */
   public function indexAction()
    {
        $this->loadLayout()
            ->_setActiveMenu('mycustomtab')
            ->_title($this->__('Index Action'));
$block11=$this->getLayout()->createBlock('sample_bigcom/adminhtml_form_edit_form');
$this->getResponse()->setBody($block11->toHtml());
$block=$this->getLayout()->createBlock('sample_bigcom/adminhtml_form_edit');
$this->getResponse()->setBody($block->toHtml());
        // my stuff

        $this->renderLayout();
    }
    public function saveAction()
    {
        echo "hiii";
    }


}
app\etc\Sample_Bigcom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <config>
        <modules>
            <Sample_Bigcom>
                <active>true</active>
                <codePool>local</codePool>
            </Sample_Bigcom>
        </modules>
    </config>

this is layout file

app\design\adminhtml\default\default\layout\sample_bigcom.xml

<?xml version="1.0"?>
<layout version="1.0.0">

    <adminhtml_bigcom_index>
        <update handle="editor"/>
        <reference name="content">
            <block type="sample_bigcom/adminhtml_form_edit" name="bigcom"/>
        </reference>
        <reference name="left">
            <block type="sample_bigcom/adminhtml_form_edit_tabs" name="bigcom_tabs"/>
        </reference>
    </adminhtml_bigcom_index>

</layout>

every time when i click Index Action tab i got only Edit.php file output and give error like varienForm is not defined

Best Answer

You're calling

$this->getLayout()->createBlock('sample_bigcom/adminhtml_form_edit_tab_form')->toHtml()

But your block class is

Sample_Bigcom_Block_Adminhtml_Form_Edit_Form

You're missing the tab in your class and make sure that's where the file lives as well so Sample/Bigcom/Block/Adminhtml/Form/Edit/Tab/Form.php.

Related Topic