You are here

public function ResponsiveShareButtonsBlock::build in Responsive Share Buttons 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/ResponsiveShareButtonsBlock.php, line 22

Class

ResponsiveShareButtonsBlock
Provides a 'Responsive Share buttons' block.

Namespace

Drupal\responsive_share_buttons\Plugin\Block

Code

public function build() {
  $request = \Drupal::request();
  $route_match = \Drupal::routeMatch();
  $title = \Drupal::service('title_resolver')
    ->getTitle($request, $route_match
    ->getRouteObject());
  if (is_array($title) && isset($title['#markup'])) {
    $title = $title['#markup'];
  }
  elseif (!is_string($title)) {
    $title = \Drupal::config('system.site')
      ->get('name');
  }
  $title = urlencode($title);
  $url = urlencode($request
    ->getUri());
  $links = [];
  $networks = $this
    ->getActiveNetworks();
  foreach ($networks as $network) {
    $links[] = $this
      ->prepareShareLink($network, $url, $title);
  }
  $render_array = [
    '#theme' => 'item_list',
    '#cache' => [
      'contexts' => [
        'url',
      ],
    ],
    '#wrapper_attributes' => [
      'class' => [
        'share-inner-wrp',
      ],
    ],
    '#items' => $links,
    '#attached' => [
      'library' => [
        'responsive_share_buttons/share',
      ],
    ],
  ];
  return $render_array;
}