home forums resources search newsjoinmembers: 6050
Hiveminds Network: Joomla Wordpress Drupal Fireorb Flash Java PHP Ruby Windows Linux
Hiveminds | Fri, 2006-07-14 10:40  tags: , ,

PHP developer

One week into it and I am already hacking the Joomla! CMS core code. I knew it would happen after a while but the reason for the needed hack came as a surprise. I was surprised because I have seen dozens of Joomla! based websites with some fantastic looking designs. I never noticed one thing though, most of those designs used text links in their menu systems. It seems that Joomla! does not support the use of images as links in its menu system.

So when I needed to use images in the main navigation menu for the website that I am building I thought that it should be a simple fix. Just go out and get a Joomla! component that does what I need to get done.
I tried five different free GPL components and came to the conclusion that if using images is possible it just is not very easy. Any component that overrides the default menu also needs help establishing a new menu via a template. None of the templates I saw allowed the simple use of an image as a link in a menu. Most where DHTML style menus that still used text links. So after a few hours of reading at the Joomla! forums and surfing the web I saw that the best and easies solution was to hack the mainmenu module.

The hack is a fairly simple one because Joomla! does provide a mechanism for using images as icons next to text links. Why they did not go the next step and add in full image usage is beyond me. It only seemed logical. But in the years that I have been using different CMS's I have found that logic is last on any feaure list. The hack was to just change the code so that the icon image becomes an actually link when an image setting is detected.

<?php

/**
    * Utility function for writing a menu link
    */
    
function mosGetMenuLink$mitem$level=0, &$params$open=null ) {
        global 
$Itemid$mosConfig_live_site$mainframe;
        
$txt '';
        
$image_link 0;
        switch (
$mitem->type) {
            case 
'separator':
            case 
'component_item_link':
                break;
            case 
'url':
                if ( 
eregi'index.php\?'$mitem->link ) ) {
                    if ( !
eregi'Itemid='$mitem->link ) ) {
                        
$mitem->link .= '&Itemid='$mitem->id;
                    }
                }
                break;
            case 
'content_item_link':
            case 
'content_typed':
                
// load menu params
                
$menuparams = new mosParameters$mitem->params$mainframe->getPath'menu_xml'$mitem->type ), 'menu' );
                
$unique_itemid $menuparams->get'unique_itemid');
                if ( 
$unique_itemid ) {
                    
$mitem->link .= '&Itemid='$mitem->id;
                } else {
                    
$temp split('&task=view&id='$mitem->link);
                    if ( 
$mitem->type == 'content_typed' ) {
                        
$mitem->link .= '&Itemid='$mainframe->getItemid($temp[1], 10);
                    } else {
                        
$mitem->link .= '&Itemid='$mainframe->getItemid($temp[1], 01);
                    }
                }
                break;
            default:
                
$mitem->link .= '&Itemid='$mitem->id;
                break;
        }
        
// Active Menu highlighting
        
$current_itemid $Itemid;
        if ( !
$current_itemid ) {
            
$id '';
        } else if ( 
$current_itemid == $mitem->id ) {
            
$id 'id="active_menu'$params->get'class_sfx' ) .'"';
        } else if( 
$params->get'activate_parent' ) && isset( $open ) && in_array$mitem->id$open ) )  {
            
$id 'id="active_menu'$params->get'class_sfx' ) .'"';
        } else {
            
$id '';
        }
        if ( 
$params->get'full_active_id' ) ) {
            
// support for `active_menu` of 'Link - Component Item'
            
if ( $id == '' && $mitem->type == 'component_item_link' ) {
                
parse_str$mitem->link$url );
                if ( 
$url['Itemid'] == $current_itemid ) {
                    
$id 'id="active_menu'$params->get'class_sfx' ) .'"';
                }
            }
            
// support for `active_menu` of 'Link - Url' if link is relative
            
if ( $id == '' && $mitem->type == 'url' && strpos'http'$mitem->link ) === false) {
                
parse_str$mitem->link$url );
                if ( isset( 
$url['Itemid'] ) ) {
                    if ( 
$url['Itemid'] == $current_itemid ) {
                        
$id 'id="active_menu'$params->get'class_sfx' ) .'"';
                    }
                }
            }
        }
        
// replace & with amp; for xhtml compliance
        
