-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadminimal_theme.theme
132 lines (116 loc) · 4.29 KB
/
adminimal_theme.theme
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* @file
* Functions to support theming in the Adminimal theme.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Template\Attribute;
use Drupal\Core\File\FileSystemInterface;
/**
* Implements hook_preprocess_HOOK() for HTML document templates.
*/
function adminimal_theme_preprocess_html(&$variables) {
// Add adminimal class to the body.
if ($variables['attributes'] instanceof Attribute) {
$variables['attributes']->addClass('adminimal');
}
else {
$variables['attributes']['class'][] = 'adminimal';
}
// Add library with custom CSS.
if (theme_get_setting('custom_css')) {
$variables['#attached']['library'][] = 'adminimal_theme/custom-styling';
}
// Add Open Sans font styles based on the theme setting.
if (!theme_get_setting('disable_google_fonts')) {
$variables['#attached']['library'][] = 'adminimal_theme/google-fonts';
}
}
/**
* Implements hook_form_system_theme_settings_alter().
*/
function adminimal_theme_form_system_theme_settings_alter(&$form, FormStateInterface &$form_state, $form_id = NULL) {
// Work-around for a core bug affecting admin themes. See issue #943212.
if (isset($form_id)) {
return;
}
// Get adminimal theme path.
global $base_url;
$adminimal_path = drupal_get_path('theme', 'adminimal_theme');
$old_css_path = $adminimal_path . '/css/custom.css';
$custom_css_path = 'public://adminimal-custom.css';
$custom_css_dir = str_replace($base_url . '/', "", file_create_url($custom_css_path));
$custom_css_url = file_create_url($custom_css_path);
// Try to create the adminimal-custom.css file automatically.
if (!file_exists($custom_css_path)) {
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
$file_system = \Drupal::service('file_system');
// Try to migrate from the old css.
if (file_exists($old_css_path)) {
$file_system->copy($old_css_path, $custom_css_path, FileSystemInterface::EXISTS_ERROR);
}
// Else create a new blank css file.
else {
$file_system->saveData("", $custom_css_path, FileSystemInterface::EXISTS_ERROR);
}
}
// Notify user to remove his old css file.
if (file_exists($old_css_path)) {
\Drupal::messenger()->addWarning(t('Please delete the old @css_location file, as its no longer used.', ['@css_location file' => $old_css_path]));
}
$form['adminimal_custom'] = [
'#type' => 'fieldset',
'#title' => t('Adminimal Customization'),
'#weight' => -10,
];
$form['adminimal_custom']['custom_css'] = [
'#type' => 'checkbox',
'#title' => t('Use "adminimal-custom.css"'),
'#description' => t('Include adminimal-custom.css file to override or add custom css code without subthememing/hacking Adminimal Theme.'),
'#default_value' => theme_get_setting('custom_css'),
];
$form['adminimal_custom']['adminimal_custom_check'] = [
'#type' => 'fieldset',
'#title' => t('Custom CSS file check'),
'#weight' => 50,
'#states' => [
// Hide the settings when the cancel notify checkbox is disabled.
'visible' => [
':input[name="custom_css"]' => ['checked' => TRUE],
],
],
];
if (file_exists($custom_css_path)) {
$form['adminimal_custom']['adminimal_custom_check']['custom_css_description'] = [
'#type' => 'container',
'#attributes' => [
'class' => ['messages', 'messages--status'],
],
'message' => [
'#markup' => t('Custom CSS file Found in: @css', ['@css' => $custom_css_dir]),
],
];
}
else {
$form['adminimal_custom']['adminimal_custom_check']['custom_css_not_found'] = [
'#type' => 'container',
'#attributes' => [
'class' => ['messages', 'messages--error'],
],
'message' => [
'#markup' => t('Custom CSS file not found. You must create the @css file manually.', ['@css' => $custom_css_dir]),
],
];
}
$form['adminimal_theme_settings'] = [
'#type' => 'details',
'#title' => t('Adminimal theme settings'),
'#open' => TRUE,
];
$form['adminimal_theme_settings']['disable_google_fonts'] = [
'#type' => 'checkbox',
'#title' => t('Avoid using "Open Sans" font'),
'#default_value' => theme_get_setting('disable_google_fonts'),
'#description' => t('Useful for languages that are not well supported by the "Open sans" font. Like Japanese for example.'),
];
}