You are here

public function FacebookCommentsBlock::build in Facebook Comments Block 8

Same name and namespace in other branches
  1. 8.2 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 287

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();
  $main_domain = $base_url;
  if ($config['facebook_comments_block_settings_domain'] !== '') {
    $main_domain = $config['facebook_comments_block_settings_domain'];
  }
  $url = $main_domain . $current_unaliased_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'];
  $js_vars = array(
    'facebook_app_id' => $facebook_app_id,
    'facebook_app_id_script' => $facebook_app_id_script,
    'facebook_app_lang' => $facebook_app_lang,
  );
  $theme_vars = array(
    'data_attributes' => array(
      'href' => $url,
      'data-href' => $url,
      'data-width' => $config['facebook_comments_block_settings_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'],
    ),
  );
  return array(
    '#cache' => array(
      'contexts' => array(
        'url',
      ),
    ),
    '#theme' => 'facebook_comments_block_block',
    '#data_attributes' => new Attribute($theme_vars['data_attributes']),
    '#attached' => array(
      'library' => array(
        'facebook_comments_block/facebook-comments-block',
      ),
      'drupalSettings' => array(
        'facebook_comments_block_settings' => $js_vars,
      ),
    ),
  );
}