The module is a great help. Thanks a lot! :-)

I am having a difficulty with a view I created using different pieces of a content type that are filtered by date. Is there a way to share this specific output? Or at least to share the "body" of the output (although I am not sure whether the link would then be to the nod of the content or to the nod of the view)

http://www.iskreno.hr/dnevnocitanje (the site is in Croatian, but I think you can get the point)

Thank you once more.

Comments

lolandese’s picture

This solution is independent from the Facebook Share module and supposes the use of URL aliases. I use the module for the single nodes and the code below for the views.

  • Enable first the Views Custom Field module.
  • Add a Customfield: PHP code to your view and use this code:
    <a name="fb_share" type="button" share_url="<?php
    $targetPath = "node/";
    $targetPath .= $data->nid;
    $targetPath = 'http://'.$_SERVER['HTTP_HOST'].base_path().drupal_get_path_alias($targetPath, $path_language = '');
    print $targetPath;
    ?>" href="http://www.facebook.com/sharer.php?u=<?php print $targetPath;
    ?>">Share</a>
  • Then, in the Basic Settings of your view, make a footer with input format full HTML with the following code:
    <script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script>

The result is a FB share button that points to the intended node for each displayed node in the view. Would be nice if you have an image in the node content. This will allow the user to display it together with some text on their FB stream.

A working demo.
More info on Facebook.
The credit for this solution goes to this node.

One thing I gave a quick look but didn't manage to do yet is this (quote from Facebook):

In order to make sure that the preview is always correctly populated, you should add the tags shown below to the <head> element in your HTML code. If you don't tag your page, Facebook Share will grab the title of your page as specified in the <title> tag, and will create a summary description from the first text that appears in the body of your page. A list of thumbnails will all be selected from available images on your page.

You can control the exact title, description, and thumbnail that Facebook Share uses by adding meta tags to the <head> element in your page.

These meta tags should of course be added to the <head> element of the node. Any suggestions?

If instead you want the share widget to show on nodes that use panel pages, go here.

summit’s picture

Subscribing, greetings, Martijn

benjifisher’s picture

Version: 7.x-1.3 » 6.x-1.0

Thanks for the explanation! I did it slightly differently:

  • I am already using a Views template, so I simply put the PHP code in there rather than using Customfield.
  • My View already had a Header but no footer, so that is where the <javascript> went.
  • I entered my pathauto alias "manually" instead of using drupal_get_path_alias.

I was going to say that $targetPath should be url-encoded, but Drupal seems to do that automatically. :)
For the Facebook-friendly <meta> tags, I have not tried anything, but Nodewords looks like the right tool, or maybe Simple Meta. Or you can edit your theme's node.tpl.php file.

lolandese’s picture

Off topic

Nodewords and Simple Meta don't support the Open Graph protocol meta tags YET (the one used by FB). Probably it's only a matter of time before at least Nodewords does (already a feature request there). For now I use a slightly tweaked version of this. It should be committed it in a real module. Ideal would be integration in the Nodewords module (or the newly under development Meta tags module).

rumblewand’s picture

Awesome! This worked for me (using drupal for facebook) to pass the Like button into my collection of frontpage content with a mix of nodes.

rickh’s picture

lolandese. thanks this works for nodes bit can you tell me how to odify the code for the My Accoutn page so to insert this into the users profile?

lolandese’s picture

@ rickh: Make a new block:

    • Block body:
    <a name="fb_share" type="button" share_url="<?php
    $options = array('absolute' => TRUE);
    $targetPath = url('user/'.drupal_get_normal_path(arg(1)), $options); print $targetPath; ?>" href="http://www.facebook.com/sharer.php?u=<?php print $targetPath; ?>">Share</a>
    <script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script>
      Input format: PHP
      Page specific visibility settings: Show only on the listed pages: user/*
      Put it in one of the top regions of your theme.
  • No need to use Views, just a plain block will do. With this code also the Share button of a single node can be handled (not a View that displays several nodes). In that case substitute user/ with node/.

    Don't forget to enable the optional core module "PHP filter" to have the PHP input format available.

    freeform.steph’s picture

    Version: 6.x-1.0 » 7.x-1.3

    If your site has multiple views, it would be beneficial to add a block that can be shared amongst those pages without having to add code in the header or footer of each view. In that case, as above, create a new block with display option set to specific pages, and then use the code above, changing:
    $targetPath = url('user/'.drupal_get_normal_path(arg(1)), $options); print $targetPath; ?>"
    to:
    $targetPath = url(drupal_get_normal_path(arg(0)), $options); print $targetPath; ?>"

    norman.lol’s picture

    Thanks #1, helped me a lot to find out how to use the 2013 code for a dynamic Facebook Share button (the one with the popup window), inside a view with a custom php field and its availabe variables. In the 'output code' field I then wrote:

    <a href="#" 
      onclick="
        window.open(
          'https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fmyfancysite.xl%2Fpath%2F<?php print $row->nid ?>', 
          'facebook-share-dialog', 
          'width=626,height=436'); 
        return false;">
      <img src="/sites/all/modules/facebookshare/images/button.png">
    </a>
    szt’s picture

    Assigned: poncije » Unassigned
    Issue summary: View changes