You are here

ImageViewBuilder.php in Twig Tweak 3.1.x

Same filename and directory in other branches
  1. 3.x src/View/ImageViewBuilder.php

File

src/View/ImageViewBuilder.php
View source
<?php

namespace Drupal\twig_tweak\View;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\file\FileInterface;

/**
 * Image view builder.
 */
class ImageViewBuilder {

  /**
   * Builds an image.
   *
   * @param \Drupal\file\FileInterface $file
   *   The file object.
   * @param string $style
   *   (optional) Image style.
   * @param array $attributes
   *   (optional) Image attributes.
   * @param bool $responsive
   *   (optional) Indicates that the provided image style is responsive.
   * @param bool $check_access
   *   (optional) Indicates that access check is required.
   *
   * @return array
   *   A renderable array to represent the image.
   */
  public function build(FileInterface $file, string $style = NULL, array $attributes = [], bool $responsive = FALSE, bool $check_access = TRUE) : array {
    $access = $check_access ? $file
      ->access('view', NULL, TRUE) : AccessResult::allowed();
    $build = [];
    if ($access
      ->isAllowed()) {
      $build['#uri'] = $file
        ->getFileUri();
      $build['#attributes'] = $attributes;
      if ($style) {
        if ($responsive) {
          $build['#type'] = 'responsive_image';
          $build['#responsive_image_style_id'] = $style;
        }
        else {
          $build['#theme'] = 'image_style';
          $build['#style_name'] = $style;
        }
      }
      else {
        $build['#theme'] = 'image';
      }
    }
    CacheableMetadata::createFromRenderArray($build)
      ->merge(CacheableMetadata::createFromObject($access))
      ->applyTo($build);
    return $build;
  }

}

Classes

Namesort descending Description
ImageViewBuilder Image view builder.