Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

ImageExtension.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension;
8
9 use eZ\Publish\API\Repository\Exceptions\InvalidVariationException;
10 use eZ\Publish\API\Repository\Values\Content\Field;
11 use eZ\Publish\API\Repository\Values\Content\VersionInfo;
12 use eZ\Publish\Core\FieldType\ImageAsset\AssetMapper;
13 use eZ\Publish\Core\MVC\Exception\SourceImageNotFoundException;
14 use eZ\Publish\SPI\Variation\VariationHandler;
15 use InvalidArgumentException;
16 use Twig_Extension;
17 use Twig_SimpleFunction;
18
19 class ImageExtension extends Twig_Extension
20 {
22 private $imageVariationService;
23
25 protected $assetMapper;
26
27 public function __construct(VariationHandler $imageVariationService, AssetMapper $assetMapper)
28 {
29 $this->imageVariationService = $imageVariationService;
30 $this->assetMapper = $assetMapper;
31 }
32
33 public function getName()
34 {
35 return 'ezpublish.image';
36 }
37
38 public function getFunctions()
39 {
40 return [
41 new Twig_SimpleFunction(
42 'ez_image_alias',
43 [$this, 'getImageVariation'],
44 ['is_safe' => ['html']]
45 ),
46 new Twig_SimpleFunction(
47 'ez_image_asset_content_field_identifier',
48 [$this, 'getImageAssetContentFieldIdentifier'],
49 ['is_safe' => ['html']]
50 ),
51 ];
52 }
53
63 public function getImageVariation(Field $field, VersionInfo $versionInfo, $variationName)
64 {
65 try {
66 return $this->imageVariationService->getVariation($field, $versionInfo, $variationName);
67 } catch (InvalidVariationException $e) {
68 if (isset($this->logger)) {
69 $this->logger->error("Couldn't get variation '{$variationName}' for image with id {$field->value->id}");
70 }
71 } catch (SourceImageNotFoundException $e) {
72 if (isset($this->logger)) {
73 $this->logger->error(
74 "Couldn't create variation '{$variationName}' for image with id {$field->value->id} because source image can't be found"
75 );
76 }
77 } catch (InvalidArgumentException $e) {
78 if (isset($this->logger)) {
79 $this->logger->error(
80 "Couldn't create variation '{$variationName}' for image with id {$field->value->id} because an image could not be created from the given input"
81 );
82 }
83 }
84 }
85
93 public function getImageAssetContentFieldIdentifier(): string
94 {
95 return $this->assetMapper->getContentFieldIdentifier();
96 }
97 }
eZ\Publish\API\Repository\Exceptions\InvalidVariationException
Definition: InvalidVariationException.php:12
eZ\Publish\API\Repository\Values\Content\Field
Definition: API/Repository/Values/Content/Field.php:21
eZ\Publish\API\Repository\Values\Content\VersionInfo
Definition: API/Repository/Values/Content/VersionInfo.php:27
eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
Invalid Argument Type Exception implementation.
Definition: Core/Base/Exceptions/InvalidArgumentException.php:20
eZ\Publish\Core\FieldType\ImageAsset\AssetMapper
Definition: AssetMapper.php:21
eZ\Publish\Core\MVC\Exception\SourceImageNotFoundException
Definition: SourceImageNotFoundException.php:12
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\ImageExtension
Definition: ImageExtension.php:20
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\ImageExtension\$imageVariationService
$imageVariationService
Definition: ImageExtension.php:22
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\ImageExtension\getImageVariation
getImageVariation(Field $field, VersionInfo $versionInfo, $variationName)
Returns the image variation object for $field/$versionInfo.
Definition: ImageExtension.php:63
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\ImageExtension\__construct
__construct(VariationHandler $imageVariationService, AssetMapper $assetMapper)
Definition: ImageExtension.php:27
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\ImageExtension\getFunctions
getFunctions()
Definition: ImageExtension.php:38
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\ImageExtension\getImageAssetContentFieldIdentifier
getImageAssetContentFieldIdentifier()
Return identifier of the field used to store Image Asset value.
Definition: ImageExtension.php:93
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\ImageExtension\$assetMapper
$assetMapper
Definition: ImageExtension.php:25
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\ImageExtension\getName
getName()
Definition: ImageExtension.php:33
eZ\Publish\SPI\Variation\VariationHandler
Interface for Variation services.
Definition: VariationHandler.php:18
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension
Definition: ContentExtension.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.