You are here

public function SocialFooterPoweredByBlock::build in Open Social 10.3.x

Same name and namespace in other branches
  1. 10.0.x modules/social_features/social_footer/src/Plugin/Block/SocialFooterPoweredByBlock.php \Drupal\social_footer\Plugin\Block\SocialFooterPoweredByBlock::build()
  2. 10.1.x modules/social_features/social_footer/src/Plugin/Block/SocialFooterPoweredByBlock.php \Drupal\social_footer\Plugin\Block\SocialFooterPoweredByBlock::build()
  3. 10.2.x modules/social_features/social_footer/src/Plugin/Block/SocialFooterPoweredByBlock.php \Drupal\social_footer\Plugin\Block\SocialFooterPoweredByBlock::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 SystemPoweredByBlock::build

See also

\Drupal\block\BlockViewBuilder

File

modules/social_features/social_footer/src/Plugin/Block/SocialFooterPoweredByBlock.php, line 166

Class

SocialFooterPoweredByBlock
Provides a 'Powered by' block.

Namespace

Drupal\social_footer\Plugin\Block

Code

public function build() {
  $build = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'content',
      ],
      'block' => 'block-socialblue-footer-powered',
    ],
  ];
  if ($this->configuration['logo']) {

    /** @var \Drupal\file\FileInterface $file */
    $file = $this->storage
      ->load($this->configuration['logo']);
    $build['logo'] = [
      '#theme' => 'image',
      '#uri' => $file
        ->getFileUri(),
    ];
  }
  elseif ($this->configFactory
    ->get('system.theme')
    ->get('default') === 'socialblue') {

    // Add default image.
    // Only when socialblue is default we continue.
    $file_path = drupal_get_path('module', 'social_footer') . DIRECTORY_SEPARATOR . 'open_social_logo.png';
    $file_system = \Drupal::service('file_system');
    $uri = $file_system
      ->copy($file_path, 'public://open_social_logo.png', FileSystemInterface::EXISTS_REPLACE);

    // Create a file media.

    /** @var \Drupal\file\FileInterface $file */
    $media = File::create([
      'uri' => $uri,
    ]);
    $media
      ->setPermanent();
    $media
      ->save();
    $build['logo'] = [
      '#theme' => 'image',
      '#uri' => $media
        ->getFileUri(),
    ];
  }
  $build['text'] = [
    '#type' => 'processed_text',
    '#text' => $this->configuration['text']['value'],
    '#format' => $this->configuration['text']['format'],
    '#prefix' => '<div class="footer-block--body">',
    '#suffix' => '</div>',
  ];
  if ($this->configuration['link']['url']) {
    $options = [
      'attributes' => $build['#attributes'] + [
        'target' => '_blank',
      ],
    ];
    if ($this->configuration['link']['title']) {
      $options['attributes']['title'] = $this->configuration['link']['title'];
    }
    $build = [
      '#type' => 'link',
      '#title' => isset($build['logo']) ? [
        'logo' => $build['logo'],
        'text' => $build['text'],
      ] : $build['text'],
      '#url' => Url::fromUri($this->configuration['link']['url'], $options),
    ];
  }
  return [
    '#attached' => [
      'library' => [
        'social_footer/block',
      ],
    ],
    'content' => $build,
  ];
}