[FOLLOWUP][BUGFIX] Fix initialization for checked attributes
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Form / RadioViewHelper.php
1 <?php
2
3 namespace TYPO3\CMS\Fluid\ViewHelpers\Form;
4
5 /* *
6 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
7 * *
8 * It is free software; you can redistribute it and/or modify it under *
9 * the terms of the GNU Lesser General Public License, either version 3 *
10 * of the License, or (at your option) any later version. *
11 * *
12 * The TYPO3 project - inspiring people to share! *
13 * */
14
15 /**
16 * View Helper which creates a simple radio button (<input type="radio">).
17 *
18 * = Examples =
19 *
20 * <code title="Example">
21 * <f:form.radio name="myRadioButton" value="someValue" />
22 * </code>
23 * <output>
24 * <input type="radio" name="myRadioButton" value="someValue" />
25 * </output>
26 *
27 * <code title="Preselect">
28 * <f:form.radio name="myRadioButton" value="someValue" checked="{object.value} == 5" />
29 * </code>
30 * <output>
31 * <input type="radio" name="myRadioButton" value="someValue" checked="checked" />
32 * (depending on $object)
33 * </output>
34 *
35 * <code title="Bind to object property">
36 * <f:form.radio property="newsletter" value="1" /> yes
37 * <f:form.radio property="newsletter" value="0" /> no
38 * </code>
39 * <output>
40 * <input type="radio" name="user[newsletter]" value="1" checked="checked" /> yes
41 * <input type="radio" name="user[newsletter]" value="0" /> no
42 * (depending on property "newsletter")
43 * </output>
44 *
45 * @api
46 */
47 class RadioViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper
48 {
49 /**
50 * @var string
51 */
52 protected $tagName = 'input';
53
54 /**
55 * Initialize the arguments.
56 *
57 * @return void
58 * @api
59 */
60 public function initializeArguments()
61 {
62 parent::initializeArguments();
63 $this->registerTagAttribute(
64 'disabled', 'string', 'Specifies that the input element should be disabled when the page loads'
65 );
66 $this->registerArgument(
67 'errorClass', 'string', 'CSS class to set if there are errors for this view helper', false, 'f3-form-error'
68 );
69 $this->overrideArgument('value', 'string', 'Value of input tag. Required for radio buttons', true);
70 $this->registerUniversalTagAttributes();
71 }
72
73 /**
74 * Renders the checkbox.
75 *
76 * @param bool $checked Specifies that the input element should be preselected
77 * @return string
78 * @api
79 */
80 public function render($checked = null)
81 {
82 $this->tag->addAttribute('type', 'radio');
83
84 $nameAttribute = $this->getName();
85 $valueAttribute = $this->getValueAttribute();
86
87 $propertyValue = null;
88 if ($this->hasMappingErrorOccurred()) {
89 $propertyValue = $this->getLastSubmittedFormData();
90 }
91 if ($checked === null && $propertyValue === null) {
92 $propertyValue = $this->getPropertyValue();
93 $propertyValue = $this->convertToPlainValue($propertyValue);
94 }
95
96 if ($propertyValue !== null) {
97
98 // no type-safe comparison by intention
99 $checked = $propertyValue == $valueAttribute;
100 }
101
102 $this->registerFieldNameForFormTokenGeneration($nameAttribute);
103 $this->tag->addAttribute('name', $nameAttribute);
104 $this->tag->addAttribute('value', $valueAttribute);
105 if ($checked === true) {
106 $this->tag->addAttribute('checked', 'checked');
107 }
108
109 $this->setErrorClassAttribute();
110
111 return $this->tag->render();
112 }
113 }