Redirect after connect
Link that sends user to specific page
This example works if you know when you created the link or button where you want to send the user. So for example, put this link on a page where the user is not connected to Facebook. By clicking on the link, they will first be asked to connect, then sent to the destination page.
Just include the following markup. For example, you could place it on a node where the input format is PHP:
<?php
$url = url('path/to/somewhere', array('absolute' => TRUE, 'fb_canvas' => fb_is_canvas()));
?>
<a href="#" onclick="Drupal.settings.fb.reload_url='<?php print $url?>';FB.login(function(response) {}, {perms:Drupal.settings.fb.perms}); return false;">
Click here to continue
</a>
This works because by default after a login, the FB_JS.reload() function will run. And it looks to Drupal.settings.fb.reload_url to determine where to send the user.
Redirect user on all connect buttons
Required modules
- fb_app.module
- fb_user.module
- a custom module. In our example it is called "fb_example.module"
Setup and Configuration
In this example, we implement hook_fb, and respond to the FB_OP_AJAX_EVENT operation. This will be called via AJAX after user clicks connect. See fb.js for the code that triggers it, and fb.module for the server-side event handler.
Your module needs code as shown below. Replace the 'welcome' path with the welcome page of your choice.
function fb_example_fb($op, $data, &$return) {
$fb_app = isset($data['fb_app']) ? $data['fb_app'] : NULL;
$fb = isset($data['fb']) ? $data['fb'] : NULL;
if ($op == FB_OP_AJAX_EVENT) {
// We get FB_OP_AJAX_EVENT when fb.js calls us in reponse to a javascript event.
if ($data['event_type'] == 'session_change') {
// The user has clicked the connect button, or logged into/out-of
// facebook in another browser window, then refreshed.
if ($fbu = fb_facebook_user()) {
// The user has connected (as opposed to logged out). Let's redirect
// them to our 'welcome' page. Replace 'welcome' with the path you
// really want.
$url = url('welcome',
array('absolute' => TRUE, 'fb_canvas' => fb_is_canvas()));
// We return javascript to be evaluated by fb.js.
$return[] = 'FB_JS.reload("'. $url .'");';
}
}
}
}
Redirect user when a new account is created
In this example we redirect the user only when a new account is created. This works because fb_user.module responds to the same AJAX callback by creating a new account (if configured to do so).
Required modules
Same as above.
Setup and Configuration
This time we implement two hooks, hook_fb() and hook_fb_user() as follows.
function fb_example_fb_user($op, $data, &$return) {
$fb_app = isset($data['fb_app']) ? $data['fb_app'] : NULL;
$fb = isset($data['fb']) ? $data['fb'] : NULL;
if ($op == FB_USER_OP_POST_USER) {
// Set a global that can be checked in hook_fb, above. Note for this to
// work properly, fb_example.module must be weighted heavier then
// fb_user.module. (See fb_example.install).
$GLOBALS['fb_example_new_user'] = TRUE;
}
}
function fb_example_fb($op, $data, &$return) {
$fb_app = isset($data['fb_app']) ? $data['fb_app'] : NULL;
$fb = isset($data['fb']) ? $data['fb'] : NULL;
// Use devel module to figure out when this hook is called and what it is passed.
//dpm(func_get_args(), "fb_example_fb($op) called"); // debug
if ($op == FB_OP_AJAX_EVENT) {
// We get FB_OP_AJAX_EVENT when fb.js calls us in reponse to a javascript event.
if ($data['event_type'] == 'session_change') {
if (!isset($GLOBALS['fb_example_new_user'])) {
// The user has clicked the connect button, or logged into/out-of
// facebook in another browser window, then refreshed.
if ($fbu = fb_facebook_user()) {
// This is the same case as the earlier example. We could redirect them, or leave them on the current page.
}
}
elseif ($GLOBALS['fb_example_new_user']) {
// The user has clicked the connect button and that results in a new
// user being created. Note this will work only when fb_example.module
// has heavier weight than fb_user.module. See fb_example.install.
// Send the user to their edit page.
$url = url('user/' . $GLOBALS['user']->uid . '/edit',
array('absolute' => TRUE, 'fb_canvas' => fb_is_canvas()));
// We return javascript to be evaluated by fb.js.
$return[] = "FB_JS.reload('$url');";
}
}
}
}
Important note! For this example to work, our custom module, fb_example, must have a weight heavier than fb_user.module. This insures the hooks are called in the proper order. This can be accomplished with an fb_example.install file as follows.
// Set weight so that fb_example comes after fb_user.
function fb_example_install() {
// So we fall after og_vocab and og
db_query("UPDATE system SET weight = 3 WHERE name='fb_example'");
}
function fb_example_update_1() {
$ret = array();
$ret[] = update_sql("UPDATE system SET weight = 3 WHERE name='fb_example'");
return $ret;
}
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion