function fb_stream_nodeapi in Drupal for Facebook 6.3
File
- ./
fb_stream.module, line 44 - Support for Facebook's Stream API.
Code
function fb_stream_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
if ($op == 'insert' || $op == 'update') {
if ($node->fb_stream_do_post) {
$node_url = url("node/{$node->nid}", array(
'absolute' => TRUE,
));
$params = array(
'access_token' => $node->fb_stream_from_token,
'message' => $node->fb_stream_message,
'link' => $node_url,
'name' => $node->title,
'description' => $node->teaser,
'caption' => variable_get('site_name', ''),
'actions' => json_encode(array(
'name' => t('View More'),
'link' => $node_url,
)),
'method' => 'POST',
);
// Let third parties alter params.
$params = fb_invoke(FB_STREAM_OP_PRE_POST, array(
'node' => $node,
), $params, 'fb_stream');
if ($params['name']) {
// http://stackoverflow.com/questions/4652628/what-markup-is-allowed-in-the-description-of-a-facebook-feed-post
$params['description'] = strip_tags($params['description'], '<b><i><small><center>');
try {
$result = fb_graph($node->fb_stream_to . '/feed', $params, 'POST');
// ID returned to us is not a normal graph ID. We can't query it, but can build a permalink.
if ($result['id']) {
$exploded_result = explode('_', $result['id']);
$story_fbid = 'story_fbid=' . $exploded_result[1];
$id = 'id=' . $exploded_result[0];
$link = FB_FACEBOOK_BASE_URL . '/' . 'permalink.php?' . $story_fbid . '&' . $id;
drupal_set_message(t('Posted %node to <a href="!url" target="_blank">Facebook</a>.', array(
'!url' => $link,
'%node' => $node->title,
)));
}
} catch (Exception $e) {
$msg = t('Failed to post content to Facebook.');
drupal_set_message($msg, 'warning');
fb_log_exception($e, $msg);
}
}
else {
// Cannot post without link name. If here, implement hook_fb_stream() to ensure name is set.
drupal_set_message(t('Cannot post <a href=!link>%title (node/!nid)</a> to facebook. Post has no name.', array(
'!nid' => $node->nid,
'!link' => $node_url,
'%title' => $node->title,
)), 'warning');
}
}
}
}