Magento add external URL to menu admin

I was just trying to add an external link on my admin menu panel and figured out that there is no direct option to add, we need to override some functionality to achieve this. So here is the solution I found out for the same. Unfortunately, we need to have a small module for this functionality:-

Firstly we need to overwrite the _buildMenuArray() function of Mage_Adminhtml_Block_Page_Menu class to add a condition for external url. So let’s start our modification:-

Create your config file to override the page menu class, as shown in below code:

<config>
    <modules>
        <W3solver_Menulink>
            <version>1.0.0</version>
        </W3solver_Menulink>
    </modules>
	
    <global>
        <blocks>
            <adminhtml>
                <rewrite>
                    <page_menu>W3solver_Menulink_Block_Adminhtml_Page_Menu</page_menu>
                </rewrite>
            </adminhtml>
        </blocks>
        <helpers>
            <menulink>
                <class>W3solver_Menulink_Helper</class> 
            </menulink>
        </helpers>
    </global>
</config>

Add a block file to override page menu, review line 40 for the modification:-

class W3solver_Menulink_Block_Adminhtml_Page_Menu extends Mage_Adminhtml_Block_Page_Menu {
    
    /**
     * Recursive Build Menu array
     *
     * @param Varien_Simplexml_Element $parent
     * @param string $path
     * @param int $level
     * @return array
     */
    protected function _buildMenuArray(Varien_Simplexml_Element $parent=null, $path='', $level=0)
    {
        if (is_null($parent)) {
            $parent = Mage::getSingleton('admin/config')->getAdminhtmlConfig()->getNode('menu');
        }

        $parentArr = array();
        $sortOrder = 0;
        foreach ($parent->children() as $childName => $child) {
            if (1 == $child->disabled) {
                continue;
            }

            $aclResource = 'admin/' . ($child->resource ? (string)$child->resource : $path . $childName);
            if (!$this->_checkAcl($aclResource) || !$this->_isEnabledModuleOutput($child)) {
                continue;
            }

            if ($child->depends && !$this->_checkDepends($child->depends)) {
                continue;
            }

            $menuArr = array();

            $menuArr['label'] = $this->_getHelperValue($child);

            $menuArr['sort_order'] = $child->sort_order ? (int)$child->sort_order : $sortOrder;

            /* condition added to manage external url*/
            if ($child->external_url) {
                $menuArr['url'] = (string)$child->external_url;
            } else if ($child->action) {
                $menuArr['url'] = $this->_url->getUrl((string)$child->action, array('_cache_secret_key' => true));
            } else {
                $menuArr['url'] = '#';
                $menuArr['click'] = 'return false';
            }

            $menuArr['active'] = ($this->getActive()==$path.$childName)
                || (strpos($this->getActive(), $path.$childName.'/')===0);

            $menuArr['level'] = $level;

            if ($child->children) {
                $menuArr['children'] = $this->_buildMenuArray($child->children, $path.$childName.'/', $level+1);
            }
            $parentArr[$childName] = $menuArr;

            $sortOrder++;
        }

        uasort($parentArr, array($this, '_sortMenu'));

        while (list($key, $value) = each($parentArr)) {
            $last = $key;
        }
        if (isset($last)) {
            $parentArr[$last]['last'] = true;
        }

        return $parentArr;
    }

 

Here is code of my menu:-

<config>
    <menu>
        <menulink translate="title" module="menulink">
            <title>Menu Link</title>
            <sort_order>400</sort_order>
            <children>
                <menulink translate="title" module="menulink">
                    <title>Menu Link</title>
                    <sort_order>1</sort_order>
                    <external_url>http:www.google.com</external_url>
                </menulink>
            </children>
        </menulink>
    </menu>
</config>

Just put down your comment if you face any issue.

4 Comments

  1. Hey there, You’ve performed an excellent job. I will definitely digg it and in my opinion recommend to my friends. I’m confident they will be benefited from this web site.

    Reply

Leave a Comment.