-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdxpr_theme_callbacks.inc
More file actions
executable file
·429 lines (386 loc) · 11.1 KB
/
dxpr_theme_callbacks.inc
File metadata and controls
executable file
·429 lines (386 loc) · 11.1 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<?php
/**
* @file
* DXPR Theme helper functions.
*/
use Drupal\Component\Serialization\Json;
use Drupal\Core\File\FileSystemInterface;
/**
* Helper function returns path of css cache file.
*
* @param string $theme
* Theme machine name.
*
* @return string
* A valid path of css cache file.
*/
function _dxpr_theme_css_cache_file($theme) {
return 'public://dxpr_theme/css/themesettings-' . $theme . '.css';
}
/**
* Write css files for the color settings.
*
* @param string $theme
* Theme machine name.
*
* phpcs:ignore
* @deprecated This function is no longer needed. Kept for legacy reasons.
*/
function dxpr_theme_color_module_css_write($theme) {
}
/**
* Theme CSS generator from DXPR Theme-settings.php.
*
* Custom theme settings might need a lot of CSS.
* So we put it in a file for optimal performance.
*
* @param string $theme
* Theme name.
*/
function dxpr_theme_css_cache_build(string $theme) {
$dxpr_theme_css_file = _dxpr_theme_css_cache_file($theme);
$dir = 'public://dxpr_theme/css/';
$dir_writable = \Drupal::service('file_system')
->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY);
$palette = unserialize((theme_get_setting('color_palette', $theme) ?? ''), [
'allowed_classes' => FALSE,
]) ?: NULL;
// Fetch default palette.
if (empty($palette)) {
$path = \Drupal::service('extension.list.theme')->getPath('dxpr_theme');
$filepath = sprintf('%s/%s/features/sooper-colors/color-settings.json', DRUPAL_ROOT, $path);
if ($path && file_exists($filepath)) {
$json = file_get_contents($filepath);
$settings = Json::decode($json);
$palette = $settings['schemes']['default']['colors'] ?? [];
}
if (empty($palette)) {
\Drupal::messenger()->addWarning(t('Could not create theme styles; please resave theme settings.'));
}
}
// Construct CSS file:
$css = '';
// Load Sooper Features CSS.
foreach (\Drupal::service('file_system')->scanDirectory(\Drupal::service('extension.list.theme')->getPath('dxpr_theme') . '/features', '/css.inc/i') as $file) {
require_once $file->uri;
$function_name = basename($file->filename, '.inc');
$function_name = str_replace('-', '_', $function_name);
if (function_exists($function_name)) {
$function_name($theme, $css, $palette);
}
}
$file_object = \Drupal::service('file.repository')->writeData($css, $dxpr_theme_css_file, FileSystemInterface::EXISTS_REPLACE);
if ($dir_writable && $file_object) {
$message = t('DXPR Theme CSS file cache built for %theme', ['%theme' => $theme]);
\Drupal::messenger()->addMessage($message);
\Drupal::logger('dxpr_theme')->notice($message);
}
else {
$message = t('Cannot write theme-settings file, please check your file system. (See status report page)');
\Drupal::messenger()->addMessage($message, 'error');
\Drupal::logger('dxpr_theme')->error($message);
}
// If the CSS & JS aggregation are enabled we need to clear the caches.
drupal_flush_all_caches();
\Drupal::service('asset.css.collection_optimizer')->deleteAll();
\Drupal::service('asset.js.collection_optimizer')->deleteAll();
}
/**
* Returns a settings value.
*
* @param string $key
* Key index in color.inc $info array.
*
* @return mixed
* Value for the given setting.
*/
function _dxpr_theme_get_const(string $key) {
$settings = &drupal_static(__FUNCTION__, []);
if (empty($settings)) {
$path = \Drupal::service('extension.list.theme')->getPath('dxpr_theme');
$filepath = sprintf('%s/%s/features/sooper-settings/theme-settings.json', DRUPAL_ROOT, $path);
if ($path && file_exists($filepath)) {
$json = file_get_contents($filepath);
$settings = Json::decode($json);
}
}
return $settings[$key] ?? NULL;
}
/**
* Adds the specified theme setting as a css variable for given property.
*
* @param string $setting
* Theme setting machine name.
* @param string $property
* CSS property to add.
*
* @return string
* CSS formatted property and value for given setting, or FALSE.
*/
function _dxpr_theme_settings_add_css(string $setting, string $property): string {
if ($css_var = _dxpr_theme_setting_get_css_var($setting)) {
return sprintf(" %s: %s;\n", $property, $css_var);
}
return '';
}
/**
* Returns the CSS variable corresponding to given theme setting.
*
* Note that this only works for theme settings and not colors.
*
* @param string $setting
* Theme setting machine name.
*
* @return string
* CSS variable, e.g. "var(--prefix-name)".
*/
function _dxpr_theme_setting_get_css_var(string $setting): string {
if (theme_get_setting($setting)) {
$css_var = str_replace('_', '-', $setting);
$prefix = _dxpr_theme_get_const('cssSettingPrefix');
return sprintf("var(%s%s)", $prefix, $css_var);
}
return '';
}
/**
* Helper determine font type.
*
* @param string $font
* The font name.
* @param string $theme
* The theme machine name.
*
* @return string
* The font stack string.
*/
function _dxpr_theme_get_font($font, $theme) {
if (substr($font, 0, 1) === '0') {
$font = _dxpr_theme_format_google_font_name($font);
}
elseif (substr($font, 0, 1) === '1') {
$font = _dxpr_theme_format_local_font_name($font);
}
else {
$font = _dxpr_theme_font_stack($font);
}
return $font;
}
/**
* Helper function to format the font name.
*
* Following a pattern that seems to hold for all google web fonts.
*
* @param string $key
* The google font name.
*
* @return string
* Formatted font string.
*/
function _dxpr_theme_format_google_font_name($key) {
$font = explode(':', substr($key, 1));
$font_basename = str_replace('+', ' ', $font[0]);
$variant = $font[1];
$goog_weight = '';
switch ($variant) {
case '100':
case '100italic':
case 'hairline':
$goog_weight = ' Hairline';
break;
case '200':
case '200italic':
case 'extralight':
$goog_weight = ' Extralight';
break;
case '300':
case '300italic':
case 'light':
$goog_weight = ' Light';
break;
case '400':
case '400italic':
case 'regular':
$goog_weight = ' Regular';
break;
case '500':
case '500italic':
case 'medium':
$goog_weight = ' Medium';
break;
case '600':
case '600italic':
case 'semibold':
$goog_weight = ' SemiBold';
break;
case '700':
case '700italic':
case 'bold':
case 'bolditalic':
$goog_weight = ' Bold';
break;
case '800':
case '800italic':
case 'extrabold':
$goog_weight = ' Extrabold';
break;
case '900':
case '900italic':
case 'Black':
$goog_weight = ' Black';
break;
}
if (stristr($variant, 'italic') !== FALSE) {
$goog_style = ' Italic';
}
else {
$goog_style = '';
}
$goog_name = '"' . $font_basename . $goog_weight . $goog_style . '", "' . $font_basename . '"';
return $goog_name;
}
/**
* Helper function to read the font name from the key.
*
* The hard part was already done in theme-settings.php.
*
* @param string $key
* The local font key.
*
* @return string
* The local font name..
*/
function _dxpr_theme_format_local_font_name($key) {
$font = explode('|', $key);
return $font[2];
}
/**
* Helper function for getting font style from font theme setting.
*
* @param string $font
* Theme setting font value.
*
* @return string
* Font style value.
*/
function _dxpr_theme_font_get_style($font): string {
return (strpos($font, 'italic')) ? 'italic' : 'normal';
}
/**
* Helper function for getting font weight form font theme setting.
*
* @param string $font
* Theme setting font value.
*
* @return string
* Font weight value.
*/
function _dxpr_theme_font_get_weight($font): string {
return (preg_match('/[0-9]{3}/', $font, $matches)) ? $matches[0] : '400';
}
/**
* Provides Font Stack values for theme settings.
*
* @param string $font
* The font name.
*
* @return string
* The font stack string.
*
* @see font_list()
*/
function _dxpr_theme_font_stack($font = NULL) {
$fonts = [
'SF_Segoe_Roboto' => '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"',
'helvetica' => 'Arial, Helvetica, "Nimbus Sans L", "Liberation Sans", "FreeSans", sans-serif',
'myriad' => '"myriad pro",myriad,calibri,arial,tahoma,verdana,sans-serif',
'verdana' => 'Verdana, "Bitstream Vera Sans", Arial, sans-serif',
'lucida' => '"Lucida Sans Unicode", "Lucida Sans", "Lucida Grande", "DejaVu Sans", Arial, sans-serif',
'geneva' => '"Geneva", "Bitstream Vera Serif", "Tahoma", sans-serif',
'tahoma' => 'Tahoma, Geneva, "DejaVu Sans Condensed", sans-serif',
'trebuchet' => '"Trebuchet MS",Trebuchet,Tahoma,arial,verdana,sans',
'century' => '"Century Gothic", "URW Gothic L", Helvetica, Arial, sans-serif',
'garamond' => 'Garamond,"Times New Roman",Times,Century,Cambria,Georgia,serif',
'georgia' => 'Georgia, "Bitstream Vera Serif", serif',
'palatino' => '"Palatino Linotype", "URW Palladio L", "Book Antiqua", "Palatino", serif',
'times' => '"Free Serif", "Times New Roman", Times, serif',
];
// Return all font stacks if no specific font requested.
if ($font === NULL) {
return $fonts;
}
if (!isset($fonts[$font])) {
$font = 'helvetica';
}
return $fonts[$font];
}
/**
* Returns data from the color-settings.json file.
*
* @param string|null $key
* Key index in color.inc $info array.
*
* @return array
* Array containing requested data.
*/
function _dxpr_theme_get_color_inc(?string $key = NULL): array {
$path = \Drupal::service('extension.list.theme')->getPath('dxpr_theme');
$filepath = sprintf('%s/%s/features/sooper-colors/color-settings.json', DRUPAL_ROOT, $path);
if ($path && file_exists($filepath)) {
$json = file_get_contents($filepath);
$settings = Json::decode($json);
if ($settings) {
$data = $key ? ($settings[$key] ?? []) : $settings;
}
}
return $data ?? [];
}
/**
* Returns the color field keys.
*
* @return array
* The 'fields' sub-array.
*/
function _dxpr_theme_get_color_names(): array {
return _dxpr_theme_get_color_inc('fields');
}
/**
* Returns the color schemes.
*
* @return array
* The 'schemes' sub-array.
*/
function _dxpr_theme_get_color_schemes(): array {
return _dxpr_theme_get_color_inc('schemes');
}
/**
* Returns the specified color scheme, defaults to 'default'.
*
* @param string $scheme
* The color scheme machine name.
*
* @return array
* The specified color scheme indexed by color machine names.
*/
function _dxpr_theme_get_color_scheme(string $scheme = 'default'): array {
$schemes = _dxpr_theme_get_color_schemes();
return $schemes['default'] ?? [];
}
/**
* Color options for theme settings.
*
* @param string $theme
* Theme machine name.
*
* @return array
* Color options.
*/
function _dxpr_theme_color_options($theme) {
$colors = [
'' => t('None (Theme Default)'),
'white' => t('White'),
'custom' => t('Custom Color'),
];
$theme_colors = _dxpr_theme_get_color_names($theme);
$colors = array_merge($colors, $theme_colors);
return $colors;
}