1<?php
2
3/*
4 * This file is part of the TYPO3 CMS project.
5 *
6 * It is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License, either version 2
8 * of the License, or any later version.
9 *
10 * For the full copyright and license information, please read the
11 * LICENSE.txt file that was distributed with this source code.
12 *
13 * The TYPO3 project - inspiring people to share!
14 */
15
16namespace TYPO3\CMS\Backend\Form\Element;
17
18/**
19 * Render elements of type radio
20 */
21class RadioElement extends AbstractFormElement
22{
23    /**
24     * Default field information enabled for this element.
25     *
26     * @var array
27     */
28    protected $defaultFieldInformation = [
29        'tcaDescription' => [
30            'renderType' => 'tcaDescription',
31        ],
32    ];
33
34    /**
35     * Default field wizards enabled for this element.
36     *
37     * @var array
38     */
39    protected $defaultFieldWizard = [
40        'localizationStateSelector' => [
41            'renderType' => 'localizationStateSelector',
42        ],
43        'otherLanguageContent' => [
44            'renderType' => 'otherLanguageContent',
45            'after' => [
46                'localizationStateSelector'
47            ],
48        ],
49        'defaultLanguageDifferences' => [
50            'renderType' => 'defaultLanguageDifferences',
51            'after' => [
52                'otherLanguageContent',
53            ],
54        ],
55    ];
56
57    /**
58     * This will render a series of radio buttons.
59     *
60     * @return array As defined in initializeResultArray() of AbstractNode
61     */
62    public function render()
63    {
64        $resultArray = $this->initializeResultArray();
65
66        $disabled = '';
67        if ($this->data['parameterArray']['fieldConf']['config']['readOnly']) {
68            $disabled = ' disabled';
69        }
70
71        $fieldInformationResult = $this->renderFieldInformation();
72        $fieldInformationHtml = $fieldInformationResult['html'];
73        $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
74
75        $fieldWizardResult = $this->renderFieldWizard();
76        $fieldWizardHtml = $fieldWizardResult['html'];
77        $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
78
79        $html = [];
80        $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
81        $html[] = $fieldInformationHtml;
82        $html[] =   '<div class="form-wizards-wrap">';
83        $html[] =       '<div class="form-wizards-element">';
84        foreach ($this->data['parameterArray']['fieldConf']['config']['items'] as $itemNumber => $itemLabelAndValue) {
85            $label = $itemLabelAndValue[0];
86            $value = $itemLabelAndValue[1];
87            $radioId = htmlspecialchars($this->data['parameterArray']['itemFormElID'] . '_' . $itemNumber);
88            $radioChecked = (string)$value === (string)$this->data['parameterArray']['itemFormElValue'] ? ' checked="checked"' : '';
89
90            $html[] = '<div class="radio' . $disabled . '">';
91            $html[] =     '<label for="' . $radioId . '">';
92            $html[] =     '<input type="radio"';
93            $html[] =         ' name="' . htmlspecialchars($this->data['parameterArray']['itemFormElName']) . '"';
94            $html[] =         ' id="' . $radioId . '"';
95            $html[] =         ' value="' . htmlspecialchars($value) . '"';
96            $html[] =         $radioChecked;
97            $html[] =         $disabled;
98            $html[] =         ' onclick="' . htmlspecialchars(implode('', $this->data['parameterArray']['fieldChangeFunc'])) . '"';
99            $html[] =     '/>';
100            $html[] =         htmlspecialchars($this->appendValueToLabelInDebugMode($label, $value));
101            $html[] =     '</label>';
102            $html[] = '</div>';
103        }
104        $html[] =       '</div>';
105        if (!$disabled && !empty($fieldWizardHtml)) {
106            $html[] =   '<div class="form-wizards-items-bottom">';
107            $html[] =       $fieldWizardHtml;
108            $html[] =   '</div>';
109        }
110        $html[] =   '</div>';
111        $html[] = '</div>';
112
113        $resultArray['html'] = implode(LF, $html);
114        return $resultArray;
115    }
116}
117