[TASK] Streamline phpdoc annotations in EXT:fluid
[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 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 public function initializeArguments()
58 {
59 parent::initializeArguments();
60 $this->registerArgument(
61 'errorClass',
62 'string',
63 'CSS class to set if there are errors for this view helper',
64 false,
65 'f3-form-error'
66 );
67 $this->registerArgument('checked', 'bool', 'Specifies that the input element should be preselected');
68 $this->overrideArgument('value', 'string', 'Value of input tag. Required for radio buttons', true);
69 $this->registerUniversalTagAttributes();
70 $this->registerTagAttribute(
71 'disabled',
72 'string',
73 'Specifies that the input element should be disabled when the page loads'
74 );
75 }
76
77 /**
78 * Renders the checkbox.
79 *
80 * @return string
81 */
82 public function render()
83 {
84 $checked = $this->arguments['checked'];
85
86 $this->tag->addAttribute('type', 'radio');
87
88 $nameAttribute = $this->getName();
89 $valueAttribute = $this->getValueAttribute();
90
91 $propertyValue = null;
92 if ($this->hasMappingErrorOccurred()) {
93 $propertyValue = $this->getLastSubmittedFormData();
94 }
95 if ($checked === null && $propertyValue === null) {
96 $propertyValue = $this->getPropertyValue();
97 $propertyValue = $this->convertToPlainValue($propertyValue);
98 }
99
100 if ($propertyValue !== null) {
101 // no type-safe comparison by intention
102 $checked = $propertyValue == $valueAttribute;
103 }
104
105 $this->registerFieldNameForFormTokenGeneration($nameAttribute);
106 $this->tag->addAttribute('name', $nameAttribute);
107 $this->tag->addAttribute('value', $valueAttribute);
108 if ($checked === true) {
109 $this->tag->addAttribute('checked', 'checked');
110 }
111
112 $this->setErrorClassAttribute();
113
114 return $this->tag->render();
115 }
116 }