By peterx on
I am adding menu items through hook menu in Drupal 4.6.0. I use !$may_cache but the items do not appear on the current page. Instead they appear on the next page. The menu must be built before hook menu gets the non cache items.
I am using module menu. I tried combinations of cache_flush_all, menu_rebuild, and placing things in hook init but ended up with the same behaviour or no menu items or duplicate menu items. The behaviour is the same with may_cache and !may_cache.
Is there a trick to adding menu items to the current menu?
Is there something I have to do other than use !may_cache?
Comments
I'm going through something
I'm going through something similar myself. The menu caching system is really interfering with debugging my menu code.
By the time your hook_menu() is called, Drupal has already loaded the cached menus from the database. So clearing the cache here won't have any noticable effect till the next page load.
Also, I had difficulty getting the menus cleared from the cache at all. cache_clear_all() with no parameters doesn't seem to do it. In stepping through the menu cache retrieval code, the query is something like "menu:en:0", which I guess is the new locale-specific menu cache support. In order to get the menus out of the cache, you have to either use a locale-specific query or use cache_clear_all('menu', true), with the wildcard flag set to true. This may be more agressive than you actually need, as it looks like there are several entries in the cache db table with a cid that contains 'menu'. But there's no harm done as long as you're in development.
So what seems to work is this:
1) Put your menu generating code inside an if (!$may_cache). If you don't do this, the next time your page gets loaded, your menus will get cached.
2) clear the menu cache once using cache_clear_all('menu', true), or by deleting the record in the database directly using mySQL or phpMyAdmin. I added the line of code in my hook_menu(), refreshed the page, then commented the line out.
3) When you're finished developing your menu code, put it inside an if ($may_cache) to reenable menu caching support.
Finally, are you using an IDE with source debugging? I'm using Zend Studio. I couldn't imagine doing Drupal development without something like this. The only way to figure out what's going on inside Drupal is to set breakpoints and watch as it happens.