• 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/CheckboxViewHelper.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;
26

27
use Traversable;
28

29
/**
30
 * ViewHelper which creates a simple checkbox :html:`<input type="checkbox">`.
31
 *
32
 * Examples
33
 * ========
34
 *
35
 * Simple one
36
 * ----------
37
 *
38
 * ::
39
 *
40
 *    <f:form.checkbox name="myCheckBox" value="someValue" />
41
 *
42
 * Output::
43
 *
44
 *    {
45
 *      "name": "tx_extension_plugin[myCheckBox]",
46
 *      "type": "hidden",
47
 *      "value": ""
48
 *    },
49
 *    {
50
 *      "name": "tx_extension_plugin[myCheckBox]",
51
 *      "type": "checkbox",
52
 *      "value": "someValue"
53
 *    }
54
 *
55
 * Preselect
56
 * ---------
57
 *
58
 * ::
59
 *
60
 *    <f:form.checkbox name="myCheckBox" value="someValue" checked="{object.value} == 5" />
61
 *
62
 * Output::
63
 *
64
 *    {
65
 *      "name": "tx_extension_plugin[myCheckBox]",
66
 *      "type": "checkbox",
67
 *      "value": "someValue",
68
 *      "checked": "checked"
69
 *    },
70
 *
71
 * Depending on bound ``object`` to surrounding :ref:`f:form <typo3-fluid-form>`.
72
 *
73
 * Bind to object property
74
 * -----------------------
75
 *
76
 * ::
77
 *
78
 *    <f:form.checkbox property="interests" value="TYPO3" multiple="1" />
79
 *
80
 * Output::
81
 *
82
 *    {
83
 *      "name": "tx_extension_plugin[customer][interests]",
84
 *      "type": "hidden",
85
 *      "value": ""
86
 *    },
87
 *    {
88
 *      "name": "tx_extension_plugin[customer][interests][]",
89
 *      "type": "checkbox",
90
 *      "value": "TYPO3"
91
 *    },
92
 *
93
 * Depending on property ``interests``.
94
 */
95
final class CheckboxViewHelper extends AbstractFormFieldViewHelper
96
{
97
    public function initializeArguments(): void
98
    {
NEW
99
        parent::initializeArguments();
×
NEW
100
        $this->registerArgument(
×
NEW
101
            'errorClass',
×
NEW
102
            'string',
×
NEW
103
            'CSS class to set if there are errors for this ViewHelper',
×
NEW
104
            false,
×
NEW
105
            'f3-form-error'
×
NEW
106
        );
×
NEW
107
        $this->registerArgument('value', 'string', 'Value of input tag. Required for checkboxes', true);
×
NEW
108
        $this->registerArgument('checked', 'bool', 'Specifies that the input element should be preselected');
×
NEW
109
        $this->registerArgument('multiple', 'bool', 'Specifies whether this checkbox belongs to a multivalue (is part of a checkbox group)', false, false);
×
110
    }
111

112
    public function render(): string
113
    {
NEW
114
        $this->data = json_decode(parent::render(), true);
×
115

NEW
116
        $checked = $this->arguments['checked'];
×
NEW
117
        $multiple = $this->arguments['multiple'];
×
118

NEW
119
        $nameAttribute = $this->getName();
×
120

NEW
121
        $valueAttribute = $this->getValueAttribute();
×
NEW
122
        $propertyValue = null;
×
NEW
123
        if ($this->hasMappingErrorOccurred()) {
×
NEW
124
            $propertyValue = $this->getLastSubmittedFormData();
×
125
        }
NEW
126
        if ($checked === null && $propertyValue === null) {
×
NEW
127
            $propertyValue = $this->getPropertyValue();
×
128
        }
129

NEW
130
        if ($propertyValue instanceof Traversable) {
×
NEW
131
            $propertyValue = iterator_to_array($propertyValue);
×
132
        }
NEW
133
        if (is_array($propertyValue)) {
×
NEW
134
            $propertyValue = array_map($this->convertToPlainValue(...), $propertyValue);
×
NEW
135
            if ($checked === null) {
×
NEW
136
                $checked = in_array($valueAttribute, $propertyValue);
×
137
            }
NEW
138
            $nameAttribute .= '[]';
×
NEW
139
        } elseif ($multiple === true) {
×
NEW
140
            $nameAttribute .= '[]';
×
NEW
141
        } elseif ($propertyValue !== null) {
×
NEW
142
            $checked = (bool)$propertyValue === (bool)$valueAttribute;
×
143
        }
144

NEW
145
        $this->registerFieldNameForFormTokenGeneration($nameAttribute);
×
NEW
146
        $this->data['name'] = $nameAttribute;
×
NEW
147
        $this->data['type'] = 'checkbox';
×
NEW
148
        $this->data['value'] = $valueAttribute;
×
NEW
149
        if ($checked === true) {
×
NEW
150
            $this->data['checked'] = 'checked';
×
151
        }
152

NEW
153
        $this->setErrorClassAttribute();
×
NEW
154
        $hiddenField = $this->renderHiddenFieldForEmptyValue();
×
NEW
155
        return $hiddenField . json_encode($this->data);
×
156
    }
157
}
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