You are here

public function SharerichBlock::build in Sharerich 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/SharerichBlock.php, line 78
Contains \Drupal\sharerich\Plugin\Block\SharerichBlock.

Class

SharerichBlock
Provides a Sharerich block.

Namespace

Drupal\sharerich\Plugin\Block

Code

public function build() {
  $entity_storage = \Drupal::entityTypeManager()
    ->getStorage('sharerich');
  if ($sharerich_set = $entity_storage
    ->load($this->configuration['sharerich_set'])) {
    $buttons = array();
    foreach ($sharerich_set
      ->getServices() as $name => $service) {
      $buttons[$name] = [
        '#attributes' => [
          'class' => [
            'sharerich-buttons-wrapper',
            'rrssb-buttons-wrapper',
          ],
        ],
        '#wrapper_attributes' => [
          'class' => [
            'rrssb-' . $name,
          ],
        ],
        '#markup' => $service['markup'],
        '#allowed_tags' => sharerich_allowed_tags(),
      ];
    }

    // Allow other modules to alter the buttons before they are rendered.
    $context = _sharerich_get_token_data();
    \Drupal::moduleHandler()
      ->alter('sharerich_buttons', $buttons, $context);

    // Render tokens.
    foreach ($buttons as &$button) {
      $button['#markup'] = \Drupal::token()
        ->replace($button['#markup'], _sharerich_get_token_data());
    }
    $item_list = [
      '#theme' => 'item_list',
      '#items' => $buttons,
      '#type' => 'ul',
      '#wrapper_attributes' => [
        'class' => [
          'sharerich-wrapper',
          'share-container',
          'sharerich-' . $this->configuration['sharerich_set'],
          'sharerich-' . $this->configuration['orientation'],
          $this->configuration['sticky'] ? 'sharerich-sticky' : '',
        ],
      ],
      '#attributes' => [
        'class' => [
          'sharerich-buttons',
          'rrssb-buttons',
        ],
      ],
      '#attached' => [
        'library' => [
          'sharerich/rrssb',
          'sharerich/sharerich',
        ],
      ],
    ];
    $build['content'] = [
      '#theme' => 'sharerich',
      '#buttons' => $item_list,
      '#cache' => [
        'contexts' => [
          'url.path',
        ],
      ],
    ];
    return $build;
  }
}