The web designer for Utopian Coffee Co, recently contacted me with a problem he was having with the VirtueMart menus in their Joomla site. He was redesigning the site to have four main sections denoted by the items on the main menu. Each section has a submenu that provides navigation for within the section. While the user remains in the section, the respective main menu item remains highlighted. This worked fine for the Joomla submenus provided by com_mainmenu, but the VirtueMart menus broke this feature.
These types of CMS problem are nasty and can often take several hours before you realize that there's not going to be any elegant solution out there. I admit, after reading the email I cringed. I was on a timer for learning the Zend Framework, a MVC framework for PHP, but I knew he wouldn't have forwarded me this problem if it hadn't caused some heartache, so I decided to take a look and see if there was a quick fix out there.
My first thought was to see if Joomla had a way to execute some PHP on every page it rendered, but I couldn't seem to find what I was looking for. I then decided to check out the Joomla libraries to see how the menu's were handled. A quick google search showed me that Joomla's main menu module component is what I needed to work with. I browsed to that component, saw a template directory, so moved in there. Inside was a single file that was called "default.php", which I opened up. Inside this default template for the menus was a single function, apparently a callback, that the menu's were using to add styling and alter the html as desired. I took a glance at the things that the default template already did, copied a snippet of their code and produced this small hack:
$url = strtolower($_SERVER['REQUEST_URI']);
if($node->attributes('class') == 'item94'){ // only change the "Shop" node
if (strpos($url,'com_virtuemart') || // check unclean URLs
strpos($url, 'coffee-shop') || // check clean URLS
(isset($path) && ( $path[0] == '40' || // check for coffee club membership page
$path[0] == '66'))) // check for coffee area page
{
$node->addAttribute('id', 'current');
if ($node->attributes('class')) {
$node->addAttribute('class', $node->attributes('class').' active');
} else {
$node->addAttribute('class', 'active');
}
}
}
These few lines of code easily (but nastily) fixed the problem. Of course, a hack is called a hack for a reason - it easily be broken by upgrading the Joomla distribution, adding new content, etc. For a few minutes and fewer lines of a code, its not a bad solution at all. It works by simply looking at the requested URL for small pieces of data like the text "com_virtuemart" or specific article numbers. Now the site's Shop section functions properly with menu.