6
6
use Drupal \Core \Form \ConfigFormBase ;
7
7
use Drupal \Core \Form \FormStateInterface ;
8
8
use Drupal \patternkit \PatternkitLibraryDiscoveryInterface ;
9
- use Exception ;
10
9
use Symfony \Component \DependencyInjection \ContainerInterface ;
11
10
12
11
class PatternkitSettingsForm extends ConfigFormBase {
@@ -38,51 +37,77 @@ public function buildForm(array $form, FormStateInterface $form_state) :array {
38
37
try {
39
38
$ libraries = $ this ->libraryDiscovery ->getLibraries ();
40
39
}
41
- catch (Exception $ exception ) {
40
+ catch (\ Exception $ exception ) {
42
41
$ this ->getLogger ('patternkit ' )->error ('Unable to load Patternkit libraries list: @message ' , ['@message ' => $ exception ->getMessage ()]);
43
42
\Drupal::messenger ()->addMessage (t ('Unable to load Patternkit libraries list. Check the logs for more information. ' ), 'error ' );
44
43
return [
45
44
'#markup ' => $ this ->t ('Settings are unavailable when Pattern libraries fail to load to prevent config errors. ' ),
46
45
];
47
46
}
48
- $ library_options = array ();
49
- $ library_values = array ();
47
+
48
+ $ form ['patternkit_libraries ' ] = [
49
+ '#type ' => 'table ' ,
50
+ '#header ' => [$ this ->t ('Patternkit Library ' ),
51
+ ['data ' => $ this ->t ('Enabled ' ), 'class ' => ['checkbox ' ]],
52
+ ['data ' => $ this ->t ('Visible in Lists ' ), 'class ' => ['checkbox ' ]],
53
+ ],
54
+ '#attributes ' => ['class ' => ['libraries ' , 'js-libraries ' ]],
55
+ '#sticky ' => TRUE ,
56
+ ];
57
+ $ library_options = $ config ->get ('patternkit_libraries ' ) ?? [];
50
58
foreach ($ libraries as $ lib_title => $ library ) {
51
59
$ lib_desc = $ library ['description ' ] ?? $ lib_title ;
52
60
if (!empty ($ library ['patterns ' ])) {
53
- $ library_values [] = $ lib_title ;
54
- $ lib_desc = t ('@title (@count patterns) ' , array (
61
+ $ lib_desc = t ('@title (@count patterns) ' , [
55
62
'@title ' => $ lib_title ,
56
63
'@count ' => count ($ library ['patterns ' ]),
57
- ));
64
+ ]);
65
+ }
66
+ $ form ['patternkit_libraries ' ][$ lib_title ]['description ' ] = [
67
+ '#type ' => 'inline_template ' ,
68
+ '#template ' => '<div class="library"><span class="title">{{ title }}</span>{% if description or warning %}<div class="description">{{ description }}</div>{% endif %}</div> ' ,
69
+ '#context ' => [
70
+ 'title ' => $ lib_desc ,
71
+ ],
72
+ ];
73
+ if (!empty ($ library ['description ' ])) {
74
+ $ form ['patternkit_libraries ' ][$ lib_title ]['description ' ]['#context ' ]['description ' ] = $ library ['description ' ];
58
75
}
59
- $ library_options [$ lib_title ] = $ lib_desc ;
76
+ $ form ['patternkit_libraries ' ][$ lib_title ]['enabled ' ] = [
77
+ '#title ' => $ this ->t ('Library Enabled ' ),
78
+ '#title_display ' => 'invisible ' ,
79
+ '#wrapper_attributes ' => ['class ' => ['checkbox ' ]],
80
+ '#type ' => 'checkbox ' ,
81
+ '#default_value ' => $ library_options [$ lib_title ]['enabled ' ] ?? 1 ,
82
+ '#attributes ' => ['class ' => ['lib- ' . $ lib_title , 'js-lib- ' . $ lib_title ]],
83
+ ];
84
+ $ form ['patternkit_libraries ' ][$ lib_title ]['visible ' ] = [
85
+ '#title ' => $ this ->t ('Library Visible in Lists ' ),
86
+ '#title_display ' => 'invisible ' ,
87
+ '#wrapper_attributes ' => ['class ' => ['checkbox ' ]],
88
+ '#type ' => 'checkbox ' ,
89
+ '#default_value ' => $ library_options [$ lib_title ]['visible ' ] ?? 1 ,
90
+ '#attributes ' => ['class ' => ['lib- ' . $ lib_title , 'js-lib- ' . $ lib_title ]],
91
+ ];
60
92
}
61
- $ form ['patternkit_libraries ' ] = array (
62
- '#type ' => 'checkboxes ' ,
63
- '#title ' => t ('Enabled Patternkit Libraries ' ),
64
- '#options ' => $ library_options ,
65
- '#default_value ' => $ library_values ,
66
- '#disabled ' => TRUE ,
67
- );
68
-
69
- $ form ['patternkit_cache_enabled ' ] = array (
93
+
94
+ $ form ['patternkit_cache_enabled ' ] = [
70
95
'#type ' => 'checkbox ' ,
71
96
'#title ' => t ('Use the Patternkit Library Cache ' ),
72
97
'#default_value ' => $ config ->get ('patternkit_cache_enabled ' ),
73
- ) ;
98
+ ] ;
74
99
75
- $ form ['patternkit_render_cache ' ] = array (
100
+ $ form ['patternkit_render_cache ' ] = [
76
101
'#type ' => 'checkbox ' ,
77
102
'#title ' => t ('Use the Patternkit Disk Render Cache ' ),
78
103
'#default_value ' => $ config ->get ('patternkit_render_cache ' ),
79
- ) ;
104
+ ] ;
80
105
81
- $ form ['patternkit_default_module_ttl ' ] = array (
106
+ $ form ['patternkit_default_module_ttl ' ] = [
82
107
'#type ' => 'textfield ' ,
83
108
'#title ' => t ('Patternkit Default Pattern TTL (in ms) ' ),
84
109
'#default_value ' => $ config ->get ('patternkit_default_module_ttl ' ),
85
- ) ;
110
+ ] ;
86
111
87
112
return parent ::buildForm ($ form , $ form_state );
88
113
}
@@ -106,7 +131,13 @@ public function getFormId() :string {
106
131
*/
107
132
public function submitForm (array &$ form , FormStateInterface $ form_state ): void {
108
133
$ config = $ this ->config (static ::SETTINGS );
109
- if ($ form_state ['values ' ]['patternkit_cache_enabled ' ]
134
+ $ config
135
+ ->set ('patternkit_libraries ' , $ form_state ->getValue ('patternkit_libraries ' ))
136
+ ->set ('patternkit_cache_enabled ' , $ form_state ->getValue ('patternkit_cache_enabled ' ))
137
+ ->set ('patternkit_render_cache ' , $ form_state ->getValue ('patternkit_render_cache ' ))
138
+ ->set ('patternkit_default_module_ttl ' , $ form_state ->getValue ('patternkit_default_module_ttl ' ))
139
+ ->save ();
140
+ if ($ form_state ->getValue ('patternkit_cache_enabled ' )
110
141
&& !$ config ->get ('patternkit_cache_enabled ' )) {
111
142
$ this ->libraryDiscovery ->clearCachedDefinitions ();
112
143
}
0 commit comments