You are here

public function TwitterBlock::build in Twitter Block 8.3

Same name and namespace in other branches
  1. 8.2 src/Plugin/Block/TwitterBlock.php \Drupal\twitter_block\Plugin\Block\TwitterBlock::build()

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/TwitterBlock.php, line 190

Class

TwitterBlock
Defines a twitter block block type.

Namespace

Drupal\twitter_block\Plugin\Block

Code

public function build() {
  $config = $this
    ->getConfiguration();
  $render['block'] = [
    '#type' => 'link',
    '#title' => $this
      ->t('Tweets by @@username', [
      '@username' => $config['username'],
    ]),
    '#url' => Url::fromUri('https://twitter.com/' . $config['username']),
    '#attributes' => [
      'class' => [
        'twitter-timeline',
      ],
    ],
    '#attached' => [
      'library' => [
        'twitter_block/widgets',
      ],
    ],
  ];
  if (!empty($config['theme'])) {
    $render['block']['#attributes']['data-theme'] = $config['theme'];
  }
  if (!empty($config['link_color'])) {
    $render['block']['#attributes']['data-link-color'] = '#' . $config['link_color'];
  }
  if (!empty($config['width'])) {
    $render['block']['#attributes']['data-width'] = $config['width'];
  }
  if (!empty($config['height'])) {
    $render['block']['#attributes']['data-height'] = $config['height'];
  }
  if (!empty($config['chrome'])) {
    $options = array_keys(array_filter($config['chrome']));
    if (count($options)) {
      $render['block']['#attributes']['data-chrome'] = implode(' ', $options);
    }
  }
  if (!empty($config['border_color'])) {
    $render['block']['#attributes']['data-border-color'] = '#' . $config['border_color'];
  }
  if (!empty($config['language'])) {
    $render['block']['#attributes']['lang'] = $config['language'];
  }
  if (!empty($config['tweet_limit'])) {
    $render['block']['#attributes']['data-tweet-limit'] = $config['tweet_limit'];
  }
  if (!empty($config['related'])) {
    $render['block']['#attributes']['data-related'] = $config['related'];
  }
  if (!empty($config['polite'])) {
    $render['block']['#attributes']['aria-polite'] = $config['polite'];
  }
  return $render;
}