Drupal 8 Module: Create twitter feed block

By Tom Mon, 02/22/2016 - 00:06

In this tutorial I'm going to show you how to create first module in Drupal 8 to pull Twitter user time line feed as Block. the module in this example name is tweets, all your custom module in Drupal 8 should in /modules/*

Create file: tweets.info.yml

in Drupal 7 is tweets.info. However in Drupal 8 it's changed to ModuleName.info.yml.

name: Tweets
type: module
description: Twitter feed
core: 8.x
package: Tabvn

Create file: tweets.module

<?php
/**
* Implements hook_theme()
*/
function tweets_theme($existing, $type, $theme, $path) {
return [
'tweets' => [
'variables' => [
'tweets' => NULL,
],
],
];
}
function template_preprocess_tweets(&$variables) {
$variables['attributes']['class'][] = 'tweets';
$tweets = $variables['tweets'];
$items = array();
if (!empty($tweets)) {
foreach ($tweets as $tweet) {
$settings = array(
'#type' => 'processed_text',
'#text' => $tweet->text,
'#format' => NULL,
'#filter_types_to_skip' => array(),
'#langcode' => '',
);
$text = \Drupal::service('renderer')->renderPlain($settings);
$items[] = array(
'id' => $tweet->id,
'text' => $text,
'time' => $tweet->created_at,
'time_ago' => \Drupal::service('date.formatter')
->formatInterval(REQUEST_TIME - strtotime($tweet->created_at)),
'username' => $tweet->user->screen_name,
'screen_name' => $tweet->user->screen_name,
'user_url' => $tweet->user->url,
);
}
}
$variables['items'] = $items;
}

Create a block display twitter feed

tweets/src/Plugin/Block/TwitterBlock.php

<?php
/**
* @file
* Contains \Drupal\tweets\Plugin\Block\TwitterBlock.
*/
namespace Drupal\tweets\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\tweets\TwitterAPIExchange;
/**
* Provides a 'TwitterBlock' block.
*
* @Block(
* id = "twitter_block",
* admin_label = @Translation("Twitter block"),
* )
*/
class TwitterBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
$form['markup'] = array(
'#type' => 'markup',
'#markup' => t('<a href="@url">Create a twitter app on the twitter developer site</a>', array('@url' => 'https://dev.twitter.com/apps/')),
);
$form['tweets_username'] = array(
'#type' => 'textfield',
'#title' => $this->t('Twitter username'),
'#description' => $this->t(''),
'#default_value' => isset($this->configuration['tweets_username']) ? $this->configuration['tweets_username'] : 'tabvn',
);
$form['tweets_limit'] = array(
'#type' => 'textfield',
'#title' => $this->t('Limit'),
'#description' => $this->t(''),
'#default_value' => isset($this->configuration['tweets_limit']) ? $this->configuration['tweets_limit'] : 3,
);
$form['access_token'] = array(
'#type' => 'textfield',
'#title' => $this->t('Access token'),
'#description' => $this->t(''),
'#default_value' => isset($this->configuration['access_token']) ? $this->configuration['access_token'] : '',
);
$form['token_secret'] = array(
'#type' => 'textfield',
'#title' => $this->t('Token secret'),
'#description' => $this->t(''),
'#default_value' => isset($this->configuration['token_secret']) ? $this->configuration['token_secret'] : '',
);
$form['consumer_key'] = array(
'#type' => 'textfield',
'#title' => $this->t('Consumer key'),
'#description' => $this->t(''),
'#default_value' => isset($this->configuration['consumer_key']) ? $this->configuration['consumer_key'] : '',
);
$form['consumer_secret'] = array(
'#type' => 'textfield',
'#title' => $this->t('Consumer secret'),
'#description' => $this->t(''),
'#default_value' => isset($this->configuration['consumer_secret']) ? $this->configuration['consumer_secret'] : '',
);
return $form;
}
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['access_token'] = $form_state->getValue('access_token');
$this->configuration['token_secret'] = $form_state->getValue('token_secret');
$this->configuration['consumer_key'] = $form_state->getValue('consumer_key');
$this->configuration['consumer_secret'] = $form_state->getValue('consumer_secret');
$this->configuration['tweets_username'] = $form_state->getValue('tweets_username');
$this->configuration['tweets_limit'] = $form_state->getValue('tweets_limit');
}
/**
* {@inheritdoc}
*/
public function build() {
$build = [];
$settings = array(
'oauth_access_token' => $this->configuration['access_token'],
'oauth_access_token_secret' => $this->configuration['token_secret'],
'consumer_key' => $this->configuration['consumer_key'],
'consumer_secret' => $this->configuration['consumer_secret']
);
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=' . $this->configuration['tweets_username'] . '&count=' . $this->configuration['tweets_limit'];
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
if ($response) {
$tweets = json_decode($response);
$build = array(
'#theme' => 'tweets',
'#tweets' => $tweets,
'#cache' => array(
'max-age' => 3600, // seconds
),
);
}
return $build;
}
}

