-
Notifications
You must be signed in to change notification settings - Fork 2
/
ubc_ckeditor_widgets.module
94 lines (83 loc) · 2.27 KB
/
ubc_ckeditor_widgets.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
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
<?php
/**
* @file
* Contains ubc_ckeditor_widgets.module.
*/
/**
* Implements hook_page_attachments_alter().
*/
function ubc_ckeditor_widgets_page_attachments_alter(array &$page) {
// Attaches css assets globally.
$page['#attached']['library'][] = 'ubc_ckeditor_widgets/ubc_ckeditor_widgets.ui';
// Read config and add to drupalSettings
$config = \Drupal::config('ubc_ckeditor_widgets.settings');
$fields = [
'background_colors',
'gap_styles',
'padding_styles',
'margin_styles',
'table_styles',
'three_column_layout_styles',
'two_column_layout_styles',
'width_styles',
];
$default_values = [
'background_colors' => [
['label' => 'White', 'value' => 'bg-white'],
],
'gap_styles' => [
['label' => 'Normal', 'value' => 'gap-6'],
],
'padding_styles' => [
['label' => 'Normal', 'value' => 'p-6'],
],
'margin_styles' => [
['label' => 'Normal', 'value' => 'my-6'],
],
'table_styles' => [
['label' => 'None', 'value' => 'table--nostyle'],
],
'three_column_layout_styles' => [
['label' => 'Equal Width', 'value' => 'align-equal'],
],
'two_column_layout_styles' => [
['label' => 'Equal Width', 'value' => 'align-equal'],
],
'width_styles' => [
['label' => 'Column width: Auto', 'value' => 'w-auto'],
],
];
$settings = [];
foreach ($fields as $field) {
$key_value_pairs = $config->get($field);
if ($key_value_pairs) {
// Parse the newline-separated value|key pairs.
$pairs = explode("\n", $key_value_pairs);
foreach ($pairs as $pair) {
list($value, $key) = explode('|', $pair, 2);
$settings[convertToCamelCase($field)][] = [
'label' => trim($key),
'value' => trim($value),
];
}
} else {
$settings[convertToCamelCase($field)] = $default_values[$field];
}
}
if (!empty($settings)) {
$page['#attached']['drupalSettings']['ubcCkeditorWidgets'] = $settings;
}
}
/**
* Converts a snake_case string to camelCase.
*
* @param string $string
* The snake_case string.
*
* @return string
* The camelCase string.
*/
function convertToCamelCase($string) {
$result = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $string))));
return $result;
}