You are here

function twitter_block_block_view in Twitter Block 7.2

Same name and namespace in other branches
  1. 7 twitter_block.module \twitter_block_block_view()

Implements hook_block_view().

File

./twitter_block.module, line 411
A module to provide simple Twitter blocks using the Twitter Search API.

Code

function twitter_block_block_view($delta) {

  // Load the configuration.
  $config = twitter_block_block_get($delta);

  // Unserialize the timeline.
  $data = unserialize($config['data']);
  $block = array();
  $block['subject'] = check_plain($config['info']);
  $block['content'] = array(
    '#type' => 'link',
    '#title' => t('Tweets by @username', array(
      '@username' => $config['username'],
    )),
    '#href' => 'https://twitter.com/' . $config['username'],
    '#options' => array(
      'attributes' => array(
        'class' => array(
          'twitter-timeline',
        ),
        'data-widget-id' => $config['widget_id'],
      ),
      'html' => FALSE,
    ),
  );

  // Use a locally cached copy of widgets.js by default.
  if (variable_get('twitter_block_cache', 0) && ($url = twitter_block_cache())) {
    $block['content']['#attached']['js'] = array(
      array(
        'data' => $url,
        'type' => 'file',
      ),
    );
  }
  else {
    $block['content']['#attached']['js'] = array(
      '//platform.twitter.com/widgets.js' => array(
        'type' => 'external',
        'scope' => 'footer',
      ),
    );
  }
  if (!empty($data['theme'])) {
    $block['content']['#options']['attributes']['data-theme'] = $data['theme'];
  }
  if (!empty($data['link_color'])) {
    $block['content']['#options']['attributes']['data-link-color'] = '#' . $data['link_color'];
  }
  if (!empty($data['width'])) {
    $block['content']['#options']['attributes']['width'] = $data['width'];
  }
  if (!empty($data['height'])) {
    $block['content']['#options']['attributes']['height'] = $data['height'];
  }
  if (!empty($data['chrome'])) {
    $options = array();
    foreach ($data['chrome'] as $option => $status) {
      if ($status) {
        $options[] = $option;
      }
    }
    if (count($options)) {
      $block['content']['#options']['attributes']['data-chrome'] = implode(' ', $options);
    }
  }
  if (!empty($data['border_color'])) {
    $block['content']['#options']['attributes']['data-border-color'] = '#' . $data['border_color'];
  }
  if (!empty($data['language'])) {
    $block['content']['#options']['attributes']['lang'] = $data['language'];
  }
  if (!empty($data['tweet_limit'])) {
    $block['content']['#options']['attributes']['data-tweet-limit'] = $data['tweet_limit'];
  }
  if (!empty($data['related'])) {
    $block['content']['#options']['attributes']['data-related'] = $data['related'];
  }
  if (!empty($data['polite'])) {
    $block['content']['#options']['attributes']['data-aria-polite'] = $data['polite'];
  }
  return $block;
}