Magento custom tab of category edit page with custom attribute.How to create new category attribute with new group in magento.
Magento custom tab of category edit page with custom attribute.

In this tutorial I am going to show you how you can manage to create Magento custom tab of category edit page with custom attribute.That means you can create a from where you can set you attributes into that form and when you will save category then that value will saved into database.So for doing all this I will explain in two ways like first is if you want only to manage through category attribute and not want use your custom database table and second one is reverse of first one.For simple description how to create custom tab in category edit page you can follow magento custom tab of category edit page article,then after you can proceed for further.If you have seen the article magento custom tab of category edit page article then below description will be more easier for you.Lets see one by one how we can create Magento custom tabe of category edit page with custom attribute.

First method:

By attribute:

Either you can use below code in your module setup file or you can use it as a custom script to create category attribute in magento. So for creating custom attribute in magento you can follow how to create attribute by custom script.I have defined the group name in group section as ‘group’ => ‘My Custom Tab’ which defines the attribute will be placed under this group.If ther is no group present then magento will create automatically and assign the newly created attribute into this group.
Dir: app/code/[CodePool]/[NameSpace]/[ModuleName]/data/[modulename_setup]/mysql4-install-0.1.0.php
e.g: app/code/local/JRB/Customtab/data/customtab_setup/mysql4-install-0.1.0.php

< ?php
/**
* create category custom attribute
* category JRB
* package JRB_Customtab
* author Jyotiranjan Biswal <biswal@jyotiranjan.in>
*/

$installer = $this;

$installer->startSetup();

$installer->addAttribute(
        'catalog_category',
        'custom_tab_text',
        array(
            'group'             => 'My Custom Tab',
            'backend'           => '',
            'frontend'          => '',
            'class'             => '',
            'default'           => '',
            'label'             => 'Custom tab text',
            'input'             => 'text',
            'type'              => 'varchar',
            'source'            => '',
            'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
            'is_visible'        => 1,
            'required'          => 0,
            'searchable'        => 0,
            'filterable'        => 0,
            'unique'            => 0,
            'comparable'        => 0,
            'visible_on_front'  => 0,
            'user_defined'      => 1,
        )
    );

//$installer->removeAttribute('catalog_category', 'custom_tab_text');

$installer->endSetup();

So do not forget to add resource in config.xml file .SO please follow below code for declaring resource.

Config.xml:

Dir: app/code/[CodePool]/[NameSpace]/[ModuleName]/etc/config.xml
e.g: app/code/local/JRB/Customtab/etc/config.xml

<models>
    <customtab>
          <class>JRB_Customtab_Model</class>
          <resourcemodel>customtab_mysql4</resourcemodel>
    </customtab>
</models>
<resources>
    <customtab_setup>
        <setup>
            <module>JRB_Customtab</module>
            <class>Mage_Eav_Model_Entity_Setup</class>
        </setup>
        <connection>
            <use>core_setup</use>
        </connection>
    </customtab_setup>
</resources>
......................
......................

Second method:

By custom table:

In this method if you will like to manage you data base table through this tab then fine you manage by the way.Here I am not explaining all possibilities to create custom tab in category edit page.For this you can follow magento custom tab of category edit page article.Lets se how we can do it.

Config.xml:

Dir: app/code/[CodePool]/[NameSpace]/[ModuleName]/etc/config.xml
e.g: app/code/local/JRB/Customtab/etc/config.xml

 <config>
    ................................
    ................................
    <global>
        .............................
        .............................
        <blocks>
            <customtab>
              <class>JRB_Customtab_Block</class>
            </customtab>
        </blocks>
        <models>
            <customtab>
                <class>JRB_Customtab_Model</class>
                <resourcemodel>customtab_mysql4</resourcemodel>
            </customtab>
        </models>
        <events>
            <adminhtml_catalog_category_tabs><!-- identifier of the event we want to catch -->
                <observers>
                    <customtab_customtab_observer><!-- identifier of the event handler -->
                        <type>model</type><!-- class method call type; valid are model, object and singleton -->
                        <class>customtab/observer</class><!-- observers class alias -->
                        <method>addCategoryTab</method><!-- observer's method to be called -->
                        <args></args> <!-- additional arguments passed to observer -->
                    </customtab_customtab_observer>
                </observers>
            </adminhtml_catalog_category_tabs>
              <catalog_category_prepare_save><!-- identifier of the event we want to catch -->
                <observers>
                    <customtab_catalog_category_prepare_save><!-- identifier of the event handler -->
                        <type>singleton</type><!-- class method call type; valid are model, object and singleton -->
                        <class>customtab/observer</class><!-- observers class alias -->
                        <method>categorySave</method><!-- observer's method to be called -->
                    </customtab_catalog_category_prepare_save><!-- additional arguments passed to observer -->
                </observers>
            </catalog_category_prepare_save>
        </events>
        .............................
        .............................
    </global>
