You are here

public function ImageEmbedBlock::build in File Entity Browser 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/ImageEmbedBlock.php, line 211

Class

ImageEmbedBlock
Provides the "Image Embed" block.

Namespace

Drupal\file_browser\Plugin\Block

Code

public function build() {
  $build = [];
  foreach ($this->configuration['files'] as $info) {

    /** @var \Drupal\file\Entity\File $file */
    $file = File::load($info['fid']);
    if ($file && $file
      ->access('view')) {
      $uri = $file
        ->getFileUri();
      $image = \Drupal::service('image.factory')
        ->get($uri);
      if ($image
        ->isValid()) {
        $width = $image
          ->getWidth();
        $height = $image
          ->getHeight();
      }
      else {
        $width = $height = NULL;
      }
      $current = [
        '#theme' => 'image',
        '#width' => $width,
        '#height' => $height,
        '#alt' => isset($info['settings']['alt']) ? $info['settings']['alt'] : '',
        '#uri' => $uri,
      ];
      if ($this->configuration['image_style']) {
        $current['#theme'] = 'image_style';
        $current['#style_name'] = $this->configuration['image_style'];
      }
      $build[] = $current;
    }
  }
  return $build;
}