Webeks.net - freelance programming
freelance programming - php, Joomla, Zend ...
Home :: Articles :: Programming :: Joomla! :: Joomla 1.6 custom menu systems with note field

Joomla 1.6 custom menu systems with note field

Written by Miha

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.

  1. $app = JFactory::getApplication();
  2. $menu = $app->getMenu();

And then on line 43 menu gets its items by calling getItems() function.

  1. $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.

  1.  
  2. class WApplication {
  3. public function getMenu($name = null, $options = array()) {
  4. if (!isset($name)) {
  5. $name = 'site';
  6. } 
  7. require_once(JPATH_BASE . DS . 'modules' . DS . 'mod_wmenu' . DS . 'helpers' . DS . 'menu.php');
  8. $menu = WMenu::getInstance($name, $options);
  9.  
  10. if (JError::isError($menu)) {
  11. return null;
  12. }
  13.  
  14. return $menu;
  15.  
  16. }
  17.  
  18. }

In folder helpers create another file menu.php for overriding menu getInstance() function.

  1. class WMenu extends JMenu {
  2.  
  3. public static function getInstance($client, $options = array()) {
  4.  
  5. static $instances;
  6.  
  7. if (!isset($instances)) {
  8.  
  9. $instances = array();
  10.  
  11. }
  12.  
  13. if (empty($instances[$client])) {
  14.  
  15. //Load the router object
  16.  
  17. $info = JApplicationHelper::getClientInfo($client, true);
  18.  
  19. require_once(JPATH_BASE.DS.'modules'.DS.'mod_wmenu'.DS.'helpers'.DS.'menuSite.php');
  20.  
  21. // Create a JPathway object
  22.  
  23. $classname = 'WMenuSite';
  24. $instance = new $classname($options);
  25. $instances[$client] = & $instance;
  26.  
  27. }
  28.  
  29. return $instances[$client];
  30.  
  31. }
  32.  
  33. }

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.

  1. class WMenuSite extends JMenuSite {
  2.  
  3. public function load() {
  4.  
  5. $cache = JFactory::getCache('mod_menu', ''); // has to be mod_menu or this cache won't get cleaned
  6.  
  7. if (!$data = $cache->get('menu_items' . JFactory::getLanguage()->getTag())) {
  8.  
  9. // Initialise variables.
  10. $db = JFactory::getDbo();
  11. $app = JFactory::getApplication();
  12. $query = $db->getQuery(true);
  13.  
  14. $query->select('m.id, m.note, m.menutype, m.title, m.alias, m.path AS route, m.link, m.type, m.level');
  15. $query->select('m.browserNav, m.access, m.params, m.home, m.img, m.template_style_id, m.component_id, m.parent_id');
  16. $query->select('m.language');
  17. $query->select('e.element as component');
  18. $query->from('#__menu AS m');
  19. $query->leftJoin('#__extensions AS e ON m.component_id = e.extension_id');
  20. $query->where('m.published = 1');
  21. $query->where('m.parent_id > 0');
  22. $query->where('m.client_id = 0');
  23. $query->order('m.lft');
  24.  
  25. $user = JFactory::getUser();
  26. $groups = implode(',', $user->getAuthorisedViewLevels());
  27. $query->where('m.access IN (' . $groups . ')');
  28.  
  29. // Set the query
  30. $db->setQuery($query);
  31. if (!($menus = $db->loadObjectList('id'))) {
  32. JError::raiseWarning(500, JText::sprintf('JERROR_LOADING_MENUS', $db->getErrorMsg()));
  33. return false;
  34.  
  35. }
  36.  
  37. foreach ($menus as &$menu) {
  38.  
  39. // Get parent information.
  40. $parent_tree = array();
  41.  
  42. if (isset($menus[$menu->parent_id])) {
  43. $parent_tree = $menus[$menu->parent_id]->tree;
  44. }
  45.  
  46. // Create tree.
  47. $parent_tree[] = $menu->id;
  48. $menu->tree = $parent_tree;
  49.  
  50. // Create the query array.
  51. $url = str_replace('index.php?', '', $menu->link);
  52. $url = str_replace('&', '&', $url);
  53.  
  54. parse_str($url, $menu->query);
  55.  
  56. }
  57.  
  58. $cache->store($menus, 'menu_items' . JFactory::getLanguage()->getTag());
  59. $this->_items = $menus;
  60.  
  61. } else {
  62. $this->_items = $data;
  63. }
  64.  
  65. }
  66.  
  67. }

 

Now all we have to do is to replace calls from helper.php.

  1. $app = JFactory::getApplication();
  2. $menu = $app->getMenu();

with

  1. require_once(JPATH_BASE.'/modules'.DS.'mod_wmenu'.DS.'helpers'.DS.'application.php');
  2. $app = new WApplication();
  3. $menu = $app->getMenu();


blog comments powered by Disqus