0eedcb8f7677304afadd25177f368f3f64c60b74
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Form / RadioViewHelper.php
1 <?php
2 namespace 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 * View Helper which creates a simple radio button (<input type="radio">).
19 *
20 * = Examples =
21 *
22 * <code title="Example">
23 * <f:form.radio name="myRadioButton" value="someValue" />
24 * </code>
25 * <output>
26 * <input type="radio" name="myRadioButton" value="someValue" />
27 * </output>
28 *
29 * <code title="Preselect">
30 * <f:form.radio name="myRadioButton" value="someValue" checked="{object.value} == 5" />
31 * </code>
32 * <output>
33 * <input type="radio" name="myRadioButton" value="someValue" checked="checked" />
34 * (depending on $object)
35 * </output>
36 *
37 * <code title="Bind to object property">
38 * <f:form.radio property="newsletter" value="1" /> yes
39 * <f:form.radio property="newsletter" value="0" /> no
40 * </code>
41 * <output>
42 * <input type="radio" name="user[newsletter]" value="1" checked="checked" /> yes
43 * <input type="radio" name="user[newsletter]" value="0" /> no
44 * (depending on property "newsletter")
45 * </output>
46 *
47 * @api
48 */
49 class RadioViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper
50 {
51 /**
52 * @var string
53 */
54 protected $tagName = 'input';
55
56 /**
57 * Initialize the arguments.
58 *
59 * @api
60 */
61 public function initializeArguments()
62 {
63 parent::initializeArguments();
64 $this->registerArgument(
65 'errorClass',
66 'string',
67 'CSS class to set if there are errors for this view helper',
68 false,
69 'f3-form-error'
70 );
71 $this->registerArgument('checked', 'bool', 'Specifies that the input element should be preselected');
72 $this->overrideArgument('value', 'string', 'Value of input tag. Required for radio buttons', true);
73 $this->registerUniversalTagAttributes();
74 $this->registerTagAttribute(
75 'disabled',
76 'string',
77 'Specifies that the input element should be disabled when the page loads'
78 );
79 }
80
81 /**
82 * Renders the checkbox.
83 *
84 * @return string
85 * @api
86 */
87 public function render()
88 {
89 $checked = $this->arguments['checked'];
90
91 $this->tag->addAttribute('type', 'radio');
92
93 $nameAttribute = $this->getName();
94 $valueAttribute = $this->getValueAttribute();
95
96 $propertyValue = null;
97 if ($this->hasMappingErrorOccurred()) {
98 $propertyValue = $this->getLastSubmittedFormData();
99 }
100 if ($checked === null && $propertyValue === null) {
101 $propertyValue = $this->getPropertyValue();
102 $propertyValue = $this->convertToPlainValue($propertyValue);
103 }
104
105 if ($propertyValue !== null) {
106 // no type-safe comparison by intention
107 $checked = $propertyValue == $valueAttribute;
108 }
109
110 $this->registerFieldNameForFormTokenGeneration($nameAttribute);
111 $this->tag->addAttribute('name', $nameAttribute);
112 $this->tag->addAttribute('value', $valueAttribute);
113 if ($checked === true) {
114 $this->tag->addAttribute('checked', 'checked');
115 }
116
117 $this->setErrorClassAttribute();
118
119 return $this->tag->render();
120 }
121 }