Project:Drupal core
Version:7.x-dev
Component:user.module
Category:feature request
Priority:normal
Assigned:Unassigned
Status:closed (works as designed)

Issue Summary

Hi,

I frequently get asked if we can have user module messages regarding logins appear in the login block for things like incorrect password, registration successful, etc.

I know it is possible to do something like this:

<?php
drupal_set_message
(t('Password and further instructions have been e-mailed to the new user <a href="@url">%name</a>.', array('@url' => url("user/$account->uid"), '%name' => $account->name)), 'user');
?>

And then in my theme I could do something like this where I wanted the messages of that type to appear:

<?php
print drupal_get_messages('user');
?>

However, the user module does not assign a type (or at least, only assigns a type of 'error' for errors) so I cannot utilise this option. Further, I cannot override the default behaviour.

I guess the ideal would be to be able to alter/override a message type somehow, but it would be great if the user module applied a type to its messages so I could get a handle on them. I'm sure other people must be having similar issues?

For now the only workaround I can think of would be to submit the form with AJAX (JQuery or Ah-ha) which would make any resulting message get pushed back to the point in the mark-up the AJAX submission was made from - I think?

Comments

#1

Component:user system» user.module

Oops, wrong component.

#2

Version:6.x-dev» 7.x-dev
Component:user.module» base system

I think your request does not work, because you can not tell exactly which messages you have to override, which messages belongs to given functionality.. Adding a new message type as module name does not help differentiate..

I think what you need is something like a form_set_message() function, which has a form-id or a form element parameter, where you could tell which form initiated the message. And then you could call a drupal_set_message() with a form-id or something..

#3

Component:base system» user.module

Hi,

Thanks for coming back. What you describe would be cool as well, but what I requested DOES work (or would) because I'm not interested in a specific message. I want to put ALL messages of a specific type (e.g. user messages) in one part of my layout. Documentation reads:

$type (optional) Only return messages of this type.

http://api.drupal.org/api/function/drupal_get_messages

Because I do not care about individual messages, only getting a handle on messages of a specific 'type', e.g. user messages, this already works. It just requires the modules (in this case user.module) to actually implement the type setting. Then, as described in the original post, I can get an array of messages that are specific to the user module in my theme.

Though I agree, your second suggestion is very cool too. It would be great to have both the ability to take a SET of messages and say where they appear (which is already possible, but the user module does not assign messages in to a set) as well as what you describe. So, I'm setting this back to a user.module request and I've created a new issue for your proposal. =)

See http://drupal.org/node/314756

#4

but the problem is, you can't tell which message is a "user" message
should all messages in user.module be "user" messages?
if I have a custom module which has some user management, should I mark messages as "user" type messages, or should I create a new category?
is "incorrect password" a "user" message?

as you can't categorize modules into one specific category, you can't categorize messages. there is no perfect categorization..

we can create a functionality where you can tell which module initiated the message, but as I see it, it is not usable for anything..

#5

Status:active» closed (works as designed)

So in your opinion the current ability in Drupal to set the type of a message to any string is useless? I don't see it like that.

we can create a functionality where you can tell which module initiated the message, but as I see it, it is not usable for anything..

You don't have to create it. It's already there. =)

At the moment most modules (including user.module) only use the Drupal 5.x default types of 'status' or 'error'. (There is now 'warning' in Drupal 6.x too, but I haven't seen it used in most cases.) While the documentation says:

$type The type of the message. One of the following values are possible:

* 'status'
* 'warning'
* 'error'

If you look at the drupal_set_message() code you can see any value is in fact valid for $type: http://api.drupal.org/api/function/drupal_set_message

is "incorrect password" a "user" message?

as you can't categorize modules into one specific category, you can't categorize messages. there is no perfect categorization..

This is a very good point though. I would say "yes, incorrect password is a user message", because it is generated by the user module. And then, as a developer, I can then check the message string in the array $messages and decide whether I want to render it as a status, warning, error or something else entirely. But this messes with Drupal's core behaviour, so you're right, it would be silly to change it while the Drupal messaging behaviour is as it is. I have come across the need to do this three times in the last year, so I think the wider issue is a valid addition to Drupal 7.x.

But it would be better (and I'll add this to the other issue I raised) to be able to have something like this:

drupal_get_messages($modules, $type, $clear_queue)

If you imagine $modules is an array, then an example implementation to get all messages generated by the user module, regardless of type, might be:

<?php
if (arg(1) == 'user') {
 
$user_messages = drupal_get_messages(array('user' => 'user'));
} else {
 
$other_messages = drupal_get_messages(module_list());
}
?>

And your form id idea would add even further granularity.

I'll set this issue to "by design". Thanks for your input and helping clarify my real request. =)

nobody click here