</config>

Form.php:

Dir: app/code/[CodePool]/[NameSpace]/[ModuleName]/Block/Adminhtml/Category/Form.php
e.g: app/code/local/JRB/Customtab/Block/Adminhtml/Category/Form.php

After declaring the block content then we can use the form for that block content. So if you see Observer.php file then you will be getting some code which declares the form location by:

$tabs = $observer->getEvent()->getTabs();
$tabs->addTab('custom_tab', array(
    'label'     => Mage::helper('catalog')->__('My Custom Tab'),
    'content' => $tabs->getLayout()->createBlock('customtab/adminhtml_category_form')->toHtml()
));

Then see the Form.php file by the code below.

< ?php
class JRB_Customtab_Block_Adminhtml_Category_Form extends Mage_Adminhtml_Block_Widget_Form {
    /**
     * create form element
     * category JRB
     * package JRB_Customtab
     * author Jyotiranjan Biswal <biswal@jyotiranjan.in>
     */
    protected function _prepareForm(){
	$form = new Varien_Data_Form();
	$this->setForm($form);
	
	$fieldset = $form->addFieldset('custom_category_tab_form', array('legend'=>Mage::helper('catalog')->__('Custom Tab')));
        
	$fieldset->addField('custom_tab_text', 'text', array(
	    'label'=> Mage::helper('catalog')->__('My Custom tab text'),
	    'class' => '',
            'required' => false,
	    'name'=> 'custom_tab_text',
	    'note' => Mage::helper('catalog')->__('My Custom tab text'),
	));
	
	$fieldset->addField('custom_tab_textarea', 'textarea', array(
            'label' => Mage::helper('catalog')->__('My Custom tab textarea'),
            'class' => '',
            'required' => false,
            'name' => 'custom_tab_textarea',
            'note' => Mage::helper('catalog')->__('My Custom tab textarea'),
        ));
        
	return parent::_prepareForm();     
    }
}

Observer.php:

Dir: app/code/[CodePool]/[NameSpace]/[ModuleName]/Model/Observer.php
e.g: app/code/local/JRB/Customtab/Model/Observer.php

Here I have taken two event one is two create custom tab that is adminhtml_catalog_category_tabs and other is to take param value when category will save that is catalog_category_prepare_save.

< ?php
class JRB_Customtab_Model_Observer
{
    /**
     * add extra tab to block content 
     * category JRB
     * package JRB_Customtab
     * author Jyotiranjan Biswal <biswal@jyotiranjan.in>
     */
    public function addCategoryTab($observer)
    {
        $tabs = $observer->getEvent()->getTabs();
        $tabs->addTab('custom_tab', array(
            'label'     => Mage::helper('catalog')->__('My Custom Tab'),
            'content' => $tabs->getLayout()->createBlock('customtab/adminhtml_category_form')->toHtml()
        ));
    }
    /**
     * add extra tab to block content 
     * category JRB
     * package JRB_Customtab
     * author Jyotiranjan Biswal 
     */
    public function categorySave($observer)
    {
        $params = $observer->getRequest()->getParams();
	// save data base column value by taking param value into you custom database table
    }
}

Hope you understand this tutorial and will never forget to share.

Magento custom tab of category edit page with custom attribute
Tagged on:                                     

3 thoughts on “Magento custom tab of category edit page with custom attribute

Leave a Reply

Your email address will not be published. Required fields are marked *