$mitem->link ampReplace$mitem->link );
        
// run through SEF convertor
        
$mitem->link sefRelToAbs$mitem->link );
        
$menuclass 'mainlevel'$params->get'class_sfx' );
        if (
$level 0) {
            
$menuclass 'sublevel'$params->get'class_sfx');
        }
        
// replace & with amp; for xhtml compliance
        // remove slashes from excaped characters
        
$mitem->name stripslashesampReplace($mitem->name) );
        switch (
$mitem->browserNav) {
            
// cases are slightly different
            
case 1:
                
// open in a new window
                
$txt '<a href="'$mitem->link .'" target="_blank" class="'$menuclass .'" '$id .'>'$mitem->name .'</a>';
                break;
            case 
2:
                
// open in a popup window
                
$txt "<a href=\"#\" onclick=\"javascript: window.open('"$mitem->link ."', '', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=780,height=550'); return false\" class=\"$menuclass\" "$id .">"$mitem->name ."</a>\n";
                break;
            case 
3:
                
// don't link it
                
$txt '<span class="'$menuclass .'" '$id .'>'$mitem->name .'</span>';
                break;
            default:
                
// open in parent window
                
if ( $params->get'menu_images' ) ) {
                    
$menu_params     = new stdClass();
                    
$menu_params     = new mosParameters$mitem->params );
                    
$menu_image     $menu_params->def'menu_image', -);
                    
$txt '<a href="'$mitem->link .'" class="'$menuclass .'" '$id .' id="link-'.$mitem->id.'">'.'<img src="'$mosConfig_live_site .'/images/stories/'$menu_image .'" border="0" alt="'$mitem->name .'"/>'.'</a>'."\n";
                    
$image_link 1;
                }
                else{
                    
$txt '<a href="'$mitem->link .'" class="'$menuclass .'" '$id .' id="link-'.$mitem->id.'">'$mitem->name .'</a>'."\n";
                }
                break;
        }
        if ( 
$params->get'menu_images' ) ) {
            
$menu_params     = new stdClass();
            
$menu_params     = new mosParameters$mitem->params );
            
$menu_image     $menu_params->def'menu_image', -);
            if ( ( 
$menu_image != '-1' ) && $menu_image && !$image_link) {
                
$image '<img src="'$mosConfig_live_site .'/images/stories/'$menu_image .'" border="0" alt="'$mitem->name .'"/>';
                if ( 
$params->get'menu_images_align' ) ) {
                    
$txt $txt .' '$image;
                } else {
                    
$txt $image .' '$txt;
                }
            }
        }
        return 
$txt;
    }
?>


Happy Publishing!

Hiveminds's picture
This article brought to you by the Hiveminds Magazine - Staff. Contact us if you want to post an article or announcement anonymously
 
a Visitor posted on: Thu, 2006-11-16 01:25.

Oh wow.. I never throught of that. thanks for the code.

a Visitor posted on: Tue, 2007-10-02 10:59.

This very useful!
Would it be possible to make another version of this function which includes both the image and the menu text in the link?

a Visitor posted on: Tue, 2007-10-02 15:26.

Preferably where the text aligns with the middle of the image.

a Visitor posted on: Fri, 2008-04-04 00:39.

where do i put this code

Hiveminds posted on: Fri, 2008-04-04 16:50.

Hi,

Let us know what version of Joomla! you are using so we can test and see if the code still works. I have to do a search and update the article because to be honest I have forgotten where the code goes.

I should have an answer for you by tomorrow.

a Visitor posted on: Sun, 2008-05-11 11:17.

Can you please tell me where to put this code?

Hiveminds posted on: Sun, 2008-05-11 11:36.

You did not say which version of Joomla! you use. In Joomla 1.5 the code changes are in legacy.php. In Joomla ! 1.0 go to modules/mod_mainmenu.php.

Bitrix Site manager - fast to create, easy to manage CMS Comparison Matrix
Put Your Site Here Developer Links
 

Newsletter

Get updates on Hiveminds services, articles and downloads by signing up for the newsletter.

Editor's choice

Some of the better articles, stories and tutorials found at Hiveminds.

Find more

Find more of Hiveminds articles, stories, tutorials and user comments by searching.




Picked links

Hand picked websites and articles from around the web that provide quality reading.