Drupal: Hooking Into Menu Item's After Save State
Drupal hook system is by far the most powerful feature of Drupal. Alas, even if it gets quite close, Drupal still does not provide hooks for every occasion. One such case is: if you want to hook into post-save state of a menu item. hook_menu_link_alter will allow you to hook into pre-save state, but not post-save state. You may need to have your function fired on post-save, for various re-cache purposes. Pre-save does not work, since the item is not, yet, saved to the database and standard re-cache would just re-cache stale data – your worst nightmare.
What to do?
One solution is to register a shutdown function in a pre-save hook (e.g.: in hook_menu_link_alter). The idea is that pre-save raises a flag, then save fires and at the end of the script, because of the flag, your post-save will fire.
The same approach can be applied to other workflows, as well.




Comments
Menu Node API
Or, in this specific case, you can just use the Menu Node API to track menu changes for you.
http://drupal.org/project/menu_node
If fires hooks for menu insert, update, and delete.
before/after variants
in drupal7, we might consider enhancing module_invoke_all() so that it fires before/after variants for all hooks. if performance does not suffer, i would like it. it would get rid of some one off hooks like nodeapi_after_update
Sounds great
Sounds great to me.
thx
Post new comment