[BUGFIX] Enable CheckboxViewHelper binding to ArrayObject and Null
[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 boolean $checked Specifies that the input element should be preselected
68 * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
69 * @return string
70 * @api
71 */
72 public function render($checked = NULL) {
73 $this->tag->addAttribute('type', 'checkbox');
74
75 $nameAttribute = $this->getName();
76 $valueAttribute = $this->getValue();
77 if ($this->isObjectAccessorMode()) {
78 if ($this->hasMappingErrorOccured()) {
79 $propertyValue = $this->getLastSubmittedFormData();
80 } else {
81 $propertyValue = $this->getPropertyValue();
82 }
83
84 if ($propertyValue instanceof \Traversable) {
85 $propertyValue = iterator_to_array($propertyValue);
86 }
87 if (is_array($propertyValue)) {
88 if ($checked === NULL) {
89 $checked = in_array($valueAttribute, $propertyValue);
90 }
91 $nameAttribute .= '[]';
92 } elseif (($multiple = FALSE) === TRUE) {
93 // @todo: implement correct as in Flow.Fluid
94 $nameAttribute .= '[]';
95 } elseif ($checked === NULL && $propertyValue !== NULL) {
96 $checked = (boolean) $propertyValue === (boolean) $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
111 ?>