Drupal: Hooking Into Menu Item's After Save State

Posted Jul 20, 2009 // 3 comments
Irakli:

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.

About Irakli

Irakli is Director of Product Development at Phase2 Technology. His main responsibility is development of packaged, turn-key solutions using open-source technologies and cutting-edge semantic APIs.

Irakli has been an avid open-source ...

more >

Read Irakli's Blog

Comments

by agentrickard (not verified) on Tue, 07/21/2009 - 10:11

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.

by moshe weitzman (not verified) on Tue, 07/21/2009 - 11:03

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

by irakli on Tue, 07/21/2009 - 13:36

Sounds great

Sounds great to me.

thx

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Allowed HTML tags: <a> <strong> <code> <p> <img> <ul> <ol> <li> <h2> <h3> <h4> <b> <u> <i>
  • You may insert videos with [video:URL]

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.