You are here

public function TwitterPostBlock::build in Social Feed 8

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/TwitterPostBlock.php, line 126

Class

TwitterPostBlock
Provides a 'TwitterPostBlock' block.

Namespace

Drupal\socialfeed\Plugin\Block

Code

public function build() {
  $build = [];
  $items = [];
  $config = $this->config;
  $block_settings = $this
    ->getConfiguration();
  if ($block_settings['override']) {
    $twitter = $this->twitter
      ->createInstance($block_settings['consumer_key'], $block_settings['consumer_secret'], $block_settings['access_token'], $block_settings['access_token_secret']);
  }
  else {
    $twitter = $this->twitter
      ->createInstance($config
      ->get('consumer_key'), $config
      ->get('consumer_secret'), $config
      ->get('access_token'), $config
      ->get('access_token_secret'));
  }
  $tweets_count = $this
    ->getSetting('tweets_count');
  $posts = $twitter
    ->getPosts($tweets_count);
  if (!is_array($posts)) {
    return $build;
  }
  foreach ($posts as $post) {
    $items[] = [
      '#theme' => 'socialfeed_twitter_post',
      '#post' => $post,
      '#cache' => [
        // Cache for 1 hour.
        'max-age' => 60 * 60,
        'cache tags' => $this->config
          ->getCacheTags(),
        'context' => $this->config
          ->getCacheContexts(),
      ],
    ];
  }
  $build['posts'] = [
    '#theme' => 'item_list',
    '#items' => $items,
  ];
  return $build;
}