You are here

public function TwitterProfile::pull in Twitter Profile Widget 8.2

Same name and namespace in other branches
  1. 8 src/TwitterProfile.php \Drupal\twitter_profile_widget\TwitterProfile::pull()
  2. 3.x 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

File

src/TwitterProfile.php, line 23

Class

TwitterProfile
Class TwitterProfile.

Namespace

Drupal\twitter_profile_widget

Code

public function pull(array $instance) {
  if (!($connection = $this
    ->getConnection())) {
    \Drupal::logger('twitter_profile_widget')
      ->error('No credentials were found for the Twitter API. Check /admin/config/media/twitter_profile_widget.');
    return FALSE;
  }
  $twitter = new TwitterAPIExchange($connection);
  $query = $this
    ->buildQuery($instance['account'], $instance['list_type'], $instance['timeline'], $instance['search'], $instance['replies'], $instance['retweets']);

  // Don't go double encoding.
  $getfield = urldecode($query['getfield']);
  $data = $twitter
    ->setGetfield($getfield)
    ->buildOauth($query['url'], 'GET')
    ->performRequest();
  $tweets = json_decode($data);
  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;
}