Browse Source

#3356042: Clean-up image builder

3.x
Chi 10 months ago
parent
commit
4b72a2c3da
  1. 51
      src/View/ImageViewBuilder.php
  2. 51
      tests/src/Kernel/ImageViewBuilderTest.php

51
src/View/ImageViewBuilder.php

@ -44,26 +44,43 @@ class ImageViewBuilder {
* A renderable array to represent the image. * 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 { 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(); $access = $check_access ? $file->access('view', NULL, TRUE) : AccessResult::allowed();
$build = []; $build = $access->isAllowed() ? $this->doBuild($file, $style, $attributes, $responsive) : [];
if ($access->isAllowed()) {
CacheableMetadata::createFromRenderArray($build)
->addCacheableDependency($access)
->addCacheableDependency($file)
->applyTo($build);
return $build;
}
/**
* Actually builds the image.
*/
private function doBuild(FileInterface $file, string $style = NULL, array $attributes = [], bool $responsive = FALSE): array {
$build['#uri'] = $file->getFileUri(); $build['#uri'] = $file->getFileUri();
$build['#attributes'] = $attributes; $build['#attributes'] = $attributes;
if ($style) {
// If an image style is given, image module needs the original if (!$style) {
// image dimensions to calculate image style's $build['#theme'] = 'image';
// width and height and set the attributes. return $build;
// See https://www.drupal.org/project/twig_tweak/issues/3356042 }
$uri = $file->getFileUri();
$image = $this->imageFactory->get($uri); $build['#width'] = $attributes['width'] ?? NULL;
$build['#height'] = $attributes['height'] ?? NULL;
if (!$build['#width'] && !$build['#height']) {
// If an image style is given, image module needs the original image
// dimensions to calculate image style's width and height and set the
// attributes.
// @see https://www.drupal.org/project/twig_tweak/issues/3356042
$image = $this->imageFactory->get($file->getFileUri());
if ($image->isValid()) { if ($image->isValid()) {
$build['#width'] = $image->getWidth(); $build['#width'] = $image->getWidth();
$build['#height'] = $image->getHeight(); $build['#height'] = $image->getHeight();
} }
else {
$build['#width'] = $build['#height'] = NULL;
} }
if ($responsive) { if ($responsive) {
@ -74,16 +91,6 @@ class ImageViewBuilder {
$build['#theme'] = 'image_style'; $build['#theme'] = 'image_style';
$build['#style_name'] = $style; $build['#style_name'] = $style;
} }
}
else {
$build['#theme'] = 'image';
}
}
CacheableMetadata::createFromRenderArray($build)
->addCacheableDependency($access)
->addCacheableDependency($file)
->applyTo($build);
return $build; return $build;
} }

51
tests/src/Kernel/ImageViewBuilderTest.php

@ -32,28 +32,14 @@ final class ImageViewBuilderTest extends AbstractTestCase {
]; ];
/** /**
* The ImageStyle. * The public image URI.
*
* @var \Drupal\image\Entity\ImageStyle
*/
protected $imageStyle;
/**
* The ResponsiveImageStyle.
*
* @var \Drupal\responsive_image\Entity\ResponsiveImageStyle
*/
protected $responsiveImageStyle;
/**
* The public image uri.
* *
* @var string * @var string
*/ */
protected string $publicImageUri; protected string $publicImageUri;
/** /**
* The private image uri. * The private image URI.
* *
* @var string * @var string
*/ */
@ -82,20 +68,20 @@ final class ImageViewBuilderTest extends AbstractTestCase {
$this->installEntitySchema('file'); $this->installEntitySchema('file');
$this->installSchema('file', 'file_usage'); $this->installSchema('file', 'file_usage');
$fileSystemService = \Drupal::service('file_system'); $file_system = $this->container->get('file_system');
$file_system->prepareDirectory($this->siteDirectory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
$private_directory = $this->siteDirectory . '/private';
$fileSystemService->prepareDirectory($this->siteDirectory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS); $file_system->prepareDirectory($private_directory, FileSystemInterface::CREATE_DIRECTORY);
$privateFilesDirectory = $this->siteDirectory . '/private'; $this->setSetting('file_private_path', $private_directory);
$fileSystemService->prepareDirectory($privateFilesDirectory, FileSystemInterface::CREATE_DIRECTORY);
$this->setSetting('file_private_path', $privateFilesDirectory);
$this->imageStyle = ImageStyle::create([ $image_style = ImageStyle::create([
'name' => 'small', 'name' => 'small',
'label' => 'Small', 'label' => 'Small',
]); ]);
$this->imageStyle->save();
// Add a crop effect: // Add a crop effect:
$this->imageStyle->addImageEffect([ $image_style->addImageEffect([
'id' => 'image_resize', 'id' => 'image_resize',
'data' => [ 'data' => [
'width' => 10, 'width' => 10,
@ -103,20 +89,19 @@ final class ImageViewBuilderTest extends AbstractTestCase {
], ],
'weight' => 0, 'weight' => 0,
]); ]);
$this->imageStyle->save(); $image_style->save();
$this->responsiveImageStyle = ResponsiveImageStyle::create([ $responsive_image_style = ResponsiveImageStyle::create([
'id' => 'wide', 'id' => 'wide',
'label' => 'Wide', 'label' => 'Wide',
'breakpoint_group' => 'twig_tweak_image_view_builder', 'breakpoint_group' => 'twig_tweak_image_view_builder',
'fallback_image_style' => 'small', 'fallback_image_style' => 'small',
]); ]);
$this->responsiveImageStyle->save(); $responsive_image_style->save();
// Create a copy of a test image file in root. // Create a copy of a test image file in root. Original sizes: 40x20px.
// Original sizes: 40x20px.
$this->publicImageUri = 'public://image-test-do.jpg'; $this->publicImageUri = 'public://image-test-do.jpg';
$fileSystemService->copy('core/tests/fixtures/files/image-test.jpg', $this->publicImageUri, FileSystemInterface::EXISTS_REPLACE); $file_system->copy('core/tests/fixtures/files/image-test.jpg', $this->publicImageUri, FileSystemInterface::EXISTS_REPLACE);
$this->assertFileExists($this->publicImageUri); $this->assertFileExists($this->publicImageUri);
$this->publicImage = File::create([ $this->publicImage = File::create([
'uri' => $this->publicImageUri, 'uri' => $this->publicImageUri,
@ -124,11 +109,9 @@ final class ImageViewBuilderTest extends AbstractTestCase {
]); ]);
$this->publicImage->save(); $this->publicImage->save();
// Create a copy of a test image file in root. // Create a copy of a test image file in root. Original sizes: 40x20px.
// Original sizes: 40x20px.
$this->privateImageUri = 'private://image-test-do.png'; $this->privateImageUri = 'private://image-test-do.png';
$fileSystemService $file_system->copy('core/tests/fixtures/files/image-test.png', $this->privateImageUri, FileSystemInterface::EXISTS_REPLACE);
->copy('core/tests/fixtures/files/image-test.png', $this->privateImageUri, FileSystemInterface::EXISTS_REPLACE);
$this->assertFileExists($this->privateImageUri); $this->assertFileExists($this->privateImageUri);
$this->privateImage = File::create([ $this->privateImage = File::create([
'uri' => $this->privateImageUri, 'uri' => $this->privateImageUri,

Loading…
Cancel
Save