Joomla 1.6 custom menu systems with note field
Menu such I'm using on this site was almost impossible to make with native Joomla 1.5 menu system. Joomla 1.5 was missing additional field where admin could input a title, menu item description etc.
Joomla 1.6 menu system now offers additional note field which can be used. Note field was initially meant as a note to be left for fellow administrator - why was a menu item created. This is why note field is not extracted from database on the frontend by default.
The best approach when developing custom menu system is to copy whole mod_menu and start from there.
In helper.php from line 32 down you'll find menu initialization.
$app = JFactory::getApplication(); $menu = $app->getMenu();
And then on line 43 menu gets its items by calling getItems() function.
$items = $menu->getItems('menutype',$params->get('menutype'));
We have to modify this functions so they return note field along with other stuff. Never, never, never modify core Joomla files!
Create a folder helpers.
In folder helpers create file application.php in which we'll override getMenu() function.
class WApplication { $name = 'site'; } require_once(JPATH_BASE . DS . 'modules' . DS . 'mod_wmenu' . DS . 'helpers' . DS . 'menu.php'); $menu = WMenu::getInstance($name, $options); if (JError::isError($menu)) { return null; } return $menu; } }
In folder helpers create another file menu.php for overriding menu getInstance() function.
class WMenu extends JMenu { static $instances; } //Load the router object $info = JApplicationHelper::getClientInfo($client, true); require_once(JPATH_BASE.DS.'modules'.DS.'mod_wmenu'.DS.'helpers'.DS.'menuSite.php'); // Create a JPathway object $classname = 'WMenuSite'; $instance = new $classname($options); $instances[$client] = & $instance; } return $instances[$client]; } }
And one more file menuSIte.php for overriding menu load() function. Please note line 14 where we add m.note to the list of fields we want to select.
class WMenuSite extends JMenuSite { public function load() { $cache = JFactory::getCache('mod_menu', ''); // has to be mod_menu or this cache won't get cleaned if (!$data = $cache->get('menu_items' . JFactory::getLanguage()->getTag())) { // Initialise variables. $db = JFactory::getDbo(); $app = JFactory::getApplication(); $query = $db->getQuery(true); $query->select('m.id, m.note, m.menutype, m.title, m.alias, m.path AS route, m.link, m.type, m.level'); $query->select('m.browserNav, m.access, m.params, m.home, m.img, m.template_style_id, m.component_id, m.parent_id'); $query->select('m.language'); $query->select('e.element as component'); $query->from('#__menu AS m'); $query->leftJoin('#__extensions AS e ON m.component_id = e.extension_id'); $query->where('m.published = 1'); $query->where('m.parent_id > 0'); $query->where('m.client_id = 0'); $query->order('m.lft'); $user = JFactory::getUser(); $query->where('m.access IN (' . $groups . ')'); // Set the query $db->setQuery($query); if (!($menus = $db->loadObjectList('id'))) { return false; } foreach ($menus as &$menu) { // Get parent information. $parent_tree = $menus[$menu->parent_id]->tree; } // Create tree. $parent_tree[] = $menu->id; $menu->tree = $parent_tree; // Create the query array. } $cache->store($menus, 'menu_items' . JFactory::getLanguage()->getTag()); $this->_items = $menus; } else { $this->_items = $data; } } }
Now all we have to do is to replace calls from helper.php.
$app = JFactory::getApplication(); $menu = $app->getMenu();
with
require_once(JPATH_BASE.'/modules'.DS.'mod_wmenu'.DS.'helpers'.DS.'application.php'); $app = new WApplication(); $menu = $app->getMenu();
| < Prev | Next > |
|---|