Skip to content

Commit

Permalink
fix: sizes and slot width
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo authored and nicolo committed Jul 25, 2024
1 parent d65f7da commit 6ede64c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
17 changes: 14 additions & 3 deletions src/View/Helper/PosterHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ public function sourceSet(ObjectEntity|null $object, array|string|false $thumbOp
}

$url = $this->url($variant, $thumbOptions, $posterOptions);
$width = $this->getStreamWidth($variant);
$slotWidth = $this->getSlotWidth($variant);

return sprintf('%s %sw, %s %sw', $url, $width, $fallbackUrl, $fallbackWidth);
return sprintf('%s %sw, %s %sw', $url, $slotWidth, $fallbackUrl, $fallbackWidth);
}

/**
Expand All @@ -182,6 +182,17 @@ protected function getStreamWidth(Media $media): int
return Hash::get($media, 'streams.0.width', $this->getConfig('PosterMobile.slotWidth', static::MOBILE_DEFAULT_WIDTH));
}

/**
* Get the slot width of a media object.
*
* @param \BEdita\Core\Model\Entity\Media $variant Media entity.
* @return int
*/
protected function getSlotWidth(Media $variant): int
{
return Hash::get($variant ?? [], '_joinData.params.slot_width', $this->getConfig('PosterMobile.slotWidth', static::MOBILE_DEFAULT_WIDTH));
}

/**
* Get sizes attribute for image.
*
Expand All @@ -192,7 +203,7 @@ public function sizes(Media $poster): string
{
$variant = $this->mobile($poster);
$maxWidth = $this->getConfig('PosterMobile.maxWidth', static::MOBILE_MAX_WIDTH);
$slotWidth = Hash::get($variant ?? [], '_joinData.params.slot_width', $this->getConfig('PosterMobile.slotWidth', static::MOBILE_DEFAULT_WIDTH));
$slotWidth = $this->getSlotWidth($variant);

return sprintf('(max-width: %spx) %spx', $maxWidth, $slotWidth);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/View/Helper/PosterHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public function testSourceSet()
// no variant
$this->assertSame('https://www.bedita.com/first.png 1500w', $this->Poster->sourceSet($image));

$this->setVariantProviderThumbnail($image, 640, 480);
$this->setVariantProviderThumbnail($image, 2000, 480);

// variant available
$this->assertSame('https://www.bedita.com/favicon.png 640w, https://www.bedita.com/first.png 1500w', $this->Poster->sourceSet($image));
Expand All @@ -219,7 +219,7 @@ public function testSourceSet()
public function testSizes()
{
$image = $this->createImage(1500, 1000);
$this->setVariantProviderThumbnail($image, 640, 480);
$this->setVariantProviderThumbnail($image, 2000, 480);

// no slot_width set, using image width
$this->assertSame('(max-width: 767px) 640px', $this->Poster->sizes($image));
Expand Down

0 comments on commit 6ede64c

Please sign in to comment.