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

TYPO3-Headless / headless / 14022693367

23 Mar 2025 08:26PM UTC coverage: 52.345% (-20.8%) from 73.13%
14022693367

Pull #815

github

web-flow
Merge e0fcdaa4a into a15e1c8c4
Pull Request #815: [FEATURE] Add support for f:form.* viewhelper

0 of 600 new or added lines in 15 files covered. (0.0%)

1105 of 2111 relevant lines covered (52.34%)

5.94 hits per line

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

0.0
/Classes/XClass/ViewHelpers/Form/Select/OptionViewHelper.php
1
<?php
2

3
/*
4
 * This file is part of the "headless" Extension for TYPO3 CMS.
5
 *
6
 * For the full copyright and license information, please read the
7
 * LICENSE.md file that was distributed with this source code.
8
 */
9

10
declare(strict_types=1);
11

12
/*
13
 * This file is part of the TYPO3 CMS project.
14
 *
15
 * It is free software; you can redistribute it and/or modify it under
16
 * the terms of the GNU General Public License, either version 2
17
 * of the License, or any later version.
18
 *
19
 * For the full copyright and license information, please read the
20
 * LICENSE.txt file that was distributed with this source code.
21
 *
22
 * The TYPO3 project - inspiring people to share!
23
 */
24

25
namespace FriendsOfTYPO3\Headless\XClass\ViewHelpers\Form\Select;
26

27
use FriendsOfTYPO3\Headless\XClass\ViewHelpers\Form\AbstractFormFieldViewHelper;
28
use FriendsOfTYPO3\Headless\XClass\ViewHelpers\Form\SelectViewHelper;
29
use Iterator;
30

31
/**
32
 * Adds custom :html:`<option>` tags inside an :ref:`<f:form.select> <typo3-fluid-form-select>`.
33
 */
34
final class OptionViewHelper extends AbstractFormFieldViewHelper
35
{
36
    /**
37
     * @var string
38
     */
39
    protected $tagName = 'option';
40

41
    public function initializeArguments(): void
42
    {
NEW
43
        $this->registerArgument('selected', 'boolean', 'If set, overrides automatic detection of selected state for this option.');
×
NEW
44
        $this->registerArgument('additionalAttributes', 'array', 'Additional tag attributes. They will be added directly to the resulting HTML tag.');
×
NEW
45
        $this->registerArgument('data', 'array', 'Additional data-* attributes. They will each be added with a "data-" prefix.');
×
NEW
46
        $this->registerArgument('value', 'mixed', 'Value to be inserted in HTML tag - must be convertible to string!');
×
47
    }
48

49
    public function render(): string
50
    {
NEW
51
        $this->data = json_decode(parent::render(), true);
×
52

NEW
53
        $childContent = $this->renderChildren();
×
NEW
54
        $this->data['content'] = (string)$childContent;
×
NEW
55
        $value = $this->arguments['value'] ?? $childContent;
×
NEW
56
        if ($this->arguments['selected'] ?? $this->isValueSelected((string)$value)) {
×
NEW
57
            $this->data['selected'] = 'selected';
×
58
        }
NEW
59
        $this->data['value'] = $value;
×
NEW
60
        $this->data['type'] = 'selectOption';
×
NEW
61
        $parentRequestedFormTokenFieldName = $this->renderingContext->getViewHelperVariableContainer()->get(
×
NEW
62
            SelectViewHelper::class,
×
NEW
63
            'registerFieldNameForFormTokenGeneration'
×
NEW
64
        );
×
NEW
65
        if ($parentRequestedFormTokenFieldName) {
×
66
            // parent (select field) has requested this option must add one more
67
            // entry in the token generation registry for one additional potential
68
            // value of the field. Happens when "multiple" is true on parent.
NEW
69
            $this->registerFieldNameForFormTokenGeneration($parentRequestedFormTokenFieldName);
×
70
        }
NEW
71
        return json_encode($this->data);
×
72
    }
73

74
    protected function isValueSelected(string $value): bool
75
    {
NEW
76
        $selectedValue = $this->renderingContext->getViewHelperVariableContainer()->get(SelectViewHelper::class, 'selectedValue');
×
NEW
77
        if (is_array($selectedValue)) {
×
NEW
78
            return in_array($value, array_map(strval(...), $selectedValue), true);
×
79
        }
NEW
80
        if ($selectedValue instanceof Iterator) {
×
NEW
81
            return in_array($value, array_map(strval(...), iterator_to_array($selectedValue)), true);
×
82
        }
NEW
83
        return $value === (string)$selectedValue;
×
84
    }
85
}
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