You are here

public static function TwitterProfile::pull in Twitter Profile Widget 3.x

Same name and namespace in other branches
  1. 8.2 src/TwitterProfile.php \Drupal\twitter_profile_widget\TwitterProfile::pull()
  2. 8 src/TwitterProfile.php \Drupal\twitter_profile_widget\TwitterProfile::pull()

Pull tweets from the Twitter API.

Parameters

array $instance: All the data for the given Twitter widget.

Return value

str[] An array of Twitter objects.

Overrides TwitterProfileInterface::pull

1 call to TwitterProfile::pull()
TwitterWidgetFormatter::getTweets in src/Plugin/Field/FieldFormatter/TwitterWidgetFormatter.php
Implements hook_preprocess_HOOK().

File

src/TwitterProfile.php, line 27

Class

TwitterProfile
Class TwitterProfile.

Namespace

Drupal\twitter_profile_widget

Code

public static function pull(array $instance) {
  if (!\Drupal::state()
    ->get('twitter_api_access_token')) {
    \Drupal::logger('twitter_profile_widget')
      ->error('No access token was found for the Twitter API. Check /admin/config/media/twitter_profile_widget.');
    return FALSE;
  }
  $query = self::buildQuery($instance['account'], $instance['list_type'], $instance['timeline'], $instance['search'], $instance['replies'], $instance['retweets']);
  $data_array = self::request($query);
  $json = json_encode($data_array);
  $tweets = json_decode($json);
  if (empty($tweets)) {
    return FALSE;
  }
  elseif (isset($tweets->errors)) {
    \Drupal::logger('twitter_profile_widget')
      ->error($tweets->errors[0]->message);
    return FALSE;
  }
  elseif (isset($tweets->statuses)) {

    // The "Search" API returns statuses within the "statuses" element.
    // See https://dev.twitter.com/rest/reference/get/search/tweets .
    return $tweets->statuses;
  }
  return $tweets;
}