Can I Indicate failure from hook_user?
Hello there, I'm new to Drupal, just started a couple of weeks ago. I had following queries regarding hooks. It would be great if someone could provide feedback/suggestions on this. 1. I have written a module which implements "hook_user" and handles "update" and "insert" operations. I want to be able to signal failure/ success from this function. For e.g. if I return failure (boolean FALSE) from "hook_user" for "update" operation, then user update should not happen. I want to do this for "insert", "update" and "delete" operations. * Is it possible to indicate failure in this way so that update/ insert/ delete will not take place? * If yes, how can I accomplish this? 2. Also, I quickly glanced through the user.module. At the top there is a function called "user_module_invoke" which takes care of invoking "hook_user" in various modules. But it does not check for return value, nor does it return anything. So probably result of invoking a hook_user is never checked. If Drupal hooks currently don't check for return value, would it be good idea to add such a functionality? A "pre-operation" hook, which will indicate (boolean TRUE/ FALSE) if the operation should continue or not. Such a hook could be used for various validations when Drupal is integrated with some third-party applications. Any takers for this? Do reply. Thanks in anticipation, Pushyamitra
Always good to see someone new to Drupal. Lots of hook return values. You'll probably want to do something with the $op == "validate" argument of hook_user. Throw an error if you don't want the update/insert to continue. Regards Steven Jones On Mon, Jul 21, 2008 at 10:26 AM, Pushyamitra Navare <pushyamitra@gmail.com> wrote:
Hello there,
I'm new to Drupal, just started a couple of weeks ago. I had following queries regarding hooks. It would be great if someone could provide feedback/suggestions on this.
1. I have written a module which implements "hook_user" and handles "update" and "insert" operations. I want to be able to signal failure/ success from this function. For e.g. if I return failure (boolean FALSE) from "hook_user" for "update" operation, then user update should not happen. I want to do this for "insert", "update" and "delete" operations.
* Is it possible to indicate failure in this way so that update/ insert/ delete will not take place? * If yes, how can I accomplish this?
2. Also, I quickly glanced through the user.module. At the top there is a function called "user_module_invoke" which takes care of invoking "hook_user" in various modules. But it does not check for return value, nor does it return anything. So probably result of invoking a hook_user is never checked.
If Drupal hooks currently don't check for return value, would it be good idea to add such a functionality? A "pre-operation" hook, which will indicate (boolean TRUE/ FALSE) if the operation should continue or not. Such a hook could be used for various validations when Drupal is integrated with some third-party applications. Any takers for this?
Do reply. Thanks in anticipation, Pushyamitra
On Mon, Jul 21, 2008 at 3:53 PM, Steven Jones <darthsteven@gmail.com> wrote:
You'll probably want to do something with the $op == "validate" argument of hook_user. Throw an error if you don't want the update/insert to continue.
Ok, thanks Steven! I will try to see how can I use hook_user's validate operation for my purpose. But can you tell me, is "validate" operation invoked for both "insert" and "update"? If yes, then how do I know in hook_user if it is being called for insert or for update? What parameter of hook_user should I check? I quickly went through documentation but did not find anything. Also, i need such a "validate" hook for "user delete" operation as well. Any way to do that? Thanks, Pushyamitra
On Mon, Jul 21, 2008 at 10:26 AM, Pushyamitra Navare
If Drupal hooks currently don't check for return value, would it be good idea to add such a functionality? A "pre-operation" hook, which will indicate (boolean TRUE/ FALSE) if the operation should continue or not. Such a hook could be used for various validations when Drupal is integrated with some third-party applications.
On Mon, Jul 21, 2008 at 10:03 PM, Pushyamitra Navare <pushyamitra@gmail.com> wrote:
But can you tell me, is "validate" operation invoked for both "insert" and "update"? If yes, then how do I know in hook_user if it is being called for insert or for update? What parameter of hook_user should I check? I quickly went through documentation but did not find anything.
Yes, it's called for any modification of the user. It's an insert if the $account parameter doesn't have a uid, and it's an update if the $account parameter does have a uid.
Also, i need such a "validate" hook for "user delete" operation as well. Any way to do that?
I don't think there is any way - looking at user_delete, it doesn't call hook_user until the user is deleted. -- John Fiala
Ok, cool. Thanks John and Steven! - Pushya On Tue, Jul 22, 2008 at 9:39 AM, John Fiala <jcfiala@gmail.com> wrote:
On Mon, Jul 21, 2008 at 10:03 PM, Pushyamitra Navare <pushyamitra@gmail.com> wrote:
But can you tell me, is "validate" operation invoked for both "insert" and "update"? If yes, then how do I know in hook_user if it is being called for insert or for update? What parameter of hook_user should I check? I quickly went through documentation but did not find anything.
Yes, it's called for any modification of the user. It's an insert if the $account parameter doesn't have a uid, and it's an update if the $account parameter does have a uid.
Also, i need such a "validate" hook for "user delete" operation as well. Any way to do that?
I don't think there is any way - looking at user_delete, it doesn't call hook_user until the user is deleted.
-- John Fiala
Quoting John Fiala <jcfiala@gmail.com>:
I don't think there is any way - looking at user_delete, it doesn't call hook_user until the user is deleted.
Well that isn't too friendly. If you don't want the user to be deleted you could use a hook_form_alter to hide or remove the button. Earnie -- http://for-my-kids.com/ -- http://give-me-an-offer.com/
On Tue, Jul 22, 2008 at 7:15 PM, Earnie Boyd <earnie@users.sourceforge.net> wrote:
Well that isn't too friendly. If you don't want the user to be deleted you could use a hook_form_alter to hide or remove the button.
Well, ideally I would like the pre-user-delete-validate function to be called for every user. If I disable button etc. through hook_form_alter, then either all users will be deleted or none. I won't be able to validate individual users and decide to delete or not delete that user. I actually ended up modifying user.module code and calling my own validate function just before deleting user. Based on validate function's result I either delete the user or just register error. ( The obvious pain would arise when I wish to upgrade to newer Drupal version in future. I would have patch the new user.module as wel. ). For now, it's working... Pushya
Well, one option - I'm not sure how easy it is, because I don't know if you're talking Drupal 5 or Drupal 6 - would be to find all the forms that would try to delete a user via user_delete, and change the validation of those forms using hook_form_alter to call your validation function before the submit comes along and does a user_delete on them. That wouldn't require hacking core. On Tue, Jul 22, 2008 at 8:48 AM, Pushyamitra Navare <pushyamitra@gmail.com> wrote:
On Tue, Jul 22, 2008 at 7:15 PM, Earnie Boyd <earnie@users.sourceforge.net> wrote:
Well that isn't too friendly. If you don't want the user to be deleted you could use a hook_form_alter to hide or remove the button.
Well, ideally I would like the pre-user-delete-validate function to be called for every user. If I disable button etc. through hook_form_alter, then either all users will be deleted or none. I won't be able to validate individual users and decide to delete or not delete that user.
I actually ended up modifying user.module code and calling my own validate function just before deleting user. Based on validate function's result I either delete the user or just register error. ( The obvious pain would arise when I wish to upgrade to newer Drupal version in future. I would have patch the new user.module as wel. ). For now, it's working...
Pushya
-- John Fiala
There's an RTBC patch to move hook_user before users are actually deleted for this against Drupal7: http://drupal.org/node/218189 Nat
That's cool. Apparently other people are also interested in such a pre-user-delete hook. Pity, someone's already generated a patch. Otherwise I could have tried and gotten my hands dirty! :-) Pushyamitra. On Wed, Jul 23, 2008 at 8:17 PM, Nathaniel Catchpole <catch56@googlemail.com> wrote:
There's an RTBC patch to move hook_user before users are actually deleted for this against Drupal7: http://drupal.org/node/218189
Nat
On Tue, Jul 22, 2008 at 9:01 PM, John Fiala <jcfiala@gmail.com> wrote:
Well, one option - I'm not sure how easy it is, because I don't know if you're talking Drupal 5 or Drupal 6 - would be to find all the forms that would try to delete a user via user_delete, and change the validation of those forms using hook_form_alter to call your validation function before the submit comes along and does a user_delete on them. That wouldn't require hacking core.
Thanks John. I am using Drupal 6.2. I am sure what you say will work, and I will be able to change validate handler though form_alter, and validate hook will be called before user is deleted. Only problem I guess is that: it will be called only once even if multiple users are being deleted ('coz it's form validate handler, not user delete handler). Right? If possible, I wanted my validate function to be called for individual user, and delete/not delete user based on errors returned by validate hook. Thanks, Pushya
Quoting Pushyamitra Navare <pushyamitra@gmail.com>:
On Tue, Jul 22, 2008 at 9:01 PM, John Fiala <jcfiala@gmail.com> wrote:
Well, one option - I'm not sure how easy it is, because I don't know if you're talking Drupal 5 or Drupal 6 - would be to find all the forms that would try to delete a user via user_delete, and change the validation of those forms using hook_form_alter to call your validation function before the submit comes along and does a user_delete on them. That wouldn't require hacking core.
Thanks John. I am using Drupal 6.2.
I am sure what you say will work, and I will be able to change validate handler though form_alter, and validate hook will be called before user is deleted. Only problem I guess is that: it will be called only once even if multiple users are being deleted ('coz it's form validate handler, not user delete handler). Right?
If possible, I wanted my validate function to be called for individual user, and delete/not delete user based on errors returned by validate hook.
Your module can modify the value for the MENU_CALLBACK item to action a different function which will eventually call the appropriate _submit hook. Earnie -- http://for-my-kids.com/ -- http://give-me-an-offer.com/
Hello there, On Mon, Jul 21, 2008 at 3:53 PM, Steven Jones <darthsteven@gmail.com> wrote:
Always good to see someone new to Drupal.
You'll probably want to do something with the $op == "validate" argument of hook_user. Throw an error if you don't want the update/insert to continue.
I am back with a question related to this. What is order in which hooks are called? For e.g. let's say there's a captcha module. Now as I see on my machine, hook_user is called first. After that, validation of captcha happens. Is there any way I can control the sequence in which validations occur? What I want to do is, after all other modules have done their validations and everything is ok, only then should my function be called, in the end. My validation function should be final validation function. Is there any way to do it? Any help on this appreciated. Any document which explains how actually hooks are called would help too. Thanks, Pushya
I think you can make sure of this by defining the weight of your module as > 0. It will then be invoked after all core modules and all contrib modules with lower weights. See includes/module.inc#module_list to see how Drupal builds and orders the list of modules invoked in a module_invoke_all call. ----- Original Message ----- From: "Pushyamitra Navare" <pushyamitra@gmail.com> To: <development@drupal.org> Sent: Wednesday, September 03, 2008 11:25 AM Subject: Re: [development] Can I Indicate failure from hook_user? Hello there, On Mon, Jul 21, 2008 at 3:53 PM, Steven Jones <darthsteven@gmail.com<mailto:darthsteven@gmail.com>> wrote: Always good to see someone new to Drupal. You'll probably want to do something with the $op == "validate" argument of hook_user. Throw an error if you don't want the update/insert to continue. I am back with a question related to this. What is order in which hooks are called? For e.g. let's say there's a captcha module. Now as I see on my machine, hook_user is called first. After that, validation of captcha happens. Is there any way I can control the sequence in which validations occur? What I want to do is, after all other modules have done their validations and everything is ok, only then should my function be called, in the end. My validation function should be final validation function. Is there any way to do it? Any help on this appreciated. Any document which explains how actually hooks are called would help too. Thanks, Pushya
On Wed, Sep 3, 2008 at 3:08 PM, FGM <fgm@osinet.fr> wrote:
I think you can make sure of this by defining the weight of your module as
0. It will then be invoked after all core modules and all contrib modules with lower weights. See includes/module.inc#module_list to see how Drupal builds and orders the list of modules invoked in a module_invoke_all call.
Cool! Much simpler than I thought. Looking at code in module.inc I can see that one way to adjust weight is by changing value in system table in database. Just to confirm, is there other way to change weight for a module (like defining weight in module.info file etc.)? - Pushya
----- Original Message ----- From: "Pushyamitra Navare" < pushyamitra@gmail.com> To: <development@drupal.org> Sent: Wednesday, September 03, 2008 11:25 AM Subject: Re: [development] Can I Indicate failure from hook_user?
Hello there,
On Mon, Jul 21, 2008 at 3:53 PM, Steven Jones <darthsteven@gmail.com <mailto:darthsteven@gmail.com>> wrote: Always good to see someone new to Drupal.
You'll probably want to do something with the $op == "validate" argument of hook_user. Throw an error if you don't want the update/insert to continue.
I am back with a question related to this.
What is order in which hooks are called? For e.g. let's say there's a captcha module. Now as I see on my machine, hook_user is called first. After that, validation of captcha happens.
Is there any way I can control the sequence in which validations occur? What I want to do is, after all other modules have done their validations and everything is ok, only then should my function be called, in the end. My validation function should be final validation function. Is there any way to do it?
Any help on this appreciated. Any document which explains how actually hooks are called would help too.
Thanks, Pushya
On Wednesday 03 September 2008 06:47:21 Pushyamitra Navare wrote:
On Wed, Sep 3, 2008 at 3:08 PM, FGM <fgm@osinet.fr> wrote:
I think you can make sure of this by defining the weight of your module as
0. It will then be invoked after all core modules and all contrib modules
with lower weights. See includes/module.inc#module_list to see how Drupal builds and orders the list of modules invoked in a module_invoke_all call.
Cool! Much simpler than I thought.
Looking at code in module.inc I can see that one way to adjust weight is by changing value in system table in database. Just to confirm, is there other way to change weight for a module (like defining weight in module.info file etc.)?
- Pushya
No, there is no other way to change your module's weight. The standard practice is to change the module's weight in hook_install() (which also means writing a hook_update() if necessary). Sam
----- Original Message ----- From: "Pushyamitra Navare" < pushyamitra@gmail.com> To: <development@drupal.org> Sent: Wednesday, September 03, 2008 11:25 AM Subject: Re: [development] Can I Indicate failure from hook_user?
Hello there,
On Mon, Jul 21, 2008 at 3:53 PM, Steven Jones <darthsteven@gmail.com <mailto:darthsteven@gmail.com>> wrote: Always good to see someone new to Drupal.
You'll probably want to do something with the $op == "validate" argument of hook_user. Throw an error if you don't want the update/insert to continue.
I am back with a question related to this.
What is order in which hooks are called? For e.g. let's say there's a captcha module. Now as I see on my machine, hook_user is called first. After that, validation of captcha happens.
Is there any way I can control the sequence in which validations occur? What I want to do is, after all other modules have done their validations and everything is ok, only then should my function be called, in the end. My validation function should be final validation function. Is there any way to do it?
Any help on this appreciated. Any document which explains how actually hooks are called would help too.
Thanks, Pushya
On Wed, Sep 3, 2008 at 6:20 AM, Sam Boyer <drupal@samboyer.org> wrote:
On Wednesday 03 September 2008 06:47:21 Pushyamitra Navare wrote:
Cool! Much simpler than I thought.
Looking at code in module.inc I can see that one way to adjust weight is by changing value in system table in database. Just to confirm, is there other way to change weight for a module (like defining weight in module.info file etc.)?
No, there is no other way to change your module's weight. The standard practice is to change the module's weight in hook_install() (which also means writing a hook_update() if necessary).
And there's some great documentation on how to do this: http://drupal.org/search/node/update+module+weight+type%3Abook Or more specifically http://drupal.org/node/110238 Regards, Greg -- Greg Knaddison Denver, CO | http://knaddison.com | 303-800-5623 Growing Venture Solutions, LLC | http://growingventuresolutions.com
Quoting Greg Knaddison - GVS <Greg@GrowingVentureSolutions.com>:
On Wed, Sep 3, 2008 at 6:20 AM, Sam Boyer <drupal@samboyer.org> wrote:
On Wednesday 03 September 2008 06:47:21 Pushyamitra Navare wrote:
Cool! Much simpler than I thought.
Looking at code in module.inc I can see that one way to adjust weight is by changing value in system table in database. Just to confirm, is there other way to change weight for a module (like defining weight in module.info file etc.)?
No, there is no other way to change your module's weight. The standard practice is to change the module's weight in hook_install() (which also means writing a hook_update() if necessary).
And there's some great documentation on how to do this:
http://drupal.org/search/node/update+module+weight+type%3Abook
Or more specifically http://drupal.org/node/110238
Be a nice feature request for myMod.info to contain weight = 10 weight = taxonomy + 1 I'll do some further research. Earnie -- http://for-my-kids.com/ -- http://give-me-an-offer.com/
participants (8)
-
Earnie Boyd -
FGM -
Greg Knaddison - GVS -
John Fiala -
Nathaniel Catchpole -
Pushyamitra Navare -
Sam Boyer -
Steven Jones