I need to redirect the user to his 'edit profile' page the first time he log in with facebook connect (and the local account is created), in order to let the user to provide more details about him that my site use (hobbyes, for examples), but this should happen only when the user is created.
Thanks to the various hooks implemented by fb_user, i've been able to do it with a custom module:
/**
* Implementations of hook_fb()
*/
function myfbconnect_redirect_fb($op, $data, $return){
switch($op){
case 'post_user':
if(isset($data['account']->uid)){
//force user login
$valid_user = user_external_login($data['account']);
//if login happen, redirect the user to the edit profile page.
if($valid_user){
$profile_url = sprintf('user/%d/edit', $data['account']->uid);
drupal_set_message(
t('Your facebook account is been sucessfully linked to @sitename!'
. '<br />Fill your profile details to complete the registration.',
array('@sitename' => $sitename)),
'status',
TRUE
);
//watchdog('myfbconnect_redirect', 'op.post_user: all done, fire drupal_goto');
drupal_goto($profile_url, 'destination=<front>');
}else{
watchdog(
'myfbconnect_redirect',
'op.post_user: user_external_login FAIL',
array(),
WATCHDOG_WARNING
);
}
}else{
ob_start();
var_dump($data['account']);
$details = ob_get_clean();
watchdog(
'myfbconnect_redirect',
'Unable to find uid, account data: <pre>!details</pre>',
array('!details' => $details),
WATCHDOG_WARNING
);
}
break;
}
}
I think will be a option usefull for many peopel, and i'll love to see it itegrated with the new version (maybe with field to customize the message)
Comments
Comment #1
Dave Cohen commentedGreat example, thanks.
You do realize, you're not forcing the new user to provide you any additional information. They could simply not fill out the profile form, then go on using your site. If you want to force them to fill out the form, configure fb_user.module to not create new accounts, and direct them to the user/register form. The account they create will be linked to their facebook account.
That's one reason there's no option like this in fb_user.module. The other reason is that different sites do such wildly different things in these cases; some bring up popups, some prefer to leave the user on the current page. As you point out, fb modules make it easy to customize with a little code. Perhaps one day there will be an add-on module that tries to make these options configurable with checkboxes, etc, but it's not my highest priority. (You, or anyone reading this, are welcome to create one!)
Comment #2
strae commentedYep i know, when the user get redirected to his edit page is already registered, but forcing to complete the profile is not my goal atm, i just need to highlight the other fields and let the user know that the drupal and facebook accounts are two different things.
Anyway, i guess if someone need to force the user to fillup additional profile fields in order to have a valid drupal account, this could easily be done with Rules ad Triggers modules
Comment #3
westis commentedLooks good. For someone who is not as acquainted with PHP, can you explain what this snippet is doing and how to modify it if for example I want a user to always be redirected after logging in with Facebook, not only when the account is created?
Comment #4
strae commentedSure, i attach the module im using, so you can download and try it... please note that im new to drupal and this module is been wrote in 5 minutes, so any suggestion is welcome.
I added some extra comments, should be more clear now.. anyway feel free to ask if you dont understand something.
For your case, if you need to redirect the user everytime he log-in with facebook, you should use another op (see the avaiables op into fb.module file, i dont remember exactly where it was after the login), login the user and then redirect wherever you like with drupal_goto
Comment #5
Dave Cohen commentedThis needs to be documented, at the very least.
Comment #6
strae commentedWhat you mean with "needs to be documented"?
By the way, im thinking to improve this feature adding:
1. The drupal_set_message customizable by the user (saving it into the db)
2. The ability to block the user account until he doesnt fillup a specific profile field
The second point could be problematic, i dont know if drupal allow the user situation "until the X field isnt filled and valid, the user can just edit his own profile". Maybe giving he a low role before and then switch to the desidered one whwn the field is filled, dont know yet.
As soon as i can i'll work on this.
Comment #7
Dave Cohen commentedWhen I say, "needs to be documented" I mean that new features don't need to be added, but a page on d.o needs to explain exactly how to do this with custom code.
once #877710: modules/fb support other languages and locales is fixed, the solution to this should be really easy. Something like...
As for the ability to not create a new account, you could do this by implementing hook_fb(), but the better way would be tell modules/fb never to create accounts. Instead let the user fill out drupal's registration form.
Comment #8
Dave Cohen commentedSo, it's not as simple as I thought in #7, because the user account is actually created during an ajax callback (at least usually).
See the last example on http://drupal.org/node/969242.
I'm open to suggestions on making this easier.
Comment #9
strae commentedWhere exactly is the problem?
I updated the code i user with the new hook (hook_fb_user) and it works, perhaps the ajax callbacks doesnt call user_external_login to force the user login before it get redirected?
Comment #11
adityagada commented1. The drupal_set_message customizable by the user (saving it into the db)
2. The ability to block the user account until he doesnt fillup a specific profile field
is the part 2 complete?
Comment #12
strae commented@adityagada: sorry but i didnt have much time to complete that.
I guess it could be easily achieved with a custom module that run those step:
Please note this is my first thought about how to do that, probably there are better way.