You are here

class ImageViewBuilder in Twig Tweak 3.1.x

Same name and namespace in other branches
  1. 3.x src/View/ImageViewBuilder.php \Drupal\twig_tweak\View\ImageViewBuilder

Image view builder.

Hierarchy

Expanded class hierarchy of ImageViewBuilder

1 string reference to 'ImageViewBuilder'
twig_tweak.services.yml in ./twig_tweak.services.yml
twig_tweak.services.yml
1 service uses ImageViewBuilder
twig_tweak.image_view_builder in ./twig_tweak.services.yml
Drupal\twig_tweak\View\ImageViewBuilder

File

src/View/ImageViewBuilder.php, line 12

Namespace

Drupal\twig_tweak\View
View source
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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ImageViewBuilder::build public function Builds an image.