Importing social media content without an account. Part two - Facebook pages

Update, June 2015: Unfortunately, Facebook deprecated RSS. The announcement says: "Page RSS Feed endpoint - at https://www.facebook.com/feeds/page.php is now deprecated and will stop returning data from June 23, 2015. Developers should call the Graph API's /v2.3/{page_id}/feed endpoint instead. This returns JSON rather than RSS/XML." (See https://developers.facebook.com/docs/apps/changelog#v2_3_90_day_deprecations). The tutorial below does therefore no longer work.

 

Twitter and Facebook are popular proprietary social network, and it is common to display Tweets or Facebook posts on a website. In the first post of this two-part series exploring how to render content from social media networks in Drupal without the requirement of a user account we described how you can get Tweets from either a Twitter account or a Twitter #hashtag. This second part looks at how to render posts from a public Facebook page into your Drupal website. In both cases without the need to have an account with either of these social networks.

 

Requirements

Install Feeds and related modules

Next we install the modules Feeds and Feeds Tamper and their dependencies, and enable all of them (including Feeds UI and Feeds Tamper UI). This can be done via a simple drush command or manually.

Prepare your content types

As outlined for Twitter in the first part of this series, for Facebook we also need two content types: one that is used for the Facebook feeds, and another one to store the Facebook posts itself. First, create a content type (which I called Facebook post with the following fields:

  • Title: we do not really need this... but Drupal can't do without it.
  • Body: is where we store the Facebook post itself.

For Facebook, you need to enable the img tag for the filtered html text format (and possibly iframe), if you want embedded photos and videos to show up in your feed.

Now we are ready to configure feeds to import our public Facebook posts. First we create the content type for our Facebook feeds (which I called Facebook Feed), with the following fields:

  • Title: We can give our Facebook feed a meaningful title
  • Feed. This is a field of content type feeds module form element and is automatically created when you attach a new Feeds Importer to this content type (see below). You can't create this field manually.

You do not need a Body field, unless you want to have the option to add a more detailed description to your feed item.

Create your Facebook feed importer

Go to admin/structure/feeds to create your feed importer for your Facebook feeds. Click on Add importer and enter the name of your importer (I called it Facebook feed importer) and a description, and click on Create. Now we need to configure the importer for our content types.

Basic Settings

 

Click on Settings. Under Attach to content type, select the Facebook Feed content type you created earlier. Next you need to adjust the settings under Node processor. Under Bundle select the content type your created for the individual posts (Facebook post). Check Update existing nodes, under Text format select Filtered HTML (or a custom text format accessible by the anonymous user), and uncheck Authorize. Under Expire nodes choose a timeframe that makes sense to you - if this is a busy Facebook page, a few days might be sufficient. For less busy accounts 1 month or 3 month might be appropriate. Click Save

Again we need to tamper with our data. Facebook feeds sometimes include links to Facebook profile picture albums in the format of https://www.facebook.com/media/set/?set=..., which we want to get rid of. Go to the Tamper tab at top. We go to Description -> Body and add the plugin called Find replace REGEX. Give it a meaningful name, and under "REGEX to find" enter /(<a href="https:\/\/www\.facebook\.com\/media\/set).*?(<\/a>)/. Leave the other fields empty, and save the plugin.

We also need to make URLs absolute again. Under Description -> Body add the plugin Make URLs absolute. Just add it - there is nothing to configure here. This is needed to make sure that links (to Facebook users or others) link to Facebook, and not to non-existent local URLs.

Add a Facebook feed

Facebook does not provide an easy link to know the feed URL of your page (or your account). So first we need to find out what the ID is. Luckily, there are web services that help us with this. For example, you can use http://findmyfacebookid.com/ to get the ID of your Facebook account or page. Just enter the URL of the page or account, and the website will return a number, which is your Facebook ID.

Now you can add a new Facebook feed. Go to Add Content and select the Facebook feed content type you created earlier. Enter a meaningful title. Under URL, enter the following URL: https://www.facebook.com/feeds/page.php?format=rss20&id=[facebookID]

You can now create a view to display your Facebook feed. You probably do not want to display the title - it is also included in the body.

Adding a view to display your Facebook posts

Facebook posts are easier to deal with than Tweets. As with Twitter, you most probably want a Block display of your Facebook posts. So create a view of content types Facebook post with a block display, and display of fields. You need to add the following fields in the following order:

  • Feeds Item: Item URL: This holds the URL to the post on Facebook page. Exclude this from display
  • Post date: This holds the date of the Facebook post. We output this one as a link under rewrite results, and link it to the above [url] field. We also tick the box "URL on external server"
  • Body: This holds the Facebook post itself.

We save our view and are now able to position the block where we want it.

This works fine when you only have one Facebook feed. If you have more than one, this view would display all tweets, independent from which Facebook feed they originate. To filter by Facebook feed, take a note of the nid of each feed (Facebook Feed node), and add a relationship to Feed Items: Owner feed. This allows you to then add a filter on Feed item: Owner feed nid, so that you can only display the Facebook feeds in the block that you're after.