Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new event 'adminhtml_widget_tabs_add_after' for adding custom tab. #879

Closed
wants to merge 1 commit into from

Conversation

kiatng
Copy link
Contributor

@kiatng kiatng commented Jan 16, 2020

There are only a handful for events in core block and none is useful for adding custom tab in admin pages.

  1. core_block_abstract_prepare_layout_after can be used to add custom tab, but it doesn't set the active tab correctly.
  2. adminhtml_block_html_before would not render the custom tab at all as we need html_after.
  3. core_block_abstract_to_html_after also wouldn't render the custom tab.

So the reliable option is to overwrite Mage_Adminhtml_Block_Customer_Edit_Tabs, which is what happened with some 3rd-party extensions, totally not ideal.

Here's the observer for reference when the event adminhtml_widget_tabs_add_after is added:

    public function addTab($observer)
    {
        /**
         * @var Mage_Adminhtml_Block_Customer_Edit_Tabs $block block data:
         * [id] => customer_info_tabs
         * [title] => Customer Information
         * [type] => adminhtml/customer_edit_tabs
         * [module_name] => Webkul_Marketplace // 3rd-party extension overwrite
         */
        $block = $observer->getBlock();
        //if ($block instanceof Mage_Adminhtml_Block_Customer_Edit_Tabs) { // problem when 3rd-party overwrite the block
        if ($block->getId() == 'customer_info_tabs') {
            if (Mage::registry('current_customer')->getId() && Mage::getSingleton('admin/session')->isAllowed('customer/manage/logger')) {  
                $block->addTabAfter('changelog_grid', [
                    'label'     => Mage::helper('logger')->__('Changelog'),
                    'class'     => 'ajax',
                    'active'    => false,
                    'url'       => $block->getUrl(
                        '*/changelog_grid', ['_current' => true, 'prefix' => 'customer']
                    ),
                ], 'tags');
            }
        }
    }

@@ -189,6 +189,7 @@ protected function _setActiveTab($tabId)

protected function _beforeToHtml()
{
Mage::dispatchEvent('adminhtml_widget_tabs_add_after', array('block' => $this));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest either a rename for consistency:

Suggested change
Mage::dispatchEvent('adminhtml_widget_tabs_add_after', array('block' => $this));
Mage::dispatchEvent('adminhtml_widget_tabs_html_before', array('block' => $this));

Or, adding a new method to dispatch the event after the layout is prepared which is always where the default tabs are added:

    protected function _prepareLayout()
    {
        parent::_prepareLayout();
        Mage::dispatchEvent('adminhtml_widget_tabs_prepare_layout_after', array('block' => $this));
        return $this;
    }

@kiatng
Copy link
Contributor Author

kiatng commented Jan 17, 2020

@colinmollenhour Thanks for your review! I tested adminhtml_widget_tabs_prepare_layout_after but the result is the same as the event core_block_abstract_prepare_layout_after; although the tab was rendered, it would set the added tab as active, irregardless of the param active value.

I corrected the event name to adminhtml_widget_tabs_html_before. My mistake seems obvious now but I was blindsided by the intended function I want.

Copy link
Member

@colinmollenhour colinmollenhour left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thanks!

@kiatng kiatng closed this Mar 2, 2020
@kiatng kiatng deleted the tabs_event branch March 2, 2020 04:32
@kiatng kiatng restored the tabs_event branch December 18, 2020 03:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants