Illustration of Yeti character writing with a pencil.

ShareThis and D7 Views: If There's an API, There's a Way

Profile picture for user Tom
Tom Fleming
Senior Developer

Even without a Drupal 7 module to date, integrating ShareThis into Drupal 7 and Views is a relatively painless process, but one that requires a little ShareThis API tinkering.

Adding ShareThis to individual nodes is pretty simple: Go to the website, sign up for an account, grab the button and script code, and then place it into a node template file.

Things get a little more interesting, however, when Views is thrown into the mix. Instead of Twitter and Facebook links redirecting users to share the teasers of their choice, every shared link pulls from the Views page’s URL, making every ShareThis button link on the page the same.

That’s when ShareThis needs a little Drupal-fication using the ShareThis API.

ShareThis offers several APIs, but for the purpose of this article, you will want to use the Sharing API.

First, using any of ShareThis’s APIs requires an access key, which is offered free of charge. To request a key, log in and go to My Account > Publishers > Request Access Key.

ShareThis offers several parameters when using the Sharing API, but as far as Drupal is concerned, you only need to worry about these parameters: destination, url, pub_key, access_key and title.

Using the following template, it is now possible to make your own custom ShareThis link.

In order to get your custom ShareThis link to pull the URL from the node teaser in a Views row, as opposed to the whole Views page, you will need to add a little URL-encoded PHP to the template. Although you can follow the API for every other parameter, you will want to use Drupal variables to construct the URL and title parameters.

Our URL and title parameters looked like this:

url=http://www.example.com<?php print urlencode($node_url); ?>

title=<?php print urlencode($teaser); ?>

And that’s about it. More information on the ShareThis API can be found via the ShareThis website at https://developer.sharethis.com/.

comments powered by Disqus