Little modification of code to use the fb_stream_publish_dialog()
Hi guys,
First of all thank's for the module, is really fun. ;)
I applied some changes on your code published on http://drupal.org/node/685320 .
I posted the little modification as a comment.
Repair a little bug that not show the teaser on streams, Drupal use html text and Facebook today only accept plain text. Strip_tags function correct this easyly. ;)
Added too a imagefield on content to show an image on the streams, same as most popular facebook applications. Can see how looks on the next image.
http://www.miquelcarol.com/sites/default/files/d4fwall.jpg
· I have some suggestion, to implement this code as a module on future versions.
Url to site don't works well on my opinion. On clicks the users go out of facebook. Works more well when links to app.facebook path, I'm working to correct it.
· I deleted the text Check out my latest..., to looks more fun on streams. The users usuarlly only need put a comment one time.
· I'm working too to put tags links on 'properties' at nodestreams.
<?php
/**
* 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' => strip_tags($node->teaser),
'media' => array(array('type' => 'image', 'src' => url(($node->field_imagefb[0]['filepath']), array('absolute' => TRUE)), 'href' => url('node/' . $node->nid, array('absolute' => TRUE)))),
);
$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' => strip_tags($a1['comment']),
'media' => array(array('type' => 'image', 'src' => url(($node->field_imagefb[0]['filepath']), array('absolute' => TRUE)), 'href' => url('node/' . $node->nid, array('absolute' => TRUE)))),
'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,
));
}
}
}
?>- Forums:

THanks for sharing the code.
THanks for sharing the code. It is easier to read on drupal.org, the link you provided.
I haven't tried to make a module for this because I think most sites will want to do it a little differently. But maybe fb_stream.module will grow to include something like this, someday.
Improvements
Thanks, this post very helped me, I almost done module about this, just i wanted to post on app or group wall using target_id, but unsucessful fb it not allow, i am experienced programmer but very new on drupal and facebook api.
1. Function strip_tags very helped, but it strip new line tags, then if i have few new lines (using it on jokes website - jokes sometimes is dialogues) then getting not nice teaser on fb, then i improved it adding some symbol before new line:
function _lh_fb_publish_prepare_content($text, $new_line = ' '){
// lets add spaces on new lines to fix content before posting
$pattern = array(
'</p>',
'</li>',
'<br />');
$replace = array(
$new_line.'</p>',
$new_line.'</li>',
$new_line.'<br />');
$content = str_replace($pattern, $replace, $text);
return strip_tags($content);
}
Then, before each new line of text is added a symbol, so words from different lines of text does not combine together.
2. Another problem then if to press "read more" it open website in same window, it can annoy fb users, good to make then it opened it in new window, just i am unsure its possible?