tags:

views:

125

answers:

1

Hello,

I need to use Drupal 6's "hook_user" to update a 3rd party API whenever a user updates their profile.

Therefore I use the 'update' operation. The problem I am facing is that I just cannot see how I can stop execution if the 3rd party API update fails.

I.e. the user updates their username, but if the API fails, prevent Drupal from updating the local record.

function myhooks_user($op, &$edit, &$account, $category) {

    switch ( $op )
    {

        case 'update':

            if ( FALSE === updateAPI($data) )
            {
                drupal_set_message("Cannot update user information", "error", false);

                return false; 
            }

            break;
    }
}

At the moment, the return false doesn't stop execution.

+1  A: 

There is not a way to stop execution.

You should be able to overwrite $edit, with what's in the db. That way there wont be any change. I haven't tried this out, but it should work just fine.

Why do you want to do this anyways? You could just add a row in the db, and update the profile at a later time with cron instead, to avoid frustrated users that need to do the same edit over and over.

googletorp
Ok, that makes sense. I want to do it to maintain an external database, so that for example, when a user updates their information, the external db gets updated. Therefore, if the external update fails, I want to stop Drupal updating. I think I may use the 'validate' operation instead.
JonB
@JonB unless it's absolutely critical that your external DB is in sync with your Drupal db at all times, I would suggest creating a cron job as a backup instead that will try sync the db's until you succeed. It's normal to have cron jobs to run every 5 mins, so you should be fine. It's a lot more usable for the user, and you don't want to discourage users not keeping their profiles up to date. It's also fairly easy to create a cron job like that with hook_cron.
googletorp