Skip to content

Commit 79025f8

Browse files
Filter custom attributes and add changes to unit test
1 parent b0ccd4f commit 79025f8

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

app/code/Magento/Catalog/Block/Product/ImageFactory.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ public function __construct(
6767
$this->imageParamsBuilder = $imageParamsBuilder;
6868
}
6969

70+
/**
71+
* Remove class from custom attributes
72+
*
73+
* @param array $attributes
74+
* @return array
75+
*/
76+
private function filterCustomAttributes(array $attributes): array
77+
{
78+
if (isset($attributes['class'])) {
79+
unset($attributes['class']);
80+
}
81+
return $attributes;
82+
}
83+
7084
/**
7185
* Retrieve image class for HTML element
7286
*
@@ -153,12 +167,14 @@ public function create(Product $product, string $imageId, array $attributes = nu
153167
'height' => $imageMiscParams['image_height'],
154168
'label' => $this->getLabel($product, $imageMiscParams['image_type']),
155169
'ratio' => $this->getRatio($imageMiscParams['image_width'], $imageMiscParams['image_height']),
156-
'custom_attributes' => $attributes,
170+
'custom_attributes' => $this->filterCustomAttributes($attributes),
157171
'class' => $this->getClass($attributes),
158172
'product_id' => $product->getId()
159173
],
160174
];
161175

162176
return $this->objectManager->create(ImageBlock::class, $data);
163177
}
178+
179+
164180
}

app/code/Magento/Catalog/Test/Unit/Block/Product/ImageFactoryTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ private function getTestDataWithoutAttributes(): array
144144
'height' => 100,
145145
'label' => 'test_image_label',
146146
'ratio' => 1,
147-
'custom_attributes' => '',
147+
'custom_attributes' => [],
148148
'product_id' => null,
149149
'class' => 'product-image-photo'
150150
],
@@ -202,7 +202,10 @@ private function getTestDataWithAttributes(): array
202202
'height' => 50,
203203
'label' => 'test_product_name',
204204
'ratio' => 0.5, // <==
205-
'custom_attributes' => 'name_1="value_1" name_2="value_2"',
205+
'custom_attributes' => [
206+
'name_1' => 'value_1',
207+
'name_2' => 'value_2',
208+
],
206209
'product_id' => null,
207210
'class' => 'my-class'
208211
],

app/code/Magento/Catalog/view/frontend/templates/product/image.phtml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88

99
<img class="photo image <?= $block->escapeHtmlAttr($block->getClass()) ?>"
1010
<?php foreach ($block->getCustomAttributes() as $name => $value): ?>
11-
<?php if ($name !== 'class'): ?>
12-
<?= $block->escapeHtmlAttr($name) ?>="<?= $block->escapeHtmlAttr($value) ?>"
13-
<?php endif; ?>
11+
<?= $block->escapeHtmlAttr($name) ?>="<?= $block->escapeHtmlAttr($value) ?>"
1412
<?php endforeach; ?>
1513
src="<?= $block->escapeUrl($block->getImageUrl()) ?>"
1614
width="<?= $block->escapeHtmlAttr($block->getWidth()) ?>"

app/code/Magento/Catalog/view/frontend/templates/product/image_with_borders.phtml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
style="padding-bottom: <?= ($block->getRatio() * 100) ?>%;">
1313
<img class="<?= $block->escapeHtmlAttr($block->getClass()) ?>"
1414
<?php foreach ($block->getCustomAttributes() as $name => $value): ?>
15-
<?php if ($name !== 'class'): ?>
16-
<?= $block->escapeHtmlAttr($name) ?>="<?= $block->escapeHtmlAttr($value) ?>"
17-
<?php endif; ?>
15+
<?= $block->escapeHtmlAttr($name) ?>="<?= $block->escapeHtmlAttr($value) ?>"
1816
<?php endforeach; ?>
1917
src="<?= $block->escapeUrl($block->getImageUrl()) ?>"
2018
max-width="<?= $block->escapeHtmlAttr($block->getWidth()) ?>"

0 commit comments

Comments
 (0)