Building a Drupal Block for Google Plus Activities

social networks

As we said in Customizing a Theme for Drupal 7 - Chapter 2, we are going to build a block for displaying the last ten activities of the author in Google Plus. According to the documentation provided by Google, An activity is a note that a user posts to their stream. Activity methods enable your application to list a collection of activities, get an activity and search through activities.

What we shall be covering throughout this little post is how to build a Drupal block for containing a collection of these activities. Let's get started...

The first thing to do is download the PHP client library for Google Plus and create an API key to identify the project and be able to access the Google Plus API. This is what Google calls Simple API Access. Since the Drupal block we shall be building does not need to access any user data, this level of access will be enough to build our block.

Once you download the PHP client library, create a folder inside the folder for the current theme (sites/all/themes/cti_flex), unzip the file into it and enable a module called PHP Filter. This module enables you to embed php code to be evaluated. Finally, create a block with the following content:

<?php

/**
 * First of all we put apiClient.php and apiPlusService.php
 * into the script. This allows Google to be able to identify our app
 * and lets us use the methods for listing the activities.
 */

require_once DRUPAL_ROOT . '/sites/all/themes/cti_flex/googleplus/'
. 'google-api-php-client/src/apiClient.php';

require_once DRUPAL_ROOT . '/sites/all/themes/cti_flex/googleplus/'
. 'google-api-php-client/src/contrib/apiPlusService.php';

// Now we create a client object and store it by using a variable.

$client = new apiClient();

// We set the name for the application and our developer key.

$client->setApplicationName('enter here the application name');

$client->setDeveloperKey('enter here your developer key');

// We create the object that allows us to use the methods for
// listing the activities.

$plus = new apiPlusService($client);

// We set 10 as the maximum number of activities to be displayed.

$optParams = array('maxResults' => 10);

// Now we pass listActivities our client id, the type of posts to
// be retrieved and the array named optParams.

$activities = $plus->activities->listActivities('enter here your client id',
 'public', $optParams);

// We start writing the <div> block for our activities.

$activityMarkup = "<div id='activities'>";

date_default_timezone_set('Europe/Madrid');

foreach ($activities['items'] as $activity) {

  // These fields are currently filtered through the PHP sanitize filters.
  // See http://www.php.net/manual/en/filter.filters.sanitize.php

  // We format the output to display the date and time properly.

  $published = filter_var($activity['published'], FILTER_SANITIZE_STRING,
   FILTER_FLAG_STRIP_HIGH);

  $published = strtotime($published);

  $published = date("D, m/d/Y - G:i", $published);

  $content = $activity['object']['content'];

  $activityMarkup .= "<div class='activity'><div
   class='activityDate'>$published</div><p>$content</p></div>\n";

}

$activityMarkup .= "</div>";

// If activityMarkup is set, print it.

if (isset($activityMarkup)) {

  print $activityMarkup;

}
?>

If the fatal error Uncaught exception: 'apiCacheException' with message 'Could not create storage directory: path to the temporary storage folder' is displayed, then the application is not allowed to write in the temporary storage folder by default.

We must specify the path to the temporary storage folder manually by editing apiFileCache.php as follows:

For example, imagine that we want to set the path /home/user/tmp as our temporary storage folder:

public function __construct() {

  $this->path = '/home/user/tmp';

}

For further information about Google Plus API, please visit http://developer.google.com