-
Notifications
You must be signed in to change notification settings - Fork 1
/
color_variables.module
65 lines (54 loc) · 1.76 KB
/
color_variables.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/**
* @file
* Contains color_variables.module.
*/
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_form_FORM_ID_alter().
*/
function color_variables_form_system_theme_settings_alter(&$form, FormStateInterface $form_state, $form_id) {
$config = \Drupal::configFactory()->get('colors.color_variables');
$form['saved_palette'] = [
'#type' => 'hidden',
'#access' => FALSE,
'#value' => $config->get('palette'),
];
$form['#submit'][] = 'color_variables_override';
}
/**
* Custom submit handler to write selected colors in SASS file.
*/
function color_variables_override(array $form, FormStateInterface $form_state) {
// Avoid color settings spilling over to theme settings.
$input = $form_state->getUserInput();
// Get the updated value and old values.
// We will calculate different of both array, so we will have only items that
// are changed.
$new_palette = $input['palette'];
$saved_pallets = $form_state->getValue('saved_palette');
$palette = $new_palette;
if (!empty($saved_pallets)) {
$palette = array_diff($new_palette, $saved_pallets);
}
if (!empty($palette)) {
/** @var \Drupal\color_variables\ColorVariablesFlushCacheInterface $service */
$service = \Drupal::service('color_variables.cache_flush');
$service->updateScss();
}
}
/**
* Implements hook_preprocess_html().
*/
function color_variables_preprocess_html(&$variables) {
$colors = \Drupal::configFactory()->get('colors.color_variables');
$data = $colors->getRawData();
if (isset($data['palette'])) {
$variables['style'] = NULL;
$variables['script'] = NULL;
$variables['script'] = json_encode($data['palette']);
foreach ($data['palette'] as $key => $hex) {
$variables['style'] .= "--$key: $hex; \n";
}
}
}