`.
*
* ```
*
*
* ```
*
* @see https://docs.typo3.org/permalink/t3viewhelper:typo3-fluid-form-checkbox
*/
final class CheckboxViewHelper extends AbstractFormFieldViewHelper
{
/**
* @var string
*/
protected $tagName = 'input';
public function initializeArguments(): void
{
parent::initializeArguments();
$this->registerArgument(
'errorClass',
'string',
'CSS class to set if there are errors for this ViewHelper',
false,
'f3-form-error'
);
$this->registerArgument('value', 'string', 'Value of input tag. Required for checkboxes', true);
$this->registerArgument('checked', 'bool', 'Specifies that the input element should be preselected');
$this->registerArgument('multiple', 'bool', 'Specifies whether this checkbox belongs to a multivalue (is part of a checkbox group)', false, false);
}
public function render(): string
{
$checked = $this->arguments['checked'];
$multiple = $this->arguments['multiple'];
$this->tag->addAttribute('type', 'checkbox');
$nameAttribute = $this->getName();
$valueAttribute = $this->getValueAttribute();
$propertyValue = null;
if ($this->hasMappingErrorOccurred()) {
$propertyValue = $this->getLastSubmittedFormData();
}
if ($checked === null && $propertyValue === null) {
$propertyValue = $this->getPropertyValue();
}
if ($propertyValue instanceof \Traversable) {
$propertyValue = iterator_to_array($propertyValue);
}
if (is_array($propertyValue)) {
$propertyValue = array_map($this->convertToPlainValue(...), $propertyValue);
if ($checked === null) {
$checked = in_array($valueAttribute, $propertyValue);
}
$nameAttribute .= '[]';
} elseif ($multiple === true) {
$nameAttribute .= '[]';
} elseif ($propertyValue !== null) {
$checked = (bool)$propertyValue === (bool)$valueAttribute;
}
$this->registerFieldNameForFormTokenGeneration($nameAttribute);
$this->tag->addAttribute('name', $nameAttribute);
$this->tag->addAttribute('value', (string)$valueAttribute);
if ($checked === true) {
$this->tag->addAttribute('checked', 'checked');
}
$this->setErrorClassAttribute();
$hiddenField = $this->renderHiddenFieldForEmptyValue();
return $hiddenField . $this->tag->render();
}
}