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' => "<fb:like href={$url}></fb:like>",
      '#type' => 'markup',
      '#prefix' => '<div class="dff_like_wrapper">',
      '#suffix' => '</div>',
    );
  }
}


/**
 * 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,
                               ));
    }
  }
}

Comments

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.

www.icelark.com
Think.Act.Be
@togbonna
@icelark