Last updated 30 November 2010. Created on 6 October 2010.
Edited by stacywray, Dave Cohen. Log in to edit this page.

Stream Publish Dialog

In response to user action, we can pop up a dialog box that allows users to publish to their Facebook stream.

Required Modules

  • fb_stream.module

Configuration

In this example, we prompt the user to publish to their Facebook stream after they perform an action on Drupal (save a node or comment). Because the stream publishing options are relatively complex, we use PHP code to control it.

This example comes from fb_example.module, which is included in Drupal for Facebook. You'll find other examples of how to customize behavior there. In your own custom module, you'll want to replace every occurrence of "fb_example" with your module's name.


/**
 * Implements 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 fb_example_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_example']['stream_publish'] = array(
        '#type' => 'checkbox',
        '#title' => 'Share on Facebook',
        '#default_value' => TRUE,
      );
    }
    elseif ($form['form_id']['#value'] == 'comment_form') {
      // Add checkbox to control feed publish.
      $form['fb_example']['stream_publish'] = array(
        '#type' => 'checkbox',
        '#title' => 'Share on Facebook',
        '#default_value' => TRUE,
      );
    }
  }
}

/**
 * Implements hook_nodeapi().
 *
 * Publish to facebook Walls when users submit nodes.
 *
 * @see http://developers.facebook.com/docs/reference/rest/stream.publish 
 * @see http://developers.facebook.com/docs/guides/attachments
 */
function fb_example_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 ($picture = $GLOBALS['user']->picture) {
        $url = url($picture, array('absolute' => TRUE));
        $attachment['media'][] = array(
        'type' => 'image',
        'src' => $url,
        'href' => $url,
        );
        }
      */
      if ($logo_path = theme_get_setting('logo_path')) {
        $url = url($logo_path, array('absolute' => TRUE));
        //dpm($logo_path, "logo_path is $logo_path and url is $url");
        $attachment['media'][] = array(
          'type' => 'image',
          'src' => $url,
          'href' => $url,
        );
      }
      
      $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('message' => $user_message,
                                     'attachment' => $attachment,
                                     'action_links' => $actions,
                               ));
    }
  }

  // Another way to add like button, as part of a node.
  if ($op == 'view' && variable_get('fb_example_nodeapi_add_like', FALSE)) {
    $url = fb_scrub_urls(url('node/' . $node->nid, array('absolute' => TRUE)));
    $node->content['dff_like'] = array(
      '#value' => "",
      '#type' => 'markup',
      '#prefix' => '
', '#suffix' => '
', ); } } /** * Implementation of hook_comment(). * * Publish to facebook Walls when users submit comments. */ function fb_example_comment(&$a1, $op) { if ($op == 'insert' || $op == 'update') { if ($a1['stream_publish']) { //dpm($a1, "fb_example_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)))), ); if ($logo_path = theme_get_setting('logo_path')) { $url = url($logo_path, array('absolute' => TRUE)); //dpm($logo_path, "logo_path is $logo_path and url is $url"); $attachment['media'][] = array( 'type' => 'image', 'src' => $url, 'href' => $url, ); } $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('message' => $user_message, 'attachment' => $attachment, 'action_links' => $actions, )); } } }

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

Tafa’s picture

Hi,

I am currently using the FBSS module which emulates a user facebook wall. So far, it works great. I also installed drupalforfacebook which is also really good. Now, my question is:
Is it possible to link FBSS and stream together so that a user only has to update his FBSS wall and then have his or her status be uploaded or shared on his real Facebook wall?

Thanks
T

pribeh’s picture

@Tafa
The initial phase for the development of such integration started here: http://drupal.org/node/358852

I'm not sure if development has continued since Facebook's API changes and DFB's subsequent 3.x version update but you could check with mErilainen (who stated in that thread that he was working on a module for said functionality).

polishyourimage’s picture

If I enable this module will this example functionality be turned on? Can you please explain how I get this to work?

polishyourimage’s picture

for others who may be looking for an answer to this you may find the following helpful: http://drupal.org/node/946126#comment-3639128

FFFFFFFab’s picture

Hello,

Have you understood how to get this module works ? I've followed your link but there is not really informations on publishing feature.
What are the modules that we must activate ? I thought that fb.module and fb_stream are the only modules that we need, but it's not work for me. Have i missed something ?

Thanks,

Fab.

aidanlis’s picture

Drupal for Facebook is an API - it does not do things like this out of the box. If you want to enable the exact functionality that is described in this API example, you will need to create a custom module and copy the example code into it.

togbonna’s picture

I believe the line
...
if ($form['#id'] == 'node-form') {
...
should read something like:
if (substr($form['#id'], -9) == 'node-form') {
or
if ($form['#node_edit_form '] == TRUE) {.
on the fb_example_form_alter function, or the user will never get prompted or post submitted.

pontoffeltier’s picture

Ok, I tried for several hours now...

My goal is to post to a fanpage (not from my private account to the fp but from the fp itself as a status-like post) whenever I create a node on my drupal 7 site.
But nothin I tried worked properly: Somtimes the checkbox was missing, sometimes the content simply wasn't posted so now I'd like to start over with some questions :)

1. What modules need to be enabled?
2. What does the app need in order to work?
3. Where do I need to set what permission?
4. Anything else?

I'd really appreciate any help, even if it's just pointing me to the correct (and idiot-proof) documentation.

Thanks in advance ;)

Ben Thvedt’s picture

Hi. I have been trying to follow this example, creating the app, enabling the modules, but I can't get the checkbox to show. I did some investigating and in the example in hook_form_alter there is the line:

if (isset($GLOBALS['_fb']) && fb_facebook_user()) {
 ...

I did a dpm of $GLOBALS['_fb'] and fb_facebook_user(). The first is null and the second appears to bean empty result. What does this mean? Up until this step, setting up the app, and enabling the modules, everything seems to have gone smoothly so far. Can anyone help me before it's too late and I have to use some other module?