function twitter_profile_widget_preprocess_twitter_widget in Twitter Profile Widget 8
Implements hook_preprocess_HOOK().
Take the user-entered Twitter "configuration" and return rendered tweets.
File
- ./
twitter_profile_widget.module, line 120 - Contains twitter_profile_widget.module.
Code
function twitter_profile_widget_preprocess_twitter_widget(&$variables) {
$entity = $variables['elements']['#twitter_widget'];
$id = $entity
->get('id')->value;
// Retrieve tweets for this widget.
$cache = \Drupal::cache()
->get('twitter_widget:' . $id);
if (!isset($cache->data)) {
$Twitter = new TwitterProfile();
$tweets = $Twitter
->pull($entity);
$config = \Drupal::config('twitter_profile_widget.settings');
$age = $config
->get('twitter_widget_cache_time');
\Drupal::cache()
->set('twitter_widget:' . $id, $tweets, time() + $age, [
'twitter_widget_view',
'twitter_widget:' . $id,
]);
}
else {
$tweets = $cache->data;
}
// If the API call returns errors, do not send any data to the template.
if ($tweets) {
$variables['tweets'] = _twitter_profile_widget_prepare_tweets($tweets, $entity
->get('count')->value);
if ($headline = $entity
->get('headline')->value) {
$variables['headline'] = $headline;
}
if ($view_all = $entity
->get('view_all')->value) {
if ($entity
->get('type')->value == 'search') {
$params = [
'q' => $entity
->get('search')->value,
];
$getfield = '?' . http_build_query($params);
$url = Url::fromUri('https://twitter.com/search' . $getfield);
}
else {
$url = Url::fromUri('https://twitter.com/' . $entity
->get('account')->value);
}
$variables['view_all'] = Link::fromTextAndUrl($view_all, $url);
}
}
}