You are here

protected function TwitterWidgetFormatter::prepareTweets in Twitter Profile Widget 3.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/TwitterWidgetFormatter.php \Drupal\twitter_profile_widget\Plugin\Field\FieldFormatter\TwitterWidgetFormatter::prepareTweets()

Helper to parse Twitter's JSON and return a normalized array of tweets.

Parameters

object $tweets: An array of tweets, in Twitter class format.

int $count: How many tweets should be displayed.

Return value

string[] Non-keyed array of tweet elements.

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

File

src/Plugin/Field/FieldFormatter/TwitterWidgetFormatter.php, line 96

Class

TwitterWidgetFormatter
Plugin implementation of the 'twitter_widget' formatter.

Namespace

Drupal\twitter_profile_widget\Plugin\Field\FieldFormatter

Code

protected function prepareTweets($tweets, $count = 5) {
  $inc = 0;
  $tweets_filtered = [];
  foreach ($tweets as $tweet) {
    $inc++;
    $tweet->retweet_eyebrow = FALSE;

    // If this is a retweet, use the API-provided sub-element.
    if (isset($tweet->retweeted_status)) {
      $id = $tweet->retweeted_status->id;
      $retweet_user = $tweet->retweeted_status->user->screen_name;
      $original_user = $tweet->user->name;
      $original_screen_name = $tweet->user->screen_name;
      $retweet_link = Url::fromUri('//twitter.com/' . $retweet_user . '/status/' . $id);
      $user_text = $original_user . ' Retweeted';
      $user_url = Url::fromUri('//twitter.com/' . $original_screen_name);
      $user_link = Link::fromTextAndUrl($user_text, $user_url);

      // Switch $tweet object to its sub-element.
      $tweet = $tweet->retweeted_status;

      // Add the retweet eyebrow.
      $tweet->retweet_link = $retweet_link;
      $tweet->retweet_user = $user_link;
    }

    // Prepare the Tweet Action links, based on $tweet->id.
    $timestamp = strtotime($tweet->created_at);
    $tweets_filtered[$inc] = [
      'id' => (int) $tweet->id,
      'image' => self::schemaFreeLink($tweet->user->profile_image_url),
      'image_user' => $tweet->user->name,
      'author' => $tweet->user->name,
      'username' => $tweet->user->screen_name,
      'text' => self::parseTwitterLinks($tweet->text),
      'timestamp' => $timestamp,
      'time_ago' => $this
        ->t('@time ago', [
        '@time' => \Drupal::service('date.formatter')
          ->formatInterval(\Drupal::time()
          ->getRequestTime() - $timestamp),
      ]),
      'tweet_reply2' => Url::fromUri('//twitter.com/intent/tweet?in_reply_to=' . $tweet->id),
      'tweet_retweet' => Url::fromUri('//twitter.com/intent/retweet?tweet_id=' . $tweet->id),
      'tweet_star' => Url::fromUri('//twitter.com/intent/favorite?tweet_id=' . $tweet->id),
    ];
    if (isset($tweet->retweet_link)) {
      $tweets_filtered[$inc]['retweet_link'] = $tweet->retweet_link;
      $tweets_filtered[$inc]['retweet_user'] = $tweet->retweet_user;
    }
    if ($inc >= $count) {
      break;
    }
  }
  return $tweets_filtered;
}