Skip to content
Snippets Groups Projects
Commit b332f08e authored by Angie Byron's avatar Angie Byron
Browse files

Issue #2534066 by Jelle_S, Dom., attiks: Allow selecting the original image...

Issue #2534066 by Jelle_S, Dom., attiks: Allow selecting the original image when creating a responsive image style
parent 932f2f2c
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
* The machine name for the empty image breakpoint image style option. * The machine name for the empty image breakpoint image style option.
*/ */
const RESPONSIVE_IMAGE_EMPTY_IMAGE = '_empty image_'; const RESPONSIVE_IMAGE_EMPTY_IMAGE = '_empty image_';
const RESPONSIVE_IMAGE_ORIGINAL_IMAGE = '_original image_';
/** /**
* Implements hook_help(). * Implements hook_help().
...@@ -476,7 +477,12 @@ function responsive_image_get_mime_type($image_style_name, $extension) { ...@@ -476,7 +477,12 @@ function responsive_image_get_mime_type($image_style_name, $extension) {
} }
// The MIME type guesser needs a full path, not just an extension, but the // The MIME type guesser needs a full path, not just an extension, but the
// file doesn't have to exist. // file doesn't have to exist.
if ($image_style_name === RESPONSIVE_IMAGE_ORIGINAL_IMAGE) {
$fake_path = 'responsive_image.' . $extension;
}
else {
$fake_path = 'responsive_image.' . ImageStyle::load($image_style_name)->getDerivativeExtension($extension); $fake_path = 'responsive_image.' . ImageStyle::load($image_style_name)->getDerivativeExtension($extension);
}
return Drupal::service('file.mime_type.guesser.extension')->guess($fake_path); return Drupal::service('file.mime_type.guesser.extension')->guess($fake_path);
} }
......
...@@ -84,6 +84,7 @@ public function form(array $form, FormStateInterface $form_state) { ...@@ -84,6 +84,7 @@ public function form(array $form, FormStateInterface $form_state) {
); );
$image_styles = image_style_options(TRUE); $image_styles = image_style_options(TRUE);
$image_styles[RESPONSIVE_IMAGE_ORIGINAL_IMAGE] = $this->t('- None (original image) -');
$image_styles[RESPONSIVE_IMAGE_EMPTY_IMAGE] = $this->t('- empty image -'); $image_styles[RESPONSIVE_IMAGE_EMPTY_IMAGE] = $this->t('- empty image -');
if ((bool) $responsive_image_style->id() && $this->operation != 'duplicate') { if ((bool) $responsive_image_style->id() && $this->operation != 'duplicate') {
......
...@@ -77,7 +77,10 @@ public function testResponsiveImageAdmin() { ...@@ -77,7 +77,10 @@ public function testResponsiveImageAdmin() {
array('wide', '1x'), array('wide', '1x'),
array('wide', '2x'), array('wide', '2x'),
); );
$image_styles = array_merge(
[RESPONSIVE_IMAGE_EMPTY_IMAGE, RESPONSIVE_IMAGE_ORIGINAL_IMAGE],
array_keys(image_style_options(FALSE))
);
foreach ($cases as $case) { foreach ($cases as $case) {
// Check if the radio buttons are present. // Check if the radio buttons are present.
$this->assertFieldByName('keyed_styles[responsive_image_test_module.' . $case[0] . '][' . $case[1] . '][image_mapping_type]', ''); $this->assertFieldByName('keyed_styles[responsive_image_test_module.' . $case[0] . '][' . $case[1] . '][image_mapping_type]', '');
...@@ -85,8 +88,17 @@ public function testResponsiveImageAdmin() { ...@@ -85,8 +88,17 @@ public function testResponsiveImageAdmin() {
$this->assertFieldByName('keyed_styles[responsive_image_test_module.' . $case[0] . '][' . $case[1] . '][image_style]', ''); $this->assertFieldByName('keyed_styles[responsive_image_test_module.' . $case[0] . '][' . $case[1] . '][image_style]', '');
// Check if the sizes textfields are present. // Check if the sizes textfields are present.
$this->assertFieldByName('keyed_styles[responsive_image_test_module.' . $case[0] . '][' . $case[1] . '][sizes]', ''); $this->assertFieldByName('keyed_styles[responsive_image_test_module.' . $case[0] . '][' . $case[1] . '][sizes]', '');
foreach ($image_styles as $image_style_name) {
// Check if the image styles are available in the dropdowns.
$this->assertTrue($this->xpath(
'//select[@name=:name]//option[@value=:style]',
[
':name' => 'keyed_styles[responsive_image_test_module.' . $case[0] . '][' . $case[1] . '][image_style]',
':style' => $image_style_name,
]
));
// Check if the image styles checkboxes are present. // Check if the image styles checkboxes are present.
foreach (array_keys(image_style_options(FALSE)) as $image_style_name) {
$this->assertFieldByName('keyed_styles[responsive_image_test_module.' . $case[0] . '][' . $case[1] . '][sizes_image_styles][' . $image_style_name . ']'); $this->assertFieldByName('keyed_styles[responsive_image_test_module.' . $case[0] . '][' . $case[1] . '][sizes_image_styles][' . $image_style_name . ']');
} }
} }
......
...@@ -148,6 +148,11 @@ protected function addTestImageStyleMappings($empty_styles = FALSE) { ...@@ -148,6 +148,11 @@ protected function addTestImageStyleMappings($empty_styles = FALSE) {
'image_mapping_type' => 'image_style', 'image_mapping_type' => 'image_style',
'image_mapping' => 'large', 'image_mapping' => 'large',
)) ))
// Test the output of the original image.
->addImageStyleMapping('responsive_image_test_module.wide', '3x', array(
'image_mapping_type' => 'image_style',
'image_mapping' => RESPONSIVE_IMAGE_ORIGINAL_IMAGE,
))
->save(); ->save();
} }
} }
...@@ -286,6 +291,8 @@ protected function doTestResponsiveImageFieldFormatters($scheme, $empty_styles = ...@@ -286,6 +291,8 @@ protected function doTestResponsiveImageFieldFormatters($scheme, $empty_styles =
// Assert the output of the 'srcset' attribute (small multipliers first). // Assert the output of the 'srcset' attribute (small multipliers first).
$this->assertRaw('data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== 1x, ' . $thumbnail_style->buildUrl($image_uri) . ' 1.5x'); $this->assertRaw('data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== 1x, ' . $thumbnail_style->buildUrl($image_uri) . ' 1.5x');
$this->assertRaw('/styles/medium/'); $this->assertRaw('/styles/medium/');
// Assert the output of the original image.
$this->assertRaw(file_create_url($image_uri) . ' 3x');
// Assert the output of the breakpoints. // Assert the output of the breakpoints.
$this->assertRaw('media="(min-width: 0px)"'); $this->assertRaw('media="(min-width: 0px)"');
$this->assertRaw('media="(min-width: 560px)"'); $this->assertRaw('media="(min-width: 560px)"');
......
...@@ -208,6 +208,10 @@ public function testGetKeyedImageStyleMappings() { ...@@ -208,6 +208,10 @@ public function testGetKeyedImageStyleMappings() {
'image_mapping_type' => 'image_style', 'image_mapping_type' => 'image_style',
'image_mapping' => 'thumbnail', 'image_mapping' => 'thumbnail',
)); ));
$entity->addImageStyleMapping('test_breakpoint2', '2x', array(
'image_mapping_type' => 'image_style',
'image_mapping' => '_original image_',
));
$expected = array( $expected = array(
'test_breakpoint' => array( 'test_breakpoint' => array(
...@@ -236,6 +240,12 @@ public function testGetKeyedImageStyleMappings() { ...@@ -236,6 +240,12 @@ public function testGetKeyedImageStyleMappings() {
'image_mapping_type' => 'image_style', 'image_mapping_type' => 'image_style',
'image_mapping' => 'thumbnail', 'image_mapping' => 'thumbnail',
), ),
'2x' => array(
'breakpoint_id' => 'test_breakpoint2',
'multiplier' => '2x',
'image_mapping_type' => 'image_style',
'image_mapping' => '_original image_',
),
) )
); );
$this->assertEquals($expected, $entity->getKeyedImageStyleMappings()); $this->assertEquals($expected, $entity->getKeyedImageStyleMappings());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment