Timeline Activity on Drupal for Facebook

For a while now, the modules/fb/fb_graph.module has included some helper functions to publish user activity to a facebook timeline. And now this site is demonstrating those features.

Here are the activities that may appear on your timeline:

  • Post - when you post new content to our forums, a link to the post will appear in your timeline.
  • Edit - when you edit a forum post, it will appear in your timeline as an edit.
  • Comment - when you leave a comment, that will also appear in your timeline, with a link back to the original post you commented on.

To enable/disable this feature, click the "my account" link (left), then click "edit" and check/uncheck the "Publish to Timeline" checkbox.

To add features like this to your own website, it's a fairly involved process to register timeline actions. This is done through Facebook's developer application, and you should read their documentation thoroughly to understand it. You'll need to define both Graph items and Actions.

On the Drupal side, modules/fb/fb_graph.module will help you both markup your pages with Graph metadata, and publish activity. To get a sense of what's involved, here are two snippets of code, both from a custom hook_nodeapi() function.

When viewing a node, include Facebook Graph metadata:

<?php
 
if ($op == 'view' && $a4) {
   
// Full page view of a node.  Treat it as an article on facebook.
   
if (module_exists('fb_graph')) {
     
fb_graph_set_tag('og:type', 'article');
     
fb_graph_set_tag('og:title', $node->title);
     
fb_graph_set_tag('og:url', url('node/' . $node->nid, array('absolute' => TRUE, 'fb_canvas' => FALSE)));

      if (isset(
$GLOBALS['theme_key']) && ($logo_path = theme_get_setting('logo_path'))) {
       
// @TODO: does the node have its own image?                                                                          
       
$url = url($logo_path, array('absolute' => TRUE, 'fb_canvas' => FALSE));
       
fb_graph_set_tag('og:image', $url);
      }
    }
  }
?>

When creating/editing a node, publish the activity to facebook:

<?php
  
// This snippet relies on fb_graph.module to publish events to a user's timeline.
  // It will work only if you've configured your story nodes to be tagged as og:type article, and also you've configured post and edit actions.
 
if (($op == 'insert' || $op == 'update') &&
     
dff_custom_do_graph_publish($GLOBALS['user']) && // enable this only if you've defined read, edit and post actions.
     
$node->uid == $GLOBALS['user']->uid &&
     
$node->status &&
      (
$fbu = fb_facebook_user())) {
   
extract(fb_vars());
   
$url = url('node/' . $node->nid, array('absolute' => TRUE, 'fb_canvas' => FALSE));
    if (
$op == 'insert') {
     
$id = fb_graph_publish_action('post', array(
                                     
'article' => $url,
                                    ));
    }
    elseif (
$op == 'update') {
     
$id = fb_graph_publish_action('edit', array(
                                     
'article' => $url,
                                    ));
    }
  }
?>
Ratul Saha's picture

Dear Developer,

It is a very very powerful module that saved my life!

I wanted to know if there is any way to:

1. Adjust which content type to be published when adding? Is there a settings for that? (I saw the opengraph settings in the content type edit page, but it does not say if it will be automatically shared or not).

For example: How to say, I want users to share only when they create review content type, but not movie.

2. What about 'Read' actions? Those which are supposed to load when loading the page, I suppose.

Thank You.

Dave Cohen's picture

If you start with that snippet of code (above) as an example, a small tweak to the if clause can test the $node->type.

If you need technical assistance with that, it would probably be better to ask on the project issue queue.

Ashok Pundit's picture

Its a great tool for doing open graph integration.

Jimmy Catizone-Loughran's picture

Can't wait to try this out. Thanks!

Nils Jansen's picture

i would like to post a "fb-user-A just shared a tear for fb-user-B" activity to the users stream once when he published a node of the content type "tear". See here for an example tear: http://www.share-a-tear.com/nils-jansen-for-omi

Is it possible to make this work with og:type "shareatear:tear"?

Nils Jansen's picture

how do i trigger a javascript equivalent to the this curl command on node creation:

curl -F 'access_token=...' -F 'tear=http://www.share-a-tear.com/node-alias' 'https://graph.facebook.com/me/shareatear:share'

Entering this command in the linux terminal after node creation works perfectly fine...

Dave Cohen's picture

Drupal for Facebook initializes facebook's javascript library. So look at facebook's doc for full details. Something sort of like this should work...

FB.api(
    'me/shareatear:share',
    'post', // Despite facebook docs: get fails, post works.
    params, // other params if needed.
    function(response) {
      // if success, response.id will be a graph id.
      if (typeof(response.id) != 'undefined') {
        // it worked
      }
      if (typeof(Drupal.settings.fb_devel) != 'undefined' &&
          typeof(response.error) != 'undefined') {
        // troubleshooting
        //alert(response.error.message);
        debugger;
      }
    });
Miroslav Nikolov's picture

I use it now. Thanks!

Stojjan Borissov's picture

Very useful, thanks :)

Abdulaziz Aldaej's picture

Thanks

Leviticus Johnson's picture

Hi, thanks for the info on Drupal for Facebook. When someone logs in from Facebook, I want them to be able to create content like a blog post and so far that doesn't work. Also, where does this code go to enable someone to post to their timeline? ...in the modules/fb/fb_graph.module file?

Thanks, I'm a newbie!

Jérôme Chesne's picture

I dont know why my var $fbu = fb_facebook_user() is null, i log into my website with facebook login but the variable seems not be initialized.
WHat is the pb, i have may miss somtehing but i dont know why, can you tell me what can be wrong plz ? (i work in localhost)

Dave Cohen's picture

For a problem like this, post your question on the project issue queue. It's easier to learn which version you're using and track the resolution that way.