I have a module that creates a couple of menu items hook_menu, displays custom forms, has a couple of form validate and submit functions etc.

But I only want any of this to happen if a specific persistent variable has been set. I check for this using variable_get(). If the variable isn't set, I just want to display a 'configuration not set!' message and go no further.

What's the proper way to achieve this? I thought about wrapping all my hook functions in an IF statement, but that can't be right. Ideally I'd want a function at the top of the .module that does the check and aborts there and then. But I have a feeling it's not that simple...

  • are you setting this variable in this module or from another module? – Jimajamma Jan 13 '14 at 23:46
  • With menu it is way more complicated. Menu registry is rebuilt when modules are activated or disabled. So the only good way is to get menu set up in unchanging way, and just set it properly to show/hide elements when you want it - not what you asked, as it's not feasible, but may give you what you need instead. – Mołot Jan 14 '14 at 8:50
  • I'm setting the variable in this module. So thanks for the suggestions, and combined with the answer below I think I've figured it out :) – pushka Jan 14 '14 at 19:18

One solution would be to set a custom access callback for each menu item.

In the function you can then do a check to see wether the variable is set or not. If it isn't then you can either return FALSE to provide an access denied page or use drupal_goto to forward to a custom config form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.