Skip to content

Commit 12ccb78

Browse files
author
Stanislav Idolov
authored
ENGCOM-3159: Add class into image builder #18388
2 parents 110e7b6 + 445049f commit 12ccb78

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
namespace Magento\Catalog\Block\Product;
77

88
/**
9+
* Product image block
10+
*
911
* @api
1012
* @method string getImageUrl()
1113
* @method string getWidth()
1214
* @method string getHeight()
1315
* @method string getLabel()
1416
* @method float getRatio()
1517
* @method string getCustomAttributes()
18+
* @method string getClass()
1619
* @since 100.0.2
1720
*/
1821
class Image extends \Magento\Framework\View\Element\Template

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,29 @@ private function getStringCustomAttributes(array $attributes): string
7777
{
7878
$result = [];
7979
foreach ($attributes as $name => $value) {
80-
$result[] = $name . '="' . $value . '"';
80+
if ($name != 'class') {
81+
$result[] = $name . '="' . $value . '"';
82+
}
8183
}
8284
return !empty($result) ? implode(' ', $result) : '';
8385
}
8486

87+
/**
88+
* Retrieve image class for HTML element
89+
*
90+
* @param array $attributes
91+
* @return string
92+
*/
93+
private function getClass(array $attributes): string
94+
{
95+
return $attributes['class'] ?? 'product-image-photo';
96+
}
97+
8598
/**
8699
* Calculate image ratio
87100
*
88-
* @param $width
89-
* @param $height
101+
* @param int $width
102+
* @param int $height
90103
* @return float
91104
*/
92105
private function getRatio(int $width, int $height): float
@@ -98,8 +111,9 @@ private function getRatio(int $width, int $height): float
98111
}
99112

100113
/**
101-
* @param Product $product
114+
* Get image label
102115
*
116+
* @param Product $product
103117
* @param string $imageType
104118
* @return string
105119
*/
@@ -114,6 +128,7 @@ private function getLabel(Product $product, string $imageType): string
114128

115129
/**
116130
* Create image block from product
131+
*
117132
* @param Product $product
118133
* @param string $imageId
119134
* @param array|null $attributes
@@ -154,6 +169,7 @@ public function create(Product $product, string $imageId, array $attributes = nu
154169
'label' => $this->getLabel($product, $imageMiscParams['image_type']),
155170
'ratio' => $this->getRatio($imageMiscParams['image_width'], $imageMiscParams['image_height']),
156171
'custom_attributes' => $this->getStringCustomAttributes($attributes),
172+
'class' => $this->getClass($attributes),
157173
'product_id' => $product->getId()
158174
],
159175
];

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ private function getTestDataWithoutAttributes(): array
145145
'label' => 'test_image_label',
146146
'ratio' => 1,
147147
'custom_attributes' => '',
148-
'product_id' => null
148+
'product_id' => null,
149+
'class' => 'product-image-photo'
149150
],
150151
],
151152
];
@@ -190,6 +191,7 @@ private function getTestDataWithAttributes(): array
190191
'custom_attributes' => [
191192
'name_1' => 'value_1',
192193
'name_2' => 'value_2',
194+
'class' => 'my-class'
193195
],
194196
],
195197
'expected' => [
@@ -201,7 +203,8 @@ private function getTestDataWithAttributes(): array
201203
'label' => 'test_product_name',
202204
'ratio' => 0.5, // <==
203205
'custom_attributes' => 'name_1="value_1" name_2="value_2"',
204-
'product_id' => null
206+
'product_id' => null,
207+
'class' => 'my-class'
205208
],
206209
],
207210
];

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
style="width:<?= /* @escapeNotVerified */ $block->getWidth() ?>px;">
1111
<span class="product-image-wrapper"
1212
style="padding-bottom: <?= /* @escapeNotVerified */ ($block->getRatio() * 100) ?>%;">
13-
<img class="product-image-photo"
13+
<img class="<?= /* @escapeNotVerified */ $block->getClass() ?>"
1414
<?= /* @escapeNotVerified */ $block->getCustomAttributes() ?>
1515
src="<?= /* @escapeNotVerified */ $block->getImageUrl() ?>"
1616
max-width="<?= /* @escapeNotVerified */ $block->getWidth() ?>"

0 commit comments

Comments
 (0)