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.

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.

Irakli Nadareishvili