You are here

public function FacebookPostBlock::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/FacebookPostBlock.php, line 174

Class

FacebookPostBlock
Provides a 'FacebookPostBlock' block.

Namespace

Drupal\socialfeed\Plugin\Block

Code

public function build() {
  $build = [];
  $items = [];
  $block_settings = $this
    ->getConfiguration();
  try {
    if ($block_settings['override']) {
      $facebook = $this->facebook
        ->createInstance($block_settings['app_id'], $block_settings['secret_key'], $block_settings['user_token'], $this->config
        ->get('page_name'));
    }
    else {
      $facebook = $this->facebook
        ->createInstance($this->config
        ->get('app_id'), $this->config
        ->get('secret_key'), $this->config
        ->get('user_token'), $this->config
        ->get('page_name'));
    }
    $post_types = $this
      ->getSetting('all_types');
    if (!$post_types) {
      $post_types = $this
        ->getSetting('post_type');
    }
    $posts = $facebook
      ->getPosts($this
      ->getSetting('page_name'), $post_types, $this
      ->getSetting('no_feeds'));
    foreach ($posts as $post) {
      if ($post['status_type'] = !NULL) {
        $items[] = [
          '#theme' => [
            'socialfeed_facebook_post__' . $post['status_type'],
            'socialfeed_facebook_post',
          ],
          '#post' => $post,
          '#cache' => [
            // Cache for 1 hour.
            'max-age' => 60 * 60,
            'cache tags' => $this->config
              ->getCacheTags(),
            'context' => $this->config
              ->getCacheContexts(),
          ],
        ];
      }
    }
  } catch (Exception $exception) {
    $this->logger
      ->error($this
      ->t('Exception: @exception', [
      '@exception' => $exception
        ->getMessage(),
    ]));
  }
  $build['posts'] = [
    '#theme' => 'item_list',
    '#items' => $items,
  ];
  return $build;
}