66use Drupal \Core \Form \ConfigFormBase ;
77use Drupal \Core \Form \FormStateInterface ;
88use Drupal \patternkit \PatternkitLibraryDiscoveryInterface ;
9- use Exception ;
109use Symfony \Component \DependencyInjection \ContainerInterface ;
1110
1211class PatternkitSettingsForm extends ConfigFormBase {
@@ -38,51 +37,77 @@ public function buildForm(array $form, FormStateInterface $form_state) :array {
3837 try {
3938 $ libraries = $ this ->libraryDiscovery ->getLibraries ();
4039 }
41- catch (Exception $ exception ) {
40+ catch (\ Exception $ exception ) {
4241 $ this ->getLogger ('patternkit ' )->error ('Unable to load Patternkit libraries list: @message ' , ['@message ' => $ exception ->getMessage ()]);
4342 \Drupal::messenger ()->addMessage (t ('Unable to load Patternkit libraries list. Check the logs for more information. ' ), 'error ' );
4443 return [
4544 '#markup ' => $ this ->t ('Settings are unavailable when Pattern libraries fail to load to prevent config errors. ' ),
4645 ];
4746 }
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 ' ) ?? [];
5058 foreach ($ libraries as $ lib_title => $ library ) {
5159 $ lib_desc = $ library ['description ' ] ?? $ lib_title ;
5260 if (!empty ($ library ['patterns ' ])) {
53- $ library_values [] = $ lib_title ;
54- $ lib_desc = t ('@title (@count patterns) ' , array (
61+ $ lib_desc = t ('@title (@count patterns) ' , [
5562 '@title ' => $ lib_title ,
5663 '@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 ' ];
5875 }
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+ ];
6092 }
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 ' ] = [
7095 '#type ' => 'checkbox ' ,
7196 '#title ' => t ('Use the Patternkit Library Cache ' ),
7297 '#default_value ' => $ config ->get ('patternkit_cache_enabled ' ),
73- ) ;
98+ ] ;
7499
75- $ form ['patternkit_render_cache ' ] = array (
100+ $ form ['patternkit_render_cache ' ] = [
76101 '#type ' => 'checkbox ' ,
77102 '#title ' => t ('Use the Patternkit Disk Render Cache ' ),
78103 '#default_value ' => $ config ->get ('patternkit_render_cache ' ),
79- ) ;
104+ ] ;
80105
81- $ form ['patternkit_default_module_ttl ' ] = array (
106+ $ form ['patternkit_default_module_ttl ' ] = [
82107 '#type ' => 'textfield ' ,
83108 '#title ' => t ('Patternkit Default Pattern TTL (in ms) ' ),
84109 '#default_value ' => $ config ->get ('patternkit_default_module_ttl ' ),
85- ) ;
110+ ] ;
86111
87112 return parent ::buildForm ($ form , $ form_state );
88113 }
@@ -106,7 +131,13 @@ public function getFormId() :string {
106131 */
107132 public function submitForm (array &$ form , FormStateInterface $ form_state ): void {
108133 $ 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 ' )
110141 && !$ config ->get ('patternkit_cache_enabled ' )) {
111142 $ this ->libraryDiscovery ->clearCachedDefinitions ();
112143 }
0 commit comments