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