You are here

public function FacebookCommentsBlock::build in Facebook Comments Block 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Block/FacebookCommentsBlock.php \Drupal\facebook_comments_block\Plugin\Block\FacebookCommentsBlock::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/FacebookCommentsBlock.php, line 305

Class

FacebookCommentsBlock
Provides a 'Facebook Comments Block' block.

Namespace

Drupal\facebook_comments_block\Plugin\Block

Code

public function build() {
  global $base_url;
  $config = $this
    ->getConfiguration();
  $current_unaliased_path = \Drupal::service('path.current')
    ->getPath();
  $current_aliased_path = \Drupal::service('path_alias.manager')
    ->getAliasByPath($current_unaliased_path);
  $main_domain = $base_url;
  if ($config['facebook_comments_block_settings_domain'] !== '') {
    $main_domain = $config['facebook_comments_block_settings_domain'];
  }
  $url = $main_domain . $current_aliased_path;
  $facebook_app_id = $config['facebook_comments_block_settings_app_id'];
  $facebook_app_id_script = $facebook_app_id != '' ? "&appId={$facebook_app_id}" : '';
  $facebook_app_lang = $config['facebook_comments_block_settings_lang'];
  $facebook_app_width_unit = $config['facebook_comments_block_settings_width_unit'];
  $facebook_app_width = '';
  if ($facebook_app_width_unit == 'percentage') {
    $facebook_app_width = '100%';
  }
  else {
    $facebook_app_width = $config['facebook_comments_block_settings_width'];
  }
  $theme_vars = array(
    'data_attributes' => array(
      'href' => $url,
      'data-href' => $url,
      'data-width' => $facebook_app_width,
      'data-numposts' => $config['facebook_comments_block_settings_number_of_posts'],
      'data-colorscheme' => $config['facebook_comments_block_settings_color_schema'],
      'data-order-by' => $config['facebook_comments_block_settings_order'],
    ),
    'facebook_app_id' => $facebook_app_id_script,
    'facebook_app_lang' => $facebook_app_lang,
  );
  return array(
    '#cache' => array(
      'contexts' => array(
        'url',
      ),
    ),
    '#theme' => 'facebook_comments_block_block',
    '#data_attributes' => new Attribute($theme_vars['data_attributes']),
    '#facebook_app_id' => $theme_vars['facebook_app_id'],
    '#facebook_app_lang' => $theme_vars['facebook_app_lang'],
  );
}