Use third party Object class

tweets/src/TwitterAPIExchange.php

<?php
/**
* Twitter-API-PHP : Simple PHP wrapper for the v1.1 API
*
* PHP version 5.3.10
*
* @category Awesomeness
* @package Twitter-API-PHP
* @author James Mallison <[email protected]>
* @license MIT License
* @version 1.0.4
* @link http://github.com/j7mbo/twitter-api-php
*/
namespace Drupal\tweets;
class TwitterAPIExchange {
/**
* @var string
*/
private $oauth_access_token;
/**
* @var string
*/
private $oauth_access_token_secret;
/**
* @var string
*/
private $consumer_key;
/**
* @var string
*/
private $consumer_secret;
/**
* @var array
*/
private $postfields;
/**
* @var string
*/
private $getfield;
/**
* @var mixed
*/
protected $oauth;
/**
* @var string
*/
public $url;
/**
* @var string
*/
public $requestMethod;
/**
* Create the API access object. Requires an array of settings::
* oauth access token, oauth access token secret, consumer key, consumer secret
* These are all available by creating your own application on dev.twitter.com
* Requires the cURL library
*
* @throws \Exception When cURL isn't installed or incorrect settings parameters are provided
*
* @param array $settings
*/
public function __construct(array $settings) {
if (!in_array('curl', get_loaded_extensions())) {
$msg = 'You need to install cURL, see: http://curl.haxx.se/docs/install.html';
\Drupal::logger('tweets')->notice($msg);
}
if (!isset($settings['oauth_access_token'])
|| !isset($settings['oauth_access_token_secret'])
|| !isset($settings['consumer_key'])
|| !isset($settings['consumer_secret'])
) {
$msg = 'Make sure you are passing in the correct parameters';
\Drupal::logger('tweets')->notice($msg);
}
$this->oauth_access_token = $settings['oauth_access_token'];
$this->oauth_access_token_secret = $settings['oauth_access_token_secret'];
$this->consumer_key = $settings['consumer_key'];
$this->consumer_secret = $settings['consumer_secret'];
}
/**
* Set postfields array, example: array('screen_name' => 'J7mbo')
*
* @param array $array Array of parameters to send to API
*
* @throws \Exception When you are trying to set both get and post fields
*
* @return TwitterAPIExchange Instance of self for method chaining
*/
public function setPostfields(array $array) {
if (!is_null($this->getGetfield())) {
$msg = 'You can only choose get OR post fields.';
\Drupal::logger('my_module')->notice($msg);
}
if (isset($array['status']) && substr($array['status'], 0, 1) === '@') {
$array['status'] = sprintf("\0%s", $array['status']);
}
foreach ($array as $key => &$value) {
if (is_bool($value)) {
$value = ($value === TRUE) ? 'true' : 'false';
}
}
$this->postfields = $array;
// rebuild oAuth
if (isset($this->oauth['oauth_signature'])) {
$this->buildOauth($this->url, $this->requestMethod);
}
return $this;
}
/**
* Set getfield string, example: '?screen_name=J7mbo'
*
* @param string $string Get key and value pairs as string
*
* @throws \Exception
*
* @return \TwitterAPIExchange Instance of self for method chaining
*/
public function setGetfield($string) {
if (!is_null($this->getPostfields())) {
$msg = 'You can only choose get OR post fields.';
\Drupal::logger('tweets')->notice($msg);
}
$getfields = preg_replace('/^\?/', '', explode('&', $string));
$params = array();
foreach ($getfields as $field) {
if ($field !== '') {
list($key, $value) = explode('=', $field);
$params[$key] = $value;
}
}
$this->getfield = '?' . http_build_query($params);
return $this;
}
/**
* Get getfield string (simple getter)
*
* @return string $this->getfields
*/
public function getGetfield() {
return $this->getfield;
}
/**
* Get postfields array (simple getter)
*
* @return array $this->postfields
*/
public function getPostfields() {
return $this->postfields;
}
/**
* Build the Oauth object using params set in construct and additionals
* passed to this method. For v1.1, see: https://dev.twitter.com/docs/api/1.1
*
* @param string $url The API url to use. Example: https://api.twitter.com/1.1/search/tweets.json
* @param string $requestMethod Either POST or GET
*
* @throws \Exception
*
* @return \TwitterAPIExchange Instance of self for method chaining
*/
public function buildOauth($url, $requestMethod) {
if (!in_array(strtolower($requestMethod), array('post', 'get'))) {
\Drupal::logger('tweets')->notice('Request method must be either POST or GET');
}
$consumer_key = $this->consumer_key;
$consumer_secret = $this->consumer_secret;
$oauth_access_token = $this->oauth_access_token;
$oauth_access_token_secret = $this->oauth_access_token_secret;
$oauth = array(
'oauth_consumer_key' => $consumer_key,
'oauth_nonce' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => $oauth_access_token,
'oauth_timestamp' => time(),
'oauth_version' => '1.0'
);
$getfield = $this->getGetfield();
if (!is_null($getfield)) {
$getfields = str_replace('?', '', explode('&', $getfield));
foreach ($getfields as $g) {
$split = explode('=', $g);
/** In case a null is passed through **/
if (isset($split[1])) {
$oauth[$split[0]] = urldecode($split[1]);
}
}
}
$postfields = $this->getPostfields();
if (!is_null($postfields)) {
foreach ($postfields as $key => $value) {
$oauth[$key] = $value;
}
}
$base_info = $this->buildBaseString($url, $requestMethod, $oauth);
$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, TRUE));
$oauth['oauth_signature'] = $oauth_signature;
$this->url = $url;
$this->requestMethod = $requestMethod;
$this->oauth = $oauth;
return $this;
}
/**
* Perform the actual data retrieval from the API
*
* @param boolean $return If true, returns data. This is left in for backward compatibility reasons
* @param array $curlOptions Additional Curl options for this request
*
* @throws \Exception
*
* @return string json If $return param is true, returns json data.
*/
public function performRequest($return = TRUE, $curlOptions = array()) {
if (!is_bool($return)) {
\Drupal::logger('tweets')->notice('performRequest parameter must be true or false');
}
$header = array($this->buildAuthorizationHeader($this->oauth), 'Expect:');
$getfield = $this->getGetfield();
$postfields = $this->getPostfields();
$options = array(
CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => FALSE,
CURLOPT_URL => $this->url,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_TIMEOUT => 10,
) + $curlOptions;
if (!is_null($postfields)) {
$options[CURLOPT_POSTFIELDS] = http_build_query($postfields);
}
else {
if ($getfield !== '') {
$options[CURLOPT_URL] .= $getfield;
}
}
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
if (($error = curl_error($feed)) !== '') {
curl_close($feed);
throw new \Exception($error);
}
curl_close($feed);
return $json;
}
/**
* Private method to generate the base string used by cURL
*
* @param string $baseURI
* @param string $method
* @param array $params
*
* @return string Built base string
*/
private function buildBaseString($baseURI, $method, $params) {
$return = array();
ksort($params);
foreach ($params as $key => $value) {
$return[] = rawurlencode($key) . '=' . rawurlencode($value);
}
return $method . "&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $return));
}
/**
* Private method to generate authorization header used by cURL
*
* @param array $oauth Array of oauth data generated by buildOauth()
*
* @return string $return Header used by cURL for request
*/
private function buildAuthorizationHeader(array $oauth) {
$return = 'Authorization: OAuth ';
$values = array();
foreach ($oauth as $key => $value) {
if (in_array($key, array(
'oauth_consumer_key',
'oauth_nonce',
'oauth_signature',
'oauth_signature_method',
'oauth_timestamp',
'oauth_token',
'oauth_version'
))) {
$values[] = "$key=\"" . rawurlencode($value) . "\"";
}
}
$return .= implode(', ', $values);
return $return;
}
/**
* Helper method to perform our request
*
* @param string $url
* @param string $method
* @param string $data
* @param array $curlOptions
*
* @throws \Exception
*
* @return string The json response from the server
*/
public function request($url, $method = 'get', $data = NULL, $curlOptions = array()) {
if (strtolower($method) === 'get') {
$this->setGetfield($data);
}
else {
$this->setPostfields($data);
}
return $this->buildOauth($url, $method)->performRequest(TRUE, $curlOptions);
}
}

Create template file: tweets.html.twig

tweets/templates/tweets.html.twig

<ul{{ attributes }}>
{% for tweet in items %}
<li class="tweet">
<div class="tweet-message">
<span class="screen-name"><a
href="{{ tweet.user_url }}">@{{ tweet.screen_name }}</a></span> {{ tweet.text }}
</div>
<div class="time">{% trans %}{{ tweet.time_ago }} ago{% endtrans %}</div>
</li>
{% endfor %}
</ul>

It's done. Now follow the URL your-domain.com/admin/modules then active the Tweets module. Add new block and configure Twitter api key. Setup your twitter authenticate key at https://dev.twitter.com/apps/

Download the module at: https://github.com/tabvn/tweets