public function TwitterProfile::pull in Twitter Profile Widget 8
Same name and namespace in other branches
- 8.2 src/TwitterProfile.php \Drupal\twitter_profile_widget\TwitterProfile::pull()
- 3.x src/TwitterProfile.php \Drupal\twitter_profile_widget\TwitterProfile::pull()
Pull tweets from the Twitter API.
Parameters
TwitterWidget $entity: A fully-built Twitter entity object.
Return value
str[] An array of Twitter objects.
Overrides TwitterProfileInterface::pull
File
- src/
TwitterProfile.php, line 24
Class
- TwitterProfile
- Class TwitterProfile.
Namespace
Drupal\twitter_profile_widgetCode
public function pull(TwitterWidget $entity) {
$account = $entity
->get('account')->value;
$type = $entity
->get('type')->value;
$search = $entity
->get('search')->value;
$timeline = $entity
->get('timeline')->value;
$count = $entity
->get('count')->value;
$replies = $entity
->get('replies')->value;
$retweets = $entity
->get('retweets')->value;
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($account, $type, $timeline, $search, $replies, $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;
}