Posting a Node Gallery to a Facebook Wall
This example allows you to post your gallery to a Facebook Wall using the Facebook Stream Publish Dialog
First you will need to setup Drupal for Facebook http://drupal.org/project/fb with (fb_stream.module) enabled, which I wont go into as this is already well documented, but in short you will need to setup a Facebook app and configure Drupal for Facebook accordingly.
You will then need to create a custom module in fb/contrib which I've called fb_page_stream.module as it's posting to a page wall. Most of the code is already provided in the fb_example module, I've just adapted it to use with Node Gallery.
function fb_page_stream_form_alter(&$form, $form_state, $form_id) {
// Add stream publish option.
if (isset($GLOBALS['_fb']) && fb_facebook_user()) {
if ($form['#id'] == 'node-form') {
// Add checkbox to control feed publish.
$form['fb_page_stream']['stream_publish'] = array(
'#type' => 'checkbox',
'#title' => 'Share on Facebook',
'#default_value' => (empty($form['nid']['#value'])),
);
}
}
}
The first part of the module adds a tick box to your node so you can choose if you want to post Facebook, in my case it defaults to ticked for new content as for most other content types this is ideal, but not really for Node Gallery as you don't want to post to Facebook until you added your images. I guess you could change this behaviour based on content type, i've just not got round to it.
function fb_page_stream_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
if ($op == 'insert' || $op == 'update') {
if (isset($node->stream_publish) && $node->stream_publish) {
$attachment = array(
'name' => $node->title,
'href' => url('node/' . $node->nid, array('absolute' => TRUE)),
'description' => filter_xss($node->teaser, array()),
);
if ($node->type == 'events') {
$image = url(
$node->field_event_image[0][filepath],
array('absolute' => TRUE)
);
}
elseif ($node->type == 'node_gallery_gallery') {
if (arg(2)=='edit' && arg(0)=='node') {
$node = node_load(arg(1));
$cover_image = node_load($node->cover_image);
$image = url(
$cover_image->field_node_gallery_image[0]['filepath'],
array('absolute' => TRUE)
);
}
}
$attachment['media'][] = array(
'type' => 'image',
'src' => $image,
'href' => $image,
);
$actions = array();
if ($node->type == 'events') {
$actions[] = array(
'text' => t('More info & Tickets'),
'href' => url('node/' . $node->nid, array('absolute' => TRUE)),
);
}
elseif ($node->type == 'node_gallery_gallery') {
$actions[] = array(
'text' => t('View all photos'),
'href' => url('node/' . $node->nid, array('absolute' => TRUE)),
);
}
fb_stream_publish_dialog(array('message' => '',
'attachment' => $attachment,
'action_links' => $actions,
'actor_id' => 'xxxxxxxxx',
));
}
}
}
In the last part of the module you'll see I've split how $image is got, based on node type, you don't have to have an image when posting to Facebook but is nice to add a event image or in this case the Album cover image. When you then save your Gallery with the tick boxed checked and you images added, you will get a Facebook dialog box with your Cover image and, album title, submit this and it will appear on your wall and link back to your site.
In this example you will notice 'actor_id' => 'xxxxxxxxx'. Replace the xxx with the ID of your page to post to your page wall, otherwise remove this line to post to your profile wall.
Also you will need to make sure you have the right Facebook perms i.e Post to Wall, and Manage Page if posting to a Page.
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