• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

heimrichhannot / contao-utils-bundle / 5892839579

17 Aug 2023 03:23PM UTC coverage: 22.549%. Remained the same
5892839579

push

github

koertho
fixed missing alt texts in image twig filter

1 of 1 new or added line in 1 file covered. (100.0%)

1196 of 5304 relevant lines covered (22.55%)

1.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Twig/ImageExtension.php
1
<?php
2

3
/*
4
 * Copyright (c) 2023 Heimrich & Hannot GmbH
5
 *
6
 * @license LGPL-3.0-or-later
7
 */
8

9
namespace HeimrichHannot\UtilsBundle\Twig;
10

11
use Contao\Controller;
12
use Contao\FilesModel;
13
use Contao\FrontendTemplate;
14
use Contao\StringUtil;
15
use Contao\Validator;
16
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
17
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
18
use Twig\Extension\AbstractExtension;
19
use Twig\TwigFilter;
20

21
class ImageExtension extends AbstractExtension implements ContainerAwareInterface
22
{
23
    use ContainerAwareTrait;
24

25
    /**
26
     * Get list of twig filters.
27
     *
28
     * @return array|\Twig_SimpleFilter[]
29
     */
30
    public function getFilters()
31
    {
32
        return [
×
33
            new TwigFilter('image', [$this, 'getImage']),
×
34
            new TwigFilter('image_caption', [$this, 'getImageCaption']),
×
35
            new TwigFilter('image_width', [$this, 'getImageWidth']),
×
36
            new TwigFilter('image_data', [$this, 'getImageData']),
×
37
            new TwigFilter('image_gallery', [$this, 'getImageGallery']),
×
38
            new TwigFilter('image_size', [$this, 'getImageSize']),
×
39
        ];
×
40
    }
41

42
    /**
43
     * Get image based on given path/uuid.
44
     *
45
     * @param mixed        $image    File path/uuid
46
     * @param string|array $size     Array or serialized string containing [width, height, imageSize-ID]
47
     * @param array        $data     Add image data here [href => 'URL', class => 'img css class']…
48
     * @param string       $template Use custom image template
49
     *
50
     * @throws \Psr\Cache\InvalidArgumentException
51
     * @throws \Twig_Error_Loader
52
     * @throws \Twig_Error_Runtime
53
     * @throws \Twig_Error_Syntax
54
     *
55
     * @return string Image html element with given size
56
     */
57
    public function getImage($image, $size = null, array $data = [], string $template = 'image.html.twig'): string
58
    {
59
        $imageData = $this->getImageData($image, $size, $data);
×
60

61
        if (empty($imageData)) {
×
62
            return '';
×
63
        }
64

65
        return $this->container->get('huh.utils.template')->renderTwigTemplate($template, $imageData);
×
66
    }
67

68
    /**
69
     * Get image data based on given path/uuid.
70
     *
71
     * @param mixed        $image File path/uuid
72
     * @param string|array $size  Array or serialized string containing [width, height, imageSize-ID]
73
     * @param array        $data  Add image data here [href => 'URL', class => 'img css class']…
74
     *
75
     * @return array Image data
76
     */
77
    public function getImageData($image, $size = null, array $data = []): array
78
    {
79
        $imageData = [];
×
80

81
        $data['singleSRC'] = $image;
×
82
        $size = $data['size'] = \is_array($size) ? $size : StringUtil::deserialize($size);
×
83

84
        // remove empty imageSize passed in
85
        if (empty($size) || (\is_array($size) && empty(array_filter($size)))) {
×
86
            unset($data['size']);
×
87
        }
88

89
//        $this->container->get('huh.utils.image')->addToTemplateData('image', 'addImage', $imageData, $data);
90

91
        if (Validator::isUuid($image)) {
×
92
            $fileModel = FilesModel::findByUuid($image);
×
93

94
            if (!$fileModel) {
×
95
                return [];
×
96
            }
97
            $data['singleSRC'] = $fileModel->path;
×
98
        }
99

100
        $template = new FrontendTemplate();
×
101
        Controller::addImageToTemplate($template, $data, null, null, $fileModel);
×
102

103
        return $template->getData();
×
104

105
        if (empty($imageData)) {
×
106
            return [];
×
107
        }
108

109
        $result = array_merge($imageData, $data);
×
110

111
        if (isset($imageData['picture']) && isset($data['picture'])) {
×
112
            $result['picture'] = array_merge($imageData['picture'], $data['picture']);
×
113
        }
114

115
        return $result;
×
116
    }
117

118
    /**
119
     * Get image caption based on given path/uuid.
120
     *
121
     * @param mixed $image File path/uuid
122
     *
123
     * @return string|null Image caption if available, else null
124
     */
125
    public function getImageCaption($image): ?string
126
    {
127
        if (null === ($file = $this->container->get('huh.utils.file')->getFileFromUuid($image))) {
×
128
            return null;
×
129
        }
130

131
        $meta = StringUtil::deserialize($file->getModel()->meta, true);
×
132

133
        if (!isset($meta[$GLOBALS['TL_LANGUAGE']]['caption'])) {
×
134
            return null;
×
135
        }
136

137
        return $meta[$GLOBALS['TL_LANGUAGE']]['caption'];
×
138
    }
139

140
    /**
141
     * Get image width based on given path/uuid.
142
     *
143
     * @param mixed $image File path/uuid
144
     *
145
     * @return string|null Image caption if available, else null
146
     */
147
    public function getImageWidth($image): ?string
148
    {
149
        if (null === ($file = $this->container->get('huh.utils.file')->getFileFromUuid($image))) {
×
150
            return null;
×
151
        }
152

153
        return $file->width;
×
154
    }
155

156
    public function getImageGallery($images, string $template = 'image_gallery.html.twig'): string
157
    {
158
        $galleryArray = \is_array($images) ? $images : StringUtil::deserialize($images, true);
×
159
        $galleryObjects = [];
×
160

161
        foreach ($galleryArray as $k => $v) {
×
162
            $galleryObjects['imageGallery'][$k] = $this->getImageData($v);
×
163
            $galleryObjects['imageGallery'][$k]['alt'] = $this->container->get('huh.utils.file')->getFileFromUuid($v)->name;
×
164
        }
165

166
        if (empty($galleryObjects)) {
×
167
            return [];
×
168
        }
169

170
        return $this->container->get('huh.utils.template')->renderTwigTemplate($template, $galleryObjects);
×
171
    }
172

173
    public function getImageSize($size)
174
    {
175
        $result = [];
×
176

177
        $size = \is_array($size) ? $size : StringUtil::deserialize($size, true);
×
178

179
        if (isset($size[2]) && $size[2] &&
×
180
            null !== ($imageSize = $this->container->get('huh.utils.model')->findModelInstanceByPk('tl_image_size', $size[2]))) {
×
181
            $result['width'] = $imageSize->width;
×
182
            $result['height'] = $imageSize->height;
×
183
        } else {
184
            $result['width'] = $size[0];
×
185
            $result['height'] = $size[1];
×
186
        }
187

188
        return $result;
×
189
    }
190
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc