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

FluidTYPO3 / vhs / 13566190336

27 Feb 2025 12:18PM UTC coverage: 72.127% (-0.6%) from 72.746%
13566190336

push

github

NamelessCoder
[TER] 7.1.0

5649 of 7832 relevant lines covered (72.13%)

20.01 hits per line

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

36.17
/Classes/ViewHelpers/Media/PictureViewHelper.php
1
<?php
2
namespace FluidTYPO3\Vhs\ViewHelpers\Media;
3

4
/*
5
 * This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.md file that was distributed with this source code.
9
 */
10

11
use FluidTYPO3\Vhs\Traits\TagViewHelperCompatibility;
12
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
13
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
14
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
15
use TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder;
16

17
/**
18
 * Renders a picture element with different images/sources for specific
19
 * media breakpoints
20
 *
21
 * ### Example
22
 *
23
 * ```
24
 * <v:media.picture src="fileadmin/some-image.png" alt="Some Image" loading="lazy">
25
 *     <v:media.source media="(min-width: 1200px)" width="500c" height="500c" />
26
 *     <v:media.source media="(min-width: 992px)" width="300c" height="300c" />
27
 *     <v:media.source media="(min-width: 768px)" width="200c" height="200c" />
28
 *     <v:media.source width="80c" height="80c" />
29
 * </v:media.picture>
30
 * ```
31
 *
32
 * ### Browser Support
33
 *
34
 * To have the widest Browser-Support you should consider using a polyfill like:
35
 * http://scottjehl.github.io/picturefill/
36
 */
37
class PictureViewHelper extends AbstractTagBasedViewHelper
38
{
39
    use TagViewHelperCompatibility;
40

41
    const SCOPE = 'FluidTYPO3\Vhs\ViewHelpers\Media\PictureViewHelper';
42
    const SCOPE_VARIABLE_SRC = 'src';
43
    const SCOPE_VARIABLE_ID = 'treatIdAsReference';
44
    const SCOPE_VARIABLE_DEFAULT_SOURCE = 'default-source';
45

46
    /**
47
     * name of the tag to be created by this view helper
48
     *
49
     * @var string
50
     * @api
51
     */
52
    protected $tagName = 'picture';
53

54
    public function initializeArguments(): void
55
    {
56
        parent::initializeArguments();
7✔
57
        $this->registerArgument('src', 'mixed', 'Path to the image or FileReference.', true);
7✔
58
        $this->registerArgument(
7✔
59
            'treatIdAsReference',
7✔
60
            'boolean',
7✔
61
            'When TRUE treat given src argument as sys_file_reference record.',
7✔
62
            false,
7✔
63
            false
7✔
64
        );
7✔
65
        $this->registerArgument('alt', 'string', 'Text for the alt attribute.', true);
7✔
66
        $this->registerArgument('title', 'string', 'Text for the title attribute.');
7✔
67
        $this->registerArgument('class', 'string', 'CSS class(es) to set.');
7✔
68
        $this->registerArgument(
7✔
69
            'loading',
7✔
70
            'string',
7✔
71
            'Native lazy-loading for images. Can be "lazy", "eager" or "auto"'
7✔
72
        );
7✔
73
    }
74

75
    /**
76
     * Render method
77
     * @return string
78
     * @throws Exception
79
     */
80
    public function render()
81
    {
82
        $src = $this->arguments['src'];
×
83
        $treatIdAsReference = (boolean) $this->arguments['treatIdAsReference'];
×
84
        if ($src instanceof FileReference) {
×
85
            $src = $src->getUid();
×
86
            $treatIdAsReference = true;
×
87
        }
88

89
        $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
×
90
        $viewHelperVariableContainer->addOrUpdate(static::SCOPE, static::SCOPE_VARIABLE_SRC, $src);
×
91
        $viewHelperVariableContainer->addOrUpdate(static::SCOPE, static::SCOPE_VARIABLE_ID, $treatIdAsReference);
×
92
        $content = $this->renderChildren();
×
93
        $viewHelperVariableContainer->remove(static::SCOPE, static::SCOPE_VARIABLE_SRC);
×
94
        $viewHelperVariableContainer->remove(static::SCOPE, static::SCOPE_VARIABLE_ID);
×
95

96
        if (!$viewHelperVariableContainer->exists(static::SCOPE, static::SCOPE_VARIABLE_DEFAULT_SOURCE)) {
×
97
            throw new Exception('Please add a source without a media query as a default.', 1438116616);
×
98
        }
99
        $defaultSource = $viewHelperVariableContainer->get(static::SCOPE, static::SCOPE_VARIABLE_DEFAULT_SOURCE);
×
100

101
        /** @var string $alt */
102
        $alt = $this->arguments['alt'];
×
103

104
        $defaultImage = new TagBuilder('img');
×
105
        $defaultImage->addAttribute('src', is_scalar($defaultSource) ? (string) $defaultSource : '');
×
106
        $defaultImage->addAttribute('alt', $alt);
×
107

108
        /** @var string|null $class */
109
        $class = $this->arguments['class'];
×
110
        if (!empty($class)) {
×
111
            $defaultImage->addAttribute('class', $class);
×
112
        }
113

114
        /** @var string|null $title */
115
        $title = $this->arguments['title'];
×
116
        if (!empty($title)) {
×
117
            $defaultImage->addAttribute('title', $title);
×
118
        }
119

120
        /** @var string|null $loading */
121
        $loading = $this->arguments['loading'];
×
122
        if (in_array($loading ?? '', ['lazy', 'eager', 'auto'], true)) {
×
123
            $defaultImage->addAttribute('loading', $loading);
×
124
        }
125

126
        $content .= $defaultImage->render();
×
127

128
        $this->tag->setContent($content);
×
129
        return $this->tag->render();
×
130
    }
131
}
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

© 2026 Coveralls, Inc