0

Which Hook should I use to simply print a message if a configuration is not done yet?

The only idea i've got was hook_node_view, but maybe someone knows something better ?

3

Traditionally that's what hook_requirements() is for

Check installation requirements and do status reporting.

Emphasis mine.

It's how modules declare that they are missing a required Javascript library, for example.

It hooks your module into the status report at /admin/reports/status.

Drupal doesn't have a mechanism for displaying messages in response to a requirement failure; so if you need to show something to the user, hook_node_view() makes as much sense as anything else IMO.

Actually that's not strictly true (I didn't read the docs properly)...hook_requirements() does already handle that in one way or another. From the docs relating to the 'runtime' phase:

any requirement with a severity of REQUIREMENT_ERROR severity will result in a notice on the administration configuration page.

So if your users have access to the admin config page you can use this hook to automatically display them a message. But if the message needs to be on a different page, you'll have to take a different route with the advice in @FreeRadical's answer.

  • Thanks, and I started thinking that i am to dumb to ask questions after getting flamed by the guys above that post ... – Sebastian Apr 17 '14 at 13:49
  • 3
    They're not trying to flame (if you can take my word for it), they're just trying to get to the root of the issue as they don't feel there's enough info to go on. I disagree (I thought it was clear as day), but do make allowances for people misunderstanding...it does happen :) – Clive Apr 17 '14 at 13:52
  • @Clive actually hook_node_view() does not make sense if module in question have nothing to do with nodes, and admin who installed it will stay in admin area, not going to see any nodes because task he is about to do, task he needed module for, does not involve them. So I still think this question lacks a bit of information needed to choose hook that will actually get the message delivered when it's needed. and I honestly didn't meant to "flame" - why would I link to hook_requirements then? – Mołot Apr 17 '14 at 14:04
  • @Mołot I'm (rightly or wrongly) making the assumption that the OP mentioning hook_node_view() inferred the need for the message on a node page. Ideally, every person that ever read anything written by anybody else on the internet would do so with a completely neutral perspective, and assume zero emotion (or maybe even default to positive)...but that ain't human nature :) We read something innocuous, and irrationally feel like we're under attack, and need to defend our position I've done it once already myself today. Got me chewing someone out for no reason, and then tail between legs ;) – Clive Apr 17 '14 at 14:31
  • 1
    @Clive If only OP would have answered "configuration about interacting with nodes" to my first question... I'm just very reluctant to assume anything. I deleted quite a bit of answers posted on bad assumptions. – Mołot Apr 17 '14 at 14:34
2

If you want to check installation requirements and report on any failures during installation or any of the other phases where it is applicable, it is hook_requirements() - already adequately covered by Clive.

But if you just want to print a warning message to the user/admin about some tasks (such as setting some variables), e.g.:

drupal_set_message(t('You've selected X and Y. These are mutually exclusive.'), 'warning');

... you can call this function from more or less anywhere inside one of the functions in your module where you do some check and want to output a warning because the check indicates that something should be done before proceeding. It doesn't have to be a hook. But obviously the function containing the call need to be executed for the message to show. However, placing it inside the same function as the function that performs the check usually takes care of that.

For instance, if the message is used to tell the admin that his choces are mutually exclusive, I have it inside settings submit handler (e.g. MYMODULE_admin_settings_submit). since I usually do this type of sanity checks in the submit handler.

PS: Calling drupal_set_message() just appends the message to an array that is shown on the next page load.

  • 1
    I think OP already knows how to set a message, but he wasn't quite sure where to call drupal_set_message for desired effect. – Mołot Apr 17 '14 at 14:41
  • Small note...hook_requirements() isn't just for installation requirements - it has several phases, one of which is 'runtime' (for runtime requirements as it suggests). If you look at the status report on an installed site, all of the green/yellow/red rows come from one implementation of hook_requirements() or another, using the 'runtime' phase – Clive Apr 17 '14 at 15:06
  • 1
    Thanks @Clive. I just wanted to refer the user to your answer for that angle. Hope it is more correct now. – Free Radical Apr 17 '14 at 15:10

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.