Skip to content

Commit 37d566e

Browse files
committed
Updates settings forms with config vars
fix(PatternkitSettingsForm)
1 parent d2f3661 commit 37d566e

7 files changed

+89
-32
lines changed

config/install/patternkit.settings.yml

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ patternkit_json_editor_css: ""
44
patternkit_json_editor_icons: fontawesome5
55
patternkit_json_editor_js: ""
66
patternkit_json_editor_theme: bootstrap4
7+
patternkit_libraries: []
78
patternkit_render_cache: true

patternkit.links.task.yml

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ patternkit.list_sub:
1414
title: Blocks
1515
route_name: entity.patternkit_block.collection
1616
parent_id: entity.patternkit_block.collection
17+
patternkit.settings:
18+
title: 'Patternkit Settings'
19+
route_name: patternkit.settings
20+
base_route: patternkit.settings
1721
patternkit.settings.json_settings:
1822
title: 'JSON Pattern Library Settings'
1923
route_name: patternkit.settings.json_settings

src/Form/PatternLibraryJSONForm.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,19 @@ class PatternLibraryJSONForm extends ConfigFormBase {
8686
public function buildForm(array $form, FormStateInterface $form_state) :array {
8787
$config = $this->config(static::SETTINGS);
8888

89+
$themes = array_keys(static::THEMES);
8990
$form['patternkit_json_editor_theme'] = array(
9091
'#type' => 'select',
9192
'#title' => t('Select the JSON Editor Theme'),
92-
'#options' => array_keys(static::THEMES),
93+
'#options' => array_combine($themes, $themes),
9394
'#default_value' => $config->get('patternkit_json_editor_theme'),
9495
);
9596

97+
$icons = array_keys(static::ICONS);
9698
$form['patternkit_json_editor_icons'] = array(
9799
'#type' => 'select',
98100
'#title' => t('Select the icons to be used with the editor'),
99-
'#options' => array_keys(static::ICONS),
101+
'#options' => array_combine($icons, $icons),
100102
'#default_value' => $config->get('patternkit_json_editor_icons'),
101103
);
102104

src/Form/PatternkitSettingsForm.php

+54-23
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Drupal\Core\Form\ConfigFormBase;
77
use Drupal\Core\Form\FormStateInterface;
88
use Drupal\patternkit\PatternkitLibraryDiscoveryInterface;
9-
use Exception;
109
use Symfony\Component\DependencyInjection\ContainerInterface;
1110

1211
class 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
}

src/PatternLibraryCollector.php

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Drupal\Core\Serialization\Yaml;
1717
use Drupal\Core\Theme\ThemeManagerInterface;
1818
use Drupal\patternkit\Form\PatternkitSettingsForm;
19-
use function str_replace;
2019
use Symfony\Component\DependencyInjection\ContainerInterface;
2120
use Drupal\Core\Cache\CacheCollector;
2221
use Drupal\Core\Lock\LockBackendInterface;

src/Plugin/Derivative/PatternkitBlock.php

+26-5
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@
33
namespace Drupal\patternkit\Plugin\Derivative;
44

55
use Drupal\Component\Plugin\Derivative\DeriverBase;
6+
use Drupal\Core\Config\ImmutableConfig;
67
use Drupal\Core\Entity\EntityStorageInterface;
78
use Drupal\Core\Logger\LoggerChannelInterface;
89
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
10+
use Drupal\patternkit\Form\PatternkitSettingsForm;
911
use Drupal\patternkit\PatternkitLibraryDiscoveryInterface;
10-
use Exception;
1112
use Symfony\Component\DependencyInjection\ContainerInterface;
12-
use function strstr;
1313

1414
class PatternkitBlock extends DeriverBase implements ContainerDeriverInterface {
1515

16+
/** @var \Drupal\Core\Config\ImmutableConfig */
17+
protected $config;
18+
1619
/**
1720
* Logs to the patternkit channel.
1821
*
@@ -29,14 +32,21 @@ class PatternkitBlock extends DeriverBase implements ContainerDeriverInterface {
2932
/**
3033
* PatternkitBlock constructor.
3134
*
35+
* @param \Drupal\Core\Config\ImmutableConfig $config
36+
* Provides patternkit configurable settings.
3237
* @param \Drupal\Core\Logger\LoggerChannelInterface $logger
3338
* Generates and provides log channels.
3439
* @param \Drupal\patternkit\PatternkitLibraryDiscoveryInterface $pattern_discovery
3540
* Provides a list of pattern libraries with metadata.
3641
* @param \Drupal\Core\Entity\EntityStorageInterface $storage
3742
* Loads and saves entities to storage.
3843
*/
39-
public function __construct(LoggerChannelInterface $logger, PatternkitLibraryDiscoveryInterface $pattern_discovery, EntityStorageInterface $storage) {
44+
public function __construct(
45+
ImmutableConfig $config,
46+
LoggerChannelInterface $logger,
47+
PatternkitLibraryDiscoveryInterface $pattern_discovery,
48+
EntityStorageInterface $storage) {
49+
$this->config = $config;
4050
$this->logger = $logger;
4151
$this->libraryDiscovery = $pattern_discovery;
4252
$this->patternkitStorage = $storage;
@@ -51,13 +61,17 @@ public function __construct(LoggerChannelInterface $logger, PatternkitLibraryDis
5161
* Thrown if the storage handler couldn't be loaded.
5262
*/
5363
public static function create(ContainerInterface $container, $base_plugin_id): PatternkitBlock {
64+
/** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
65+
$config_factory = $container->get('config.factory');
66+
$config = $config_factory->get(PatternkitSettingsForm::SETTINGS);
5467
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager */
5568
$entity_manager = $container->get('entity.manager');
5669
/** @var \Drupal\Core\Logger\LoggerChannelInterface $logger */
5770
$logger = $container->get('logger.channel.patternkit');
5871
/** @var \Drupal\patternkit\PatternkitLibraryDiscoveryInterface $pattern_discovery */
5972
$pattern_discovery = $container->get('patternkit.library.discovery');
6073
return new static(
74+
$config,
6175
$logger,
6276
$pattern_discovery,
6377
$entity_manager->getStorage('patternkit_block'));
@@ -77,15 +91,22 @@ public function getDerivativeDefinitions($base_definition): array {
7791
/** @var \Drupal\patternkit\Pattern[] $patterns */
7892
$patterns = $this->libraryDiscovery->getAssets();
7993
}
80-
catch (Exception $exception) {
94+
catch (\Exception $exception) {
8195
$this->logger->error('Error loading patterns for derivative blocks: @message', ['@message' => $exception->getMessage()]);
8296
}
97+
98+
$libraries_config = $this->config->get('patternkit_libraries');
8399
foreach ($patterns as $pattern_id => $pattern) {
100+
$lib = strstr($pattern_id, '.', TRUE);
101+
if (isset($libraries_config[$lib])
102+
&& ($libraries_config[$lib]['enabled'] === 0 || $libraries_config[$lib]['visible'] === 0)) {
103+
continue;
104+
}
84105
$this->derivatives[$pattern_id] = [
85106
'category' => t(
86107
'Patternkit:@lib/@category',
87108
[
88-
'@lib' => strstr($pattern_id, '.', TRUE) ?? 'patternkit',
109+
'@lib' => $lib ?? 'patternkit',
89110
'@category' => $pattern->category ?? 'default',
90111
]
91112
),

src/StreamWrapper/LibraryStream.php

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Drupal\patternkit\StreamWrapper;
44

5-
use function array_merge;
65
use Drupal\Core\Asset\AttachedAssets;
76
use Drupal\Core\StreamWrapper\PublicStream;
87

0 commit comments

Comments
 (0)