@@ -10,7 +10,32 @@ class Checkboxes extends Field
10
10
protected function _getElementHtml (AbstractElement $ element )
11
11
{
12
12
$ elementId = $ element ->getHtmlId ();
13
- $ html = <<<CSS
13
+
14
+ $ html = $ this ->getCss ($ elementId );
15
+
16
+ $ html .= sprintf ('<div id="%s"> ' , $ elementId );
17
+ $ name = $ element ->getName ();
18
+ $ options = $ element ->getValues ();
19
+ $ values = empty ($ element ->getValue ()) ? [] : explode (', ' , $ element ->getValue ()); // store as CSV in config
20
+
21
+ foreach ($ options as $ key => $ option ) {
22
+ $ value = $ option ['value ' ];
23
+ $ html .= $ this ->getCheckboxHtml (
24
+ $ elementId . '_ ' . $ key ,
25
+ $ name ,
26
+ $ value ,
27
+ in_array ($ value , $ values ),
28
+ $ option ['label ' ],
29
+ array_key_exists ('description ' , $ option ) ? $ option ['description ' ] : ''
30
+ );
31
+ }
32
+
33
+ $ html .= '</div> ' ;
34
+ return $ html ;
35
+ }
36
+
37
+ protected function getCss ($ elementId ): string {
38
+ return <<<CSS
14
39
<style>
15
40
# $ elementId .form-group {
16
41
display: grid;
@@ -37,28 +62,10 @@ protected function _getElementHtml(AbstractElement $element)
37
62
}
38
63
</style>
39
64
CSS ;
40
-
41
- $ html .= sprintf ('<div id="%s"> ' , $ elementId );
42
- $ name = $ element ->getName ();
43
- $ options = $ element ->getValues ();
44
- $ values = empty ($ element ->getValue ()) ? [] : explode (', ' , $ element ->getValue ()); // store as CSV in config
45
-
46
- foreach ($ options as $ option ) {
47
- $ value = $ option ['value ' ];
48
- $ html .= $ this ->getCheckboxHtml (
49
- $ name ,
50
- $ value ,
51
- in_array ($ value , $ values ),
52
- $ option ['label ' ],
53
- array_key_exists ('description ' , $ option ) ? $ option ['description ' ] : ''
54
- );
55
- }
56
-
57
- $ html .= '</div> ' ;
58
- return $ html ;
59
65
}
60
66
61
67
protected function getCheckboxHtml (
68
+ string $ id ,
62
69
string $ name ,
63
70
string $ value ,
64
71
bool $ checked ,
@@ -68,13 +75,14 @@ protected function getCheckboxHtml(
68
75
{
69
76
$ html = '<div class="form-group"> ' ;
70
77
$ html .= sprintf (
71
- '<input type="checkbox" name="%s[]" value="%s" %s /> ' ,
78
+ '<input type="checkbox" id="%s" name="%s[]" value="%s" %s /> ' ,
79
+ $ id ,
72
80
$ name ,
73
81
$ value ,
74
82
$ checked ? 'checked ' : ''
75
83
);
76
84
$ html .= '<div class="form-content"> ' ;
77
- $ html .= sprintf ('<label>%s</label> ' ,$ label ?? $ name );
85
+ $ html .= sprintf ('<label for="%s" >%s</label> ' ,$ id , $ label ?? $ name );
78
86
if ($ description ) {
79
87
$ html .= sprintf ('<span class="description">%s</span> ' , $ description );
80
88
}
0 commit comments