You are here

public function FacebookCommentsBlock::build in Facebook Comments Social Plugin 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/FacebookCommentsBlock.php, line 22

Class

FacebookCommentsBlock
Provides a Facebook Comments Block

Namespace

Drupal\facebook_comments\Plugin\Block

Code

public function build() {
  $config = $this
    ->getConfiguration();
  $style = $config['facebook_comments_style'];
  $width = $config['facebook_comments_width'];
  $fluid = $config['facebook_comments_width_fluid'];
  $amount = $config['facebook_comments_amount'];
  $config = \Drupal::config('facebook_comments.settings');
  $appid = $config
    ->get('facebook_comments_appid');
  $admins = $config
    ->get('facebook_comments_admins');
  $ssl = $config
    ->get('facebook_comments_ssl');
  $options = array(
    'absolute' => TRUE,
  );
  $url = Url::fromRoute('<current>', array(), $options)
    ->toString();
  $lang = _facebook_comments_get_language_code();

  // If the path is non-SSL, rewrite it to SSL.
  if ($ssl && strpos($url, "http://") !== FALSE) {
    $url = str_ireplace("http://", "https://", $url);
  }
  if ($fluid) {
    $class = "fb-comments-fluid";
  }
  else {
    $class = "";
  }
  $output = array(
    '#theme' => 'facebook_comments_block',
    '#style' => $style,
    '#amount' => $amount,
    '#width' => $width,
    '#class' => $class,
    '#url' => $url,
    '#lang' => $lang,
  );

  // Display Facebook comments with fluid width
  if ($fluid) {
    $output['#attached']['library'][] = 'facebook_comments/fluid';
  }

  // Add the Facebook App ID if it exists
  if ($appid) {
    $a = array(
      '#tag' => 'meta',
      '#attributes' => array(
        'property' => 'fb:app_id',
        'content' => $appid,
      ),
    );
    $output['#attached']['html_head'][] = [
      $a,
      'facebook_comments',
    ];
  }
  elseif ($admins) {
    $admin = explode(",", $admins);
    foreach ($admin as $key => $value) {
      $a = array(
        '#tag' => 'meta',
        '#attributes' => array(
          'property' => 'fb:admins',
          'content' => trim($value),
        ),
      );
      $output['#attached']['html_head'][] = [
        $a,
        'facebook_comments_' . $key,
      ];
    }
  }
  return $output;
}