I'm not code-smart, so I apologize in advance if this is obvious. But is it possible to pull Facebook like data into views? Right now I'm using the flag plugin with views to allow users to flag nodes as recommended, but I would like to be able to have them use the Facebook "like" button only, and then let me use that data in views (ie. show counts of how many people like something, create a block showing the most recently "liked" nodes, etc)

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ferdi’s picture

Great idea. Here is what I think could work: Make the like button a cck field, integrate it with some existing drupal voting modules (i.e. votingapi) and intercept the click using fb javascript api (http://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe). Hope I find some time to try this out in the next couple of days.

jdblank’s picture

Hi, I also have a need to be able to display the like button on individual items in a list from a view. Did you have any luck getting this to work?

egarias’s picture

My problem is with comments.
When using the comments, they are not accesible throught views, only the old comments (no Facebook) are shown.
My question is how can i access those comments, maybe some php code?.
Will you plan to integrate comments with views if they are not?
Thanks
Enrique

ferdi’s picture

The original ticket is about integrating number of fb likes with views. There is one thing that makes it hard to implement. Currently facebook allows users to undo their "like" (unlike) but there is not a javascript event associated with unlike.

@jdblank you can use views already to show node teasers which contain the like widget.

@egarias Facebook comments are not saved locally in the database. Please open a new ticket if you are asking for integration of fb comments with drupal.

liquidcms’s picture

although yes, you can do your View as a list of teasers and therefore show the like button that way; ideally we could make field views (as they are much more powerful than teaser views) and would then have a "field" that was the like button that we could add.

walden’s picture

@liquidcms I totally agree with you, it would be incredibly useful to be able to access the FB like button as a field in views.

Has anyone come up with an interim solution besides using the teaser style?

eyenology’s picture

subscribe

ferdi’s picture

Here is some code that integrate fb like button with views i.e. fb like will be one of the available fields in your view. http://drupal.org/cvs?commit=410224 .It would be great if someone can test it.
Thanks.

liquidcms’s picture

i'll test. any chance you can post a zip or something a little more accessible that whatever the link above goes to? :)

liquidcms’s picture

hmm.. ok, i think you are just saying it is the latest dev release as it looks like that has the views.inc file

i'll test first thing in the morning. thanks a ton.

liquidcms’s picture

uh nope, doesn't work.. as far as i can tell anyway.

to start with this breaks the attachment i had that was adding the teaser view to my fields view (but if this worked; no big deal)

i see when i am adding a field and trying to filter the list that there is a fb social button listed at the end (this is the actual field though and should be the group "FB Social" or something like that). selecting this does not do any filtering.

but without filtering i can still see the fb social button listed at the bottom... when i add the field i don't get any thing added to my fields list BUT i do see the label "fb social like button: " added to my output.

ferdi’s picture

It should provide the functionality described in #5 and #6 . Here is a simple view that i'm using for testing. Import & save the view and then visit yoursite.com/fbviewtest

$view = new view;
$view->name = 'fb_social_view_test';
$view->description = '';
$view->tag = '';
$view->view_php = '';
$view->base_table = 'node';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->override_option('fields', array(
  'fb_social_like' => array(
    'label' => '',
    'alter' => array(
      'alter_text' => FALSE,
      'text' => '',
      'make_link' => FALSE,
      'path' => '',
      'alt' => '',
      'link_class' => '',
      'prefix' => '',
      'suffix' => '',
      'target' => '',
      'trim' => 0,
      'max_length' => '',
      'word_boundary' => 1,
      'ellipsis' => 1,
      'strip_tags' => 0,
      'html' => 0,
      'help' => '',
    ),
    'empty' => '',
    'hide_empty' => 0,
    'empty_zero' => 0,
    'link_to_node' => FALSE,
    'fb_social_like_layout_style' => 'button_count',
    'fb_social_like_show_faces' => 0,
    'fb_social_like_width' => '350',
    'fb_social_like_verb' => 'like',
    'fb_social_like_font' => 'verdana',
    'fb_social_like_color' => 'dark',
    'exclude' => 0,
    'trigger_field' => 'trigger_field',
    'popup' => '',
    'caption' => '',
    'rel_group' => 1,
    'custom_group' => '',
    'height' => '400px',
    'width' => '600px',
    'id' => 'fb_social_like',
    'table' => 'fb_social_like',
    'field' => 'fb_social_like',
    'relationship' => 'none',
    'override' => array(
      'button' => 'Override',
    ),
  ),
  'title' => array(
    'label' => 'Title',
    'alter' => array(
      'alter_text' => 0,
      'text' => '',
      'make_link' => 0,
      'path' => '',
      'link_class' => '',
      'alt' => '',
      'prefix' => '',
      'suffix' => '',
      'target' => '',
      'help' => '',
      'trim' => 0,
      'max_length' => '',
      'word_boundary' => 1,
      'ellipsis' => 1,
      'html' => 0,
      'strip_tags' => 0,
    ),
    'empty' => '',
    'hide_empty' => 0,
    'empty_zero' => 0,
    'link_to_node' => 0,
    'exclude' => 0,
    'id' => 'title',
    'table' => 'node',
    'field' => 'title',
    'relationship' => 'none',
  ),
));
$handler->override_option('access', array(
  'type' => 'none',
));
$handler->override_option('cache', array(
  'type' => 'none',
));
$handler = $view->new_display('page', 'Page', 'page_1');
$handler->override_option('path', 'fbviewtest');
$handler->override_option('menu', array(
  'type' => 'none',
  'title' => '',
  'description' => '',
  'weight' => 0,
  'name' => 'navigation',
));
$handler->override_option('tab_options', array(
  'type' => 'none',
  'title' => '',
  'description' => '',
  'weight' => 0,
  'name' => 'navigation',
));

thanks for testing !

ferdi’s picture

Version: 6.x-1.0-beta1 » 6.x-1.x-dev
minus’s picture

Thank you very much for the effort you have been putting into this module ferdi!!

I tested your view, it gives me a list of nodes and a like button for each field. I tried to sort this view by the number of times the like button had been clicked, but there are no options for that in my sort options list.

A nice feature would be a statistics option, so that you can create a block with most liked, most shared and most commented (if this is possible it would be really nice.)

Node title (89) where 89 is the number of users who has clicked the liked button (I guess you knew what I ment :p)

Morten

liquidcms’s picture

yay.. it does seem to work!! it doesn't show in the Views preview but once i saved; it does show on the site.

thank-you.. this sure helps a lot.. :)

liquidcms’s picture

although, for some odd reason the colour selector when adding the button as a views field doesn't do anything. i mean i can select it and see the options.. but picking one doesn't change it from the default of Dark.

lee20’s picture

The like button doesn't "like" the right URL. Here is the diff that fixes this:

Index: sites/all/modules/fb_social/modules/fb_social_like/views/fb_social_like_handler_field_like.inc
===================================================================
--- sites/all/modules/fb_social/modules/fb_social_like/views/fb_social_like_handler_field_like.inc      (revision 169)
+++ sites/all/modules/fb_social/modules/fb_social_like/views/fb_social_like_handler_field_like.inc      (working copy)
@@ -67,7 +67,7 @@
   function render($values){

     $attrs = array(
-        'href' => fb_social_url($values->nid),
+        'href' => fb_social_url('node/' . $values->nid),
         'layout' => $this->options['fb_social_like_layout_style'],
         'show_faces' => $this->options['fb_social_like_show_faces'] ? 'true' : 'false',
         'width' => $this->options['fb_social_like_width'],
chia’s picture

subscribe

alexmartin’s picture

Thanks for your work on this Ferdi. I've got this working using the view supplied above. However, I'd also like to display the 'share' button, share count and comment count in the view (comment count being the most important to me). Is there any way you know to achieve this?

Thanks again,
Alex

Roavei’s picture

Works like a charm. But there is missing a "exclude from display" checkbox :|

Amir Simantov’s picture

Hi guys,

It seems that you are dealing here mostly with Like button, right? What I need is to show the fb comments in a view when using the node row style. Is there any solution for this?

Great module, thanks!

rumblewand’s picture

Hey everybody I did some digging and found #870262: Using FS with Views for a diff mod that enabled me to pass the php code into a views field so that the fb:like like button is posted AND the appropriate node address is passed to it.

I'm using Drupal for Facebook and I was making a frontpage view of events and general we posts. Works like a charm!>

rumblewand’s picture

Oh and @Amir you can probably adjust the code in that example to use the xfbml for comments. You might want to open an issue over at the drupal for facebook module.

Oscar Illada’s picture

Version: 6.x-1.x-dev » 6.x-1.0-beta5

I was looking for the functionality too, to put in a view the top liked nodes. Is there a possibility?

Oscar Illada’s picture

vin247’s picture

I think the code by lee20 should get more notice (if it hasn't already)...I was having trouble with the like button when used in a view (it wasn't outputting the link correctly) and adding the line: 'href' => fb_social_url('node/' . $values->nid), it now works perfectly!

ferdi’s picture

#17 is committed http://drupal.org/cvs?commit=473098. Thanks!

YK85’s picture

subscribing - is there any further development happening here or is this status 'fixed'?

ferdi’s picture

we dont have anything better than #25 for now.

Júlio Boaro’s picture

Subscribe

dafeder’s picture

Version: 6.x-1.0-beta5 » 7.x-2.0-alpha4

Here's one solution for 7.x. Add an "nid" field to your view, as well as a Content Type field that is hidden. Copy views-view-field.tpl.php template to your theme directory, and add the following to template.php, correcting for theme, fb_social preset, and view names:


function your_theme_preprocess_views_view_field(&$vars) {
  $view = $vars['view'];
  $field = $vars['field'];
  // In the media_detail view, replace nid field with a fb like button
  if (module_exists('fb_social') && ($view->name == 'your_view') && $field->field == 'nid') {
    // Create a "pseudo-node" object to send to fb_social_like_link
    $row = $vars['row'];
    $pnode = new stdClass();
    $pnode->status = 1;
    $pnode->type = $row->node_type;
    $pnode->nid = $row->nid;
    fb_social_fb_plugin_load('like');
    $preset = fb_social_preset_load('your_preset');
    $link = fb_social_like_link($preset, 'node', $pnode);
    $vars['output'] = $link['fb_social_like_media_like']['title'];
  }
}

I'm sure something similar could be done for 6.x.

Would of course be nice to have a field available right in views. Wouldn't be too hard but don't have the hours to devote to it at the moment, as this is working well for me for now.

acouch’s picture

Version: 7.x-2.0-alpha4 » 7.x-2.x-dev
FileSize
18.24 KB

I created a views handler for this which adds a global field that allows you to add an already created preset.

Unfortunately if it is being displayed on a page where it is referring to another page (on a list of teasers for example) it requires the nid for the node in the row. The nid is only available if some field from the node column is added. So this will work on a teaser as long as there is a node title, body, teaser, field etc.

That limitation seems like a blocker however for a module that may have a lot of new users using it. I'm attaching anyway in case someone wants to use this or it doesn't seem as bad a limitation as I fear.

kiranjyoth’s picture

I tried to do this, but i am getting some errors. May be you could help me. Here are the steps i followed.

1. Copied views-view-field.tpl.php from views module's theme folder to my theme directory
2. Edited and added the code that you provided to the template.php file. I changed the theme name, view name and preset name ( in this case, my preset name is 'like')
3. Added nid field to the fields
4. added a content type field and excluded it from display.

I get the following error message

Notice: Undefined property: stdClass::$node_type in corporateclean_preprocess_views_view_field() (line 75 of /home/devnode/public_html/sites/all/themes/corporateclean/template.php).
Notice: Undefined index: fb_social_like in corporateclean_preprocess_views_view_field() (line 80 of /home/devnode/public_html/sites/all/themes/corporateclean/template.php).
Notice: Undefined property: stdClass::$node_type in corporateclean_preprocess_views_view_field() (line 75 of /home/devnode/public_html/sites/all/themes/corporateclean/template.php).
Notice: Undefined index: fb_social_like in corporateclean_preprocess_views_view_field() (line 80 of /home/devnode/public_html/sites/all/themes/corporateclean/template.php).

Can you help me with what I am doing wrong here?

acouch’s picture

I have this fixed but haven't updated this yet. Will do so shortly.

kiranjyoth’s picture

Please do! Thanks for the quick response!

acouch’s picture

Attached is a patch that provides views support for users and node tables. I fixed the alias issue I was having and re-rolled versus the recent commits.

The url changes for each node or user for like, facepile, comments, and send plugins (the only plugins that wil accept the href option). If one of those is not chosen it just displays the plugin as normal.

This could be extended to other entities like taxonomy etc if requested.

acouch’s picture

Status: Active » Needs review

Please give it a test.

wiherek’s picture

tried it, it works with comments (yeah! :)). The patch does it all, theres no need for the solution from #31 (which I couldn't reproduce)
Issues found:

1. MAJOR - there is no views field for the comments preset, or any other fb social preset, which made me have to render the views by 'content' which is annoying if only used on a blog, and a 'blockerer' when used with more specific views

2. when the content was rendered without showing links or comments, and the window width was somewhat narrow (??), this error showed up:
Notice: Undefined variable: nr_comments in _fb_social_comments_comments_count() (line 235 of /(...)/sites/all/modules/fb_social/plugins/fb_plugin/comments.inc).

except for that it works fine. By the way, I added a href field for the like button some time ago (very simple). will add that to git sometime soon.

acouch’s picture

hi @wiherek5

Thanks for your comments. For:

| 1. MAJOR - there is no views field for the comments preset, or any other fb social preset,

The steps for this to work with comment #36 are to:

1) Create the fb_social preset which you wish to use in a view.

2) Create a view with 'content' or 'user' as the base table.

3) Add the "Facebool Social" preset field which will be in the "Node" or "User" group in the view.

For:

| 2.

That is an issue unrelated to views support. I've created a separate issue #1371416: Undefined comment property in _fb_social_comments_comments_count()

kiranjyoth’s picture

#36 @acouch, This patch worked for me. Thanks!

wiherek’s picture

@acouch: I still can't see the field.

What I did was: just installed the module, without following #31.

I create the view, showing content of type 'article', created as a page. I save and get to the view edit screen. The default way that view is rendered is 'content', which doesn't allow adding new fields (it uses the fields from the content type). That way, the fb_social is shown, I can add comments.
However, if I change the way the view is rendered to 'fields', and try to add a new field, there is no "Facebook Social" preset field neither in the 'node' group, nor 'user' (when creating a view showing users).

This is views3.

ferdi’s picture

Status: Needs review » Needs work

Is plugins/views/fb_social_handler_field.inc missing in #36?
Instead of changing the signature for fb_social_preset_view(), can we use theme_fb_social_plugin ?

acouch’s picture

Status: Needs work » Needs review
FileSize
4.62 KB

@ferdi, you are correct, patch in #36 was missing that file. I rerolled.

For using theme_fb_social_plugin() vs. fb_social_preset_view() I would argue that since we adding data to the output that that should happen before the theme function if possible.

@wiherek5 can you re-try? Thanks!

wiherek’s picture

it works now. thanks!

ferdi’s picture

@acouch

How about something like this? Adding an url argument to fb_social_preset_view() feels a bit strange so I'm trying to avoid that.

acouch’s picture

That looks good. I noticed that you missed the fb_social.views.inc in your patch so I re-rolled.

ferdi’s picture

ferdi’s picture

Status: Needs review » Fixed

Use fb_social_url to construct the 'href' attribute http://drupalcode.org/project/fb_social.git/commit/8ecdb9c

And backported for 6--2 branch http://drupalcode.org/project/fb_social.git/commit/5e680af

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

heyyo’s picture

Is there a filter to content which has been liked or commented ?

heyyo’s picture

Status: Closed (fixed) » Active
raidcha’s picture

Version: 7.x-2.x-dev » 6.x-2.x-dev

Hi

Like heyyo, i'm interested in a view sort criteria or filter to get the most commented or liked node