Skip to content

Commit

Permalink
Added deferred option
Browse files Browse the repository at this point in the history
  • Loading branch information
mcop1 committed Oct 24, 2024
1 parent 029f58c commit c5d427f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/GraphQL/AssetType/AssetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public function build(&$config)
'args' => [
'thumbnail' => ['type' => Type::string()],
'format' => ['type' => Type::string()],
'deferred' => ['type' => Type::boolean(), 'defaultValue' => false],
],
'resolve' => [$resolver, 'resolvePath'],
],
Expand Down Expand Up @@ -140,6 +141,7 @@ public function build(&$config)
'args' => [
'thumbnail' => ['type' => Type::nonNull(Type::string())],
'format' => ['type' => Type::string()],
'deferred' => ['type' => Type::boolean(), 'defaultValue' => false],
],
'resolve' => [$resolver, 'resolveSrcSet'],
],
Expand Down
20 changes: 15 additions & 5 deletions src/GraphQL/FieldHelper/AssetFieldHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,19 @@ public function getVideoThumbnail(Asset\Video $asset, string | Video\Thumbnail\C
return null;
}

public function getImageDocumentThumbnail(Asset $asset, string | Image\Thumbnail\Config $thumbNailConfig, string $thumbNailFormat = null): mixed
public function getImageDocumentThumbnail(
Asset $asset,
string | Image\Thumbnail\Config $thumbNailConfig,
string $thumbNailFormat = null,
bool $deferred = false
): mixed
{
$thumb = null;

if ($asset instanceof Asset\Document || $asset instanceof Asset\Video) {
$thumb = $asset->getImageThumbnail($thumbNailConfig);
$thumb = $asset->getImageThumbnail($thumbNailConfig, $deferred);

Check failure on line 54 in src/GraphQL/FieldHelper/AssetFieldHelper.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.1, lowest, false)

Parameter #2 $page|timeOffset of method Pimcore\Model\Asset\Document::getImageThumbnail() expects int|null, bool given.

Check failure on line 54 in src/GraphQL/FieldHelper/AssetFieldHelper.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, highest, true)

Parameter #2 $page|timeOffset of method Pimcore\Model\Asset\Document::getImageThumbnail() expects int|null, bool given.

Check failure on line 54 in src/GraphQL/FieldHelper/AssetFieldHelper.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3, highest, 11.x-dev, true, true)

Parameter #2 $page|timeOffset of method Pimcore\Model\Asset\Document::getImageThumbnail() expects int|null, bool given.
} elseif ($asset instanceof Asset\Image) {
$thumb = $asset->getThumbnail($thumbNailConfig, false);
$thumb = $asset->getThumbnail($thumbNailConfig, $deferred);
}
if (isset($thumb, $thumbNailFormat) && method_exists($thumb, 'getAsFormat') && !($asset instanceof Asset\Video)) {
$thumb = $thumb->getAsFormat($thumbNailFormat);
Expand All @@ -57,12 +62,17 @@ public function getImageDocumentThumbnail(Asset $asset, string | Image\Thumbnail
return $thumb;
}

public function getAssetThumbnail(Asset $asset, string | Image\Thumbnail\Config | Video\Thumbnail\Config $thumbNailConfig, string $thumbNailFormat = null): mixed
public function getAssetThumbnail(
Asset $asset,
string | Image\Thumbnail\Config | Video\Thumbnail\Config $thumbNailConfig,
string $thumbNailFormat = null,
bool $deferred = false
): mixed
{
if (($asset instanceof Asset\Video) && (is_string($thumbNailConfig) || $thumbNailConfig instanceof Video\Thumbnail\Config)) {
return $this->getVideoThumbnail($asset, $thumbNailConfig, $thumbNailFormat);
} else {
return $this->getImageDocumentThumbnail($asset, $thumbNailConfig, $thumbNailFormat);
return $this->getImageDocumentThumbnail($asset, $thumbNailConfig, $thumbNailFormat, $deferred);
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/GraphQL/Resolver/AssetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,14 @@ public function resolvePath($value = null, $args = [], $context = [], ResolveInf
$asset = $this->getAssetFromValue($value, $context);
$thumbNailConfig = $args['thumbnail'] ?? null;
$thumbNailFormat = $args['format'] ?? null;
$deferred = $args['deferred'] ?? false;
$assetFieldHelper = $this->getGraphQLService()->getAssetFieldHelper();

if (!isset($thumbNailConfig)) {
return $asset->getFullPath();
}

return $assetFieldHelper->getAssetThumbnail($asset, $thumbNailConfig, $thumbNailFormat);
return $assetFieldHelper->getAssetThumbnail($asset, $thumbNailConfig, $thumbNailFormat, $deferred);
}

/**
Expand All @@ -170,12 +171,13 @@ public function resolveData($value = null, $args = [], $context = [], ResolveInf
$asset = $this->getAssetFromValue($value, $context);
$thumbNailConfig = $args['thumbnail'] ?? null;
$thumbNailFormat = $args['format'] ?? null;
$deferred = $args['deferred'] ?? false;
$assetFieldHelper = $this->getGraphQLService()->getAssetFieldHelper();

if (!isset($thumbNailConfig)) {
return base64_encode(stream_get_contents($asset->getStream()));
}
$thumb = $assetFieldHelper->getAssetThumbnail($asset, $thumbNailConfig, $thumbNailFormat);
$thumb = $assetFieldHelper->getAssetThumbnail($asset, $thumbNailConfig, $thumbNailFormat, $deferred);

return $thumb ? base64_encode(stream_get_contents($thumb->getStream())) : base64_encode(stream_get_contents($asset->getStream()));
}
Expand All @@ -194,12 +196,13 @@ public function resolveSrcSet($value = null, $args = [], $context = [], ResolveI
$asset = $this->getAssetFromValue($value, $context);
$thumbNailConfig = $args['thumbnail'] ?? null;
$thumbNailFormat = $args['format'] ?? null;
$deferred = $args['deferred'] ?? null;
$assetFieldHelper = $this->getGraphQLService()->getAssetFieldHelper();

if ($asset instanceof Asset\Image) {
$mediaQueries = [];
$thumbnail = $assetFieldHelper->getAssetThumbnail($asset, $thumbNailConfig, $thumbNailFormat);
$thumbnailConfig = $asset->getThumbnail($args['thumbnail'])->getConfig();
$thumbnail = $assetFieldHelper->getAssetThumbnail($asset, $thumbNailConfig, $thumbNailFormat, $deferred);
$thumbnailConfig = $asset->getThumbnail($args['thumbnail'], $deferred)->getConfig();
if ($thumbnailConfig) {
foreach ($thumbnailConfig->getMedias() as $key => $val) {
$mediaQueries[] = [
Expand Down

0 comments on commit c5d427f

Please sign in to comment.