1<?php
2namespace TYPO3\CMS\Fluid\ViewHelpers\Form;
3
4/*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17/**
18 * ViewHelper which creates a simple radio button :html:`<input type="radio">`.
19 *
20 * Examples
21 * ========
22 *
23 * Simple
24 * ------
25 *
26 * ::
27 *
28 *    <f:form.radio name="myRadioButton" value="someValue" />
29 *
30 * Output::
31 *
32 *    <input type="radio" name="myRadioButton" value="someValue" />
33 *
34 * Preselect
35 * ---------
36 *
37 * ::
38 *
39 *    <f:form.radio name="myRadioButton" value="someValue" checked="{object.value} == 5" />
40 *
41 * Output::
42 *
43 *    <input type="radio" name="myRadioButton" value="someValue" checked="checked" />
44 *
45 * Depending on bound ``object`` to surrounding :ref:`f:form <typo3-fluid-form>`.
46 *
47 * Bind to object property
48 * -----------------------
49 *
50 * ::
51 *
52 *    <f:form.radio property="newsletter" value="1" /> yes
53 *    <f:form.radio property="newsletter" value="0" /> no
54 *
55 * Output::
56 *
57 *    <input type="radio" name="user[newsletter]" value="1" checked="checked" /> yes
58 *    <input type="radio" name="user[newsletter]" value="0" /> no
59 *
60 * Depending on property ``newsletter``.
61 */
62class RadioViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper
63{
64    /**
65     * @var string
66     */
67    protected $tagName = 'input';
68
69    /**
70     * Initialize the arguments.
71     */
72    public function initializeArguments()
73    {
74        parent::initializeArguments();
75        $this->registerArgument(
76            'errorClass',
77            'string',
78            'CSS class to set if there are errors for this ViewHelper',
79            false,
80            'f3-form-error'
81        );
82        $this->registerArgument('checked', 'bool', 'Specifies that the input element should be preselected');
83        $this->overrideArgument('value', 'string', 'Value of input tag. Required for radio buttons', true);
84        $this->registerUniversalTagAttributes();
85        $this->registerTagAttribute(
86            'disabled',
87            'string',
88            'Specifies that the input element should be disabled when the page loads'
89        );
90    }
91
92    /**
93     * Renders the checkbox.
94     *
95     * @return string
96     */
97    public function render()
98    {
99        $checked = $this->arguments['checked'];
100
101        $this->tag->addAttribute('type', 'radio');
102
103        $nameAttribute = $this->getName();
104        $valueAttribute = $this->getValueAttribute();
105
106        $propertyValue = null;
107        if ($this->hasMappingErrorOccurred()) {
108            $propertyValue = $this->getLastSubmittedFormData();
109        }
110        if ($checked === null && $propertyValue === null) {
111            $propertyValue = $this->getPropertyValue();
112            $propertyValue = $this->convertToPlainValue($propertyValue);
113        }
114
115        if ($propertyValue !== null) {
116            // no type-safe comparison by intention
117            $checked = $propertyValue == $valueAttribute;
118        }
119
120        $this->registerFieldNameForFormTokenGeneration($nameAttribute);
121        $this->tag->addAttribute('name', $nameAttribute);
122        $this->tag->addAttribute('value', $valueAttribute);
123        if ($checked === true) {
124            $this->tag->addAttribute('checked', 'checked');
125        }
126
127        $this->setErrorClassAttribute();
128
129        return $this->tag->render();
130    }
131}
132