-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from fumikage/feat/libraryImage
feat: add getImage function for Twig component
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace TheliaLibrary\Twig; | ||
|
||
use TheliaBlocks\Service\JsonBlockService; | ||
use TheliaLibrary\Model\Base\LibraryImageQuery; | ||
use TheliaLibrary\Service\LibraryImageService; | ||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFunction; | ||
|
||
class LibraryImage extends AbstractExtension | ||
{ | ||
public function __construct( | ||
private JsonBlockService $jsonBlockService, | ||
private LibraryImageService $libraryImageService | ||
) { | ||
} | ||
|
||
public function getFunctions(): array | ||
{ | ||
return [ | ||
new TwigFunction('getImages', [$this, 'getImages']), | ||
|
||
]; | ||
} | ||
|
||
public function getImages(string $itemType ,int $itemId,int $width,int $height, string $format) { | ||
$query = LibraryImageQuery::create(); | ||
|
||
$images = $query | ||
->useLibraryItemImageQuery() | ||
->filterByItemType($itemType) | ||
->filterByItemId($itemId) | ||
->endUse(); | ||
|
||
|
||
|
||
$results = []; | ||
|
||
foreach($images as $image) { | ||
|
||
$imageUrl = $this->libraryImageService->getImagePublicUrl( | ||
$image, | ||
$width, | ||
$height, | ||
$format | ||
); | ||
|
||
$results[] = [ | ||
'URL' => $imageUrl, | ||
'ID' => $image->getId(), | ||
]; | ||
} | ||
return $results; | ||
|
||
|
||
} | ||
|
||
|
||
|
||
} |