You are here

public function FblikebuttonBlock::build in Facebook Like Button 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/FblikebuttonBlock.php, line 24

Class

FblikebuttonBlock
Provides a Facebook Like Button Block

Namespace

Drupal\fblikebutton\Plugin\Block

Code

public function build() {
  $block = array(
    '#theme' => 'fblikebutton',
    '#layout' => $this->configuration['layout'],
    '#show_faces' => $this->configuration['show_faces'],
    '#action' => $this->configuration['action'],
    '#font' => $this->configuration['font'],
    '#color_scheme' => $this->configuration['color_scheme'],
    '#language' => $this->configuration['language'],
  );

  // If it's not for the current page
  if ($this->configuration['block_url'] != '<current>') {
    $block['#url'] = $this->configuration['block_url'];
  }
  else {

    // Avoid this block to be cached
    $block['#cache'] = array(
      'max-age' => 0,
    );

    /**
     * Drupal uses the /node path to refers to the frontpage. That's why facebook
     * could point to www.example.com/node instead of wwww.example.com.
     *
     * To avoid this, we check if the current path is the frontpage
     */

    // Check if the path is pointing home
    if (\Drupal::routeMatch()
      ->getRouteName() == 'view.frontpage.page_1') {
      global $base_url;
      $block['#url'] = $base_url;
    }
    else {
      $block['#url'] = Url::fromRoute('<current>', array(), array(
        'absolute' => true,
      ))
        ->toString();
    }
  }
  return $block;
}