Skip to content

Commit

Permalink
feat: mobile variant methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolocarpignoli committed Jul 18, 2024
1 parent 724ebcd commit 670b3d5
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/View/Helper/PosterHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class PosterHelper extends Helper
{
protected const OBJECT_TYPE = 'images';

protected const MOBILE_DEFAULT_WIDTH = 640;

protected const MOBILE_MAX_WIDTH = 767;

/**
* @inheritDoc
*/
Expand Down Expand Up @@ -105,6 +109,53 @@ protected function candidates(ObjectEntity|null $object, array $options): Iterat
}
}

/**
* Check if object has a mobile variant.
*
* @param \BEdita\Core\Model\Entity\ObjectEntity|null $object Object entity.
* @return bool
*/
public function mobileExists(ObjectEntity|null $object): bool
{
return !empty($object['has_variant_mobile']);
}

/**
* Get mobile variant url, if any.
*
* @return string|null
*/
public function mobile(ObjectEntity|null $object): string|null
{
if (!$this->mobileExists($object)) {
return null;
}

$variant = $object['has_variant_mobile'][0];
$url = $this->url($variant);

if (!$url) {
return null;
}

$intrinsicWidth = Hash::get($variant, '_joinData.params.slot_width') || static::MOBILE_DEFAULT_WIDTH;

return sprintf('%s %sw', $url, $intrinsicWidth);
}

/**
* Get sizes attribute for image.
*
* @return string
*/
public function sizes(): string
{
$maxWidth = $this->getConfig('PosterMobile.maxWidth', static::MOBILE_MAX_WIDTH);
$slotWidth = $this->getConfig('PosterMobile.slotWidth', static::MOBILE_DEFAULT_WIDTH);

return sprintf('(max-width: %spx) %spx', $maxWidth, $slotWidth);
}

/**
* Check if object has a valid poster, or is an Image itself, and the referenced file does actually exist.
*
Expand Down

0 comments on commit 670b3d5

Please sign in to comment.