Skip to content

Commit

Permalink
Filter custom attributes and add changes to unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-aleman committed Feb 21, 2020
1 parent b0ccd4f commit 79025f8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
18 changes: 17 additions & 1 deletion app/code/Magento/Catalog/Block/Product/ImageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ public function __construct(
$this->imageParamsBuilder = $imageParamsBuilder;
}

/**
* Remove class from custom attributes
*
* @param array $attributes
* @return array
*/
private function filterCustomAttributes(array $attributes): array
{
if (isset($attributes['class'])) {
unset($attributes['class']);
}
return $attributes;
}

/**
* Retrieve image class for HTML element
*
Expand Down Expand Up @@ -153,12 +167,14 @@ public function create(Product $product, string $imageId, array $attributes = nu
'height' => $imageMiscParams['image_height'],
'label' => $this->getLabel($product, $imageMiscParams['image_type']),
'ratio' => $this->getRatio($imageMiscParams['image_width'], $imageMiscParams['image_height']),
'custom_attributes' => $attributes,
'custom_attributes' => $this->filterCustomAttributes($attributes),
'class' => $this->getClass($attributes),
'product_id' => $product->getId()
],
];

return $this->objectManager->create(ImageBlock::class, $data);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private function getTestDataWithoutAttributes(): array
'height' => 100,
'label' => 'test_image_label',
'ratio' => 1,
'custom_attributes' => '',
'custom_attributes' => [],
'product_id' => null,
'class' => 'product-image-photo'
],
Expand Down Expand Up @@ -202,7 +202,10 @@ private function getTestDataWithAttributes(): array
'height' => 50,
'label' => 'test_product_name',
'ratio' => 0.5, // <==
'custom_attributes' => 'name_1="value_1" name_2="value_2"',
'custom_attributes' => [
'name_1' => 'value_1',
'name_2' => 'value_2',
],
'product_id' => null,
'class' => 'my-class'
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

<img class="photo image <?= $block->escapeHtmlAttr($block->getClass()) ?>"
<?php foreach ($block->getCustomAttributes() as $name => $value): ?>
<?php if ($name !== 'class'): ?>
<?= $block->escapeHtmlAttr($name) ?>="<?= $block->escapeHtmlAttr($value) ?>"
<?php endif; ?>
<?= $block->escapeHtmlAttr($name) ?>="<?= $block->escapeHtmlAttr($value) ?>"
<?php endforeach; ?>
src="<?= $block->escapeUrl($block->getImageUrl()) ?>"
width="<?= $block->escapeHtmlAttr($block->getWidth()) ?>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
style="padding-bottom: <?= ($block->getRatio() * 100) ?>%;">
<img class="<?= $block->escapeHtmlAttr($block->getClass()) ?>"
<?php foreach ($block->getCustomAttributes() as $name => $value): ?>
<?php if ($name !== 'class'): ?>
<?= $block->escapeHtmlAttr($name) ?>="<?= $block->escapeHtmlAttr($value) ?>"
<?php endif; ?>
<?= $block->escapeHtmlAttr($name) ?>="<?= $block->escapeHtmlAttr($value) ?>"
<?php endforeach; ?>
src="<?= $block->escapeUrl($block->getImageUrl()) ?>"
max-width="<?= $block->escapeHtmlAttr($block->getWidth()) ?>"
Expand Down

0 comments on commit 79025f8

Please sign in to comment.