1

I am trying to add an extra tab to the top of the category edit page. The default ones are: General Information, Display Settings, Custom Design and Category Products.

So I have created a new module that rewrites the block that generates the tabs. Here is the relevant snippet from config.xml:

    <blocks>

        <adminhtml>

            <rewrite>

                <catalog_category_tabs>

                    MyNamespace_MyModule_Block_Catalog_Category_Tabs

                </catalog_category_tabs>

            </rewrite>

        </adminhtml>

    </blocks>

Here is my block that overwrites the default Magento one:

class MyNamespace_MyModule_Block_Catalog_Category_Tabs extends Mage_Adminhtml_Block_Catalog_Category_Tabs
{

    protected function _prepareLayout()
    {
        $categoryAttributes = $this->getCategory()->getAttributes();
        if (!$this->getCategory()->getId()) {
            foreach ($categoryAttributes as $attribute) {
                $default = $attribute->getDefaultValue();
                if ($default != '') {
                    $this->getCategory()->setData($attribute->getAttributeCode(), $default);
                }
            }
        }

        $attributeSetId     = $this->getCategory()->getDefaultAttributeSetId();
        /** @var $groupCollection Mage_Eav_Model_Resource_Entity_Attribute_Group_Collection */
        $groupCollection    = Mage::getResourceModel('eav/entity_attribute_group_collection')
            ->setAttributeSetFilter($attributeSetId)
            ->setSortOrder()
            ->load();
        $defaultGroupId = 0;
        foreach ($groupCollection as $group) {
            /* @var $group Mage_Eav_Model_Entity_Attribute_Group */
            if ($defaultGroupId == 0 or $group->getIsDefault()) {
                $defaultGroupId = $group->getId();
            }
        }

        foreach ($groupCollection as $group) {
            /* @var $group Mage_Eav_Model_Entity_Attribute_Group */
            $attributes = array();
            foreach ($categoryAttributes as $attribute) {
                /* @var $attribute Mage_Eav_Model_Entity_Attribute */
                if ($attribute->isInGroup($attributeSetId, $group->getId())) {
                    $attributes[] = $attribute;
                }
            }

            // do not add grops without attributes
            if (!$attributes) {
                continue;
            }

            $active  = $defaultGroupId == $group->getId();
            $block = $this->getLayout()->createBlock($this->getAttributeTabBlock(), '')
                ->setGroup($group)
                ->setAttributes($attributes)
                ->setAddHiddenFields($active)
                ->toHtml();
            $this->addTab('group_' . $group->getId(), array(
                'label'     => Mage::helper('catalog')->__($group->getAttributeGroupName()),
                'content'   => $block,
                'active'    => $active
            ));
        }

        $this->addTab('products', array(
            'label'     => Mage::helper('catalog')->__('Category Products'),
            'content'   => $this->getLayout()->createBlock(
                'adminhtml/catalog_category_tab_product',
                'category.product.grid'
            )->toHtml(),
        ));

        // dispatch event add custom tabs
        Mage::dispatchEvent('adminhtml_catalog_category_tabs', array(
            'tabs'  => $this
        ));

        $this->addTab('myextratab', array(
            'label'     => Mage::helper('catalog')->__('My Extra Tab'),
            'content'   => 'Here is the contents for my extra tab'
        ));        

        return parent::_prepareLayout();
    }
}

Note the extra tab code:

        $this->addTab('myextratab', array(
            'label'     => Mage::helper('catalog')->__('My Extra Tab'),
            'content'   => 'Here is the contents for my extra tab'
        )); 

However, the right hand side of the screen is just blank. The category tree still remains but clicking on a category gives this Javascript error in Firebug: ReferenceError: category_info_tabsJsTabs is not defined

UPDATE: Having read this duplicate question and aswer on SO it looks like I have done everything. Is there some layout code I am missing?

Any help is massively appreciated.

  • Covering the basics... You have logged out and back into the backend and cleaned caches.? – Melvyn Jan 16 '14 at 17:49
  • Yep logged in, logged out rm -rf var/cache/*'d n everything :( – beingalex Jan 16 '14 at 17:53
  • You trying to group some new attribute fields or are you just adding custom content? – benmarks Jan 16 '14 at 22:11
11

If you are simply organizing new attribute groups, create a setup script:

$installer = Mage::getResourceModel('catalog/setup','catalog_setup');
$installer->startSetup();

//Categories typically only have one attribute set, this will retrieve its ID
$setId = Mage::getSingleton('eav/config')->getEntityType('catalog_category')->getDefaultAttributeSetId();

//Add group to entity & set
$installer->addAttributeGroup('catalog_category',$setId, 'My Extra Tab');

$installer->endSetup();

If you are adding one or more attributes as well, just specify the tab name in the group configuration value for the attribute and the group will be added automatically:

$installer = Mage::getResourceModel('catalog/setup','catalog_setup');
$installer->startSetup();

//Add group to entity & all attribute sets
$installer->addAttribute(
    'catalog_category',
    'new_attribute',
    array(
        'label' => 'New Attribute',
        'group' => 'My Extra Tab'   //will be created if necessary
    )
);

$installer->endSetup();

If you are trying to just add some generic content, adding a tab via the adminhtml_catalog_category_tabs event - as noted in another answer - will be the way to go.

  • Oh yeah! How did I miss that? If you are adding new attributes do what ben says. – David Manners Jan 17 '14 at 6:29
  • @benmarks is it possible to add serializable attribute with setup script? – tmm Jul 14 '15 at 9:56
  • @tmm That's a new question ;-) – benmarks Jul 14 '15 at 10:58
  • @benmarks magento.stackexchange.com/questions/74375/… – tmm Jul 15 '15 at 8:58
8

So the problem with your code is fairly simple.

The xml is malformatted and should look as follows, with the <catalog_category_tabs> and MyNamespace_MyModule_Block_Catalog_Category_Tabs on the same line with no extra white space:

<blocks>
    <adminhtml>
        <rewrite>
            <catalog_category_tabs>MyNamespace_MyModule_Block_Catalog_Category_Tabs</catalog_category_tabs>
        </rewrite>
    </adminhtml>
</blocks>

After making this change your code will work, but there are some other comments about your code that you should take into account.

Point 1

What your code will try to do is call your block's _prepareLayout and then call the parent::_prepareLayout() which in this case is Mage_Adminhtml_Block_Catalog_Category_Tabs. What you can do is update your block as follows. Note: this will add your new tab first which might not be idea.

protected function _prepareLayout()
{
    $this->addTab('myextratab', array(
            'label'     => Mage::helper('catalog')->__('My Extra Tab'),
            'content'   => 'Here is the contents for my extra tab'
        ));

    return parent::_prepareLayout();
}

Point 2

There is an event that is perfect for what you want to do. You can listen to the event adminhtml_catalog_category_tabs and then in your observer doing something like.

$tabs = $observer->getTabs();
$tabs->addTab('myextratab', array(
    'label'     => Mage::helper('catalog')->__('My Extra Tab'),
    'content'   => 'Here is the contents for my extra tab'
)); 

This would help you if any other code rewrites this block

  • Of course! Can't believe I missed that XML error!!! ARRGH!!! – beingalex Jan 17 '14 at 9:03
  • Which method would you recommend? The observer method or the rewrite method? – beingalex Jan 17 '14 at 9:19
  • If you are not adding new attributes then the observer, if you are dealing with custom attributes then I would suggest Ben's answer about the set-up scripts :) – David Manners Jan 17 '14 at 9:21
  • Thank you :) Apologies but changed accepted to Ben's answer. You fixed my initial problem though :) – beingalex Jan 17 '14 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.