Skip to content

Instantly share code, notes, and snippets.

@a-fro
Last active December 15, 2015 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-fro/5222565 to your computer and use it in GitHub Desktop.
Save a-fro/5222565 to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Tweet block bean plugin.
*/
/**
* Require the twitteroauth library
*/
require_once(drupal_get_path('module','bean_tweets') . '/libraries/twitteroauth/twitteroauth.php');
class TweetBlockBean extends BeanPlugin {
private $_bean;
/**
* Declares default block settings.
*/
public function values() {
$values = array(
'settings' => array(
'handle' => '',
'number_of_tweets' => '5',
'cache_duration' => '30',
),
);
return array_merge(parent::values(), $values);
}
/**
* Builds extra settings for the block edit form.
*/
public function form($bean, $form, &$form_state) {
$form = array();
$form['settings'] = array(
'#type' => 'fieldset',
'#tree' => 1,
'#title' => t('Output'),
);
$form['settings']['handle'] = array(
'#type' => 'textfield',
'#title' => t('Twitter Handle'),
'#size' => 30,
'#default_value' => $bean->settings['handle'],
'#description' => t("The user's twitter account handle"),
'#required' => TRUE,
);
$form['settings']['number_of_tweets'] = array(
'#type' => 'textfield',
'#title' => t('Number of tweets to show'),
'#size' => 5,
'#default_value' => $bean->settings['number_of_tweets'],
'#required' => TRUE,
);
$form['settings']['cache_duration'] = array(
'#type' => 'textfield',
'#title' => t('Cache duration (in minutes)'),
'#size' => 5,
'#default_value' => $bean->settings['cache_duration'],
'#description' => t("The length of time (in minutes) to cache the results from twitter."),
'#required' => TRUE,
);
$form['#submit'][] = 'bean_tweets_clear_cache';
return $form;
}
/**
* Displays the bean.
*/
public function view($bean, $content, $view_mode = 'default', $langcode = NULL) {
// Set the bean instance
$this->_bean = $bean;
$markup = "<h1>Tweets from @{$bean->settings['handle']}</h1>";
foreach($this->getTweets() as $tweet) {
$sent_datetime = strtotime($tweet->created_at);
$sent = format_date($sent_datetime, 'custom', 'M j, g:i a');
$markup .= "<p><span style='color: #4099ff; font-weight: bold'>{$sent}</span><br />{$tweet->text}</p>";
}
// Add the markup to the render array
$content[] = array('#markup' => $markup);
return $content;
}
/**
* Gets the tweets from the cache or twitter.
*/
private function getTweets() {
if ($cached_tweets = $this->getCacheData())
$tweets = $cached_tweets;
else
$tweets = $this->getUserTimeline();
return $tweets;
}
/**
* Getter for the cache
*/
private function getCacheData() {
if ($cache = cache_get('bean_tweets-' . $this->_bean->settings['handle']))
return $cache->data;
return NULL;
}
/**
* Setter for the cache
*/
private function setCacheData($tweetData) {
cache_set(
'bean_tweets-' . $this->_bean->settings['handle'],
$tweetData,
'cache',
time() + 60 * $this->_bean->settings['cache_duration']
);
}
/**
* Get user timeline
*/
private function getUserTimeline() {
$connection = $this->getOauthConnection();
$timeline = $connection->get(
'statuses/user_timeline.json?' .
"screen_name={$this->_bean->settings['handle']}" .
"&count={$this->_bean->settings['number_of_tweets']}"
);
// Cache the results
$this->setCacheData($timeline);
return $timeline;
}
/**
* Establish OAuth connection
*/
private function getOauthConnection() {
$connection = new TwitterOAuth(
variable_get('twitter_consumer_key'),
variable_get('twitter_consumer_secret'),
variable_get('twitter_access_token'),
variable_get('twitter_token_secret')
);
// Set the host to the 1.1 API
$connection->host = "https://api.twitter.com/1.1/";
return $connection;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment