On this page
Publishing to Facebook Wall
This page was originally written for the 2.x branch of Drupal for Facebook. You're probably using the 3.x branch (or later). The 3.x branch includes a file, modules/fb/contrib/fb_example.module, with code showing the updated API for fb_stream_publish_dialog(). Some of the information that follows is out of date.
Facebook provides a Stream API for publishing content to "streams". These streams include user and page Walls, and also allows you update a user's status. Read Facebook's documentation to get a sense of everything that's possible using these APIs.
Drupal for Facebook includes a module, fb_stream.module, to simplify publishing to Facebook Streams.
fb_stream_publish_dialog()
The fb_stream_publish_dialog() function presents the user with a dialog box. This form allows them to make edits to the content before it is published to their wall, or to skip publishing entirely.
The parameters expected by fb_stream_publish_dialog() closely mirror the parameters expected by FB.Connect.streamPublish (Connect pages) and Facebook.streamPublish (Canvas pages). Consult that documentation for complete details. Note that fb_stream_publish_dialog() function for both Connect and Canvas page apps.
You may call fb_stream_publish_dialog() from any of Drupal's hooks. Use PHP code to compose the messages and control when they are published. Below is an example, from a module called dff_custom. This code is used on the drupalforfacebook.org forums.
/**
* Implementation of hook_form_alter.
*
* Adds a checkbox to node edit and comment forms. This checkbox lets
* facebook users know that content may be published to their Wall,
* and gives them a chance to prevent that.
*/
function dff_custom_form_alter(&$form, $form_state, $form_id) {
if (isset($GLOBALS['_fb']) && fb_facebook_user()) {
if ($form['#id'] == 'node-form') {
// Add checkbox to control feed publish.
$form['dff_custom']['stream_publish'] = array(
'#type' => 'checkbox',
'#title' => 'Share on Facebook',
'#default_value' => TRUE,
);
}
else if ($form['form_id']['#value'] == 'comment_form') {
// Add checkbox to control feed publish.
$form['dff_custom']['stream_publish'] = array(
'#type' => 'checkbox',
'#title' => 'Share on Facebook',
'#default_value' => TRUE,
);
}
}
}
/**
* Implementation of hook_nodeapi().
*
* Publish to facebook Walls when users submit nodes.
*/
function dff_custom_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
if ($op == 'insert' || $op == 'update') {
if (isset($node->stream_publish) && $node->stream_publish) {
//dpm($node, "dff_custom_nodeapi, publishing to stream");
// http://wiki.developers.facebook.com/index.php/Attachment_(Streams)
$attachment = array(
'name' => $node->title,
'href' => url('node/' . $node->nid, array('absolute' => TRUE)),
'description' => $node->teaser,
);
$user_message = t('Check out my latest post on !site...',
array('!site' => variable_get('site_name', t('my Drupal for Facebook powered site'))));
$actions = array();
$actions[] = array('text' => t('Read More'),
'href' => url('node/'.$node->nid, array('absolute' => TRUE)),
);
fb_stream_publish_dialog(array('user_message' => $user_message,
'attachment' => $attachment,
'action_links' => $actions,
));
}
}
}
/**
* Implementation of hook_comment().
*
* Publish to facebook Walls when users submit comments.
*/
function dff_custom_comment(&$a1, $op) {
if ($op == 'insert' || $op == 'update') {
if ($a1['stream_publish']) {
//dpm($a1, "dff_custom_comment, publishing to stream");
$node = node_load($a1['nid']);
// http://wiki.developers.facebook.com/index.php/Attachment_(Streams)
$attachment = array(
'name' => $a1['subject'],
'href' => url('node/' . $a1['nid'], array('absolute' => TRUE, 'fragment' => 'comment-' . $a1['cid'])),
'description' => $a1['comment'],
'properties' => array(t('In reply to') => array('text' => $node->title, 'href' => url("node/" . $node->nid, array('absolute' => TRUE)))),
);
$user_message = t('Check out my latest comment on !site...',
array('!site' => variable_get('site_name', t('my Drupal for Facebook powered site'))));
$actions = array();
$actions[] = array('text' => t('Read More'),
'href' => url('node/'.$a1['nid'], array('absolute' => TRUE)),
);
fb_stream_publish_dialog(array('user_message' => $user_message,
'attachment' => $attachment,
'action_links' => $actions,
));
}
}
}
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