-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_css.admin.inc
136 lines (113 loc) · 4.88 KB
/
node_css.admin.inc
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
<?php
/**
* Define the Node CSS settings form with content type selection.
*/
function node_css_settings() {
// Load existing configuration.
$config = config('node_css.settings');
// Available themes for CodeMirror.
$themes = ['default' => 'Default']; // Start with the default theme.
// Path to the theme directory.
$theme_dir = backdrop_get_path('module', 'node_css') . '/codemirror/theme';
// Check if the directory exists and then scan for theme files.
if (is_dir($theme_dir)) {
$theme_files = scandir($theme_dir);
// Loop through each file and add it to the themes list if it's a CSS file.
foreach ($theme_files as $file) {
if (pathinfo($file, PATHINFO_EXTENSION) === 'css') {
// Get the theme name without the .css extension.
$theme_name = pathinfo($file, PATHINFO_FILENAME);
// Convert theme name to title case for display (e.g., "material" becomes "Material").
$display_name = ucwords(str_replace('-', ' ', $theme_name));
// Add to the themes array.
$themes[$theme_name] = $display_name;
}
}
}
// Select list for choosing the CodeMirror theme.
$form['codemirror_theme'] = [
'#type' => 'select',
'#title' => t('Theme'),
'#description' => t('Choose a theme for the CSS editor.'),
'#options' => $themes,
'#default_value' => $config->get('codemirror_theme') ?: 'default',
];
// Add a height setting for the CSS text area.
$form['css_textarea_height'] = [
'#type' => 'number',
'#title' => t('CSS Textarea Height (rows)'),
'#description' => t('Set the number of rows for the Node CSS textarea on each node. Default is 10.'),
'#default_value' => $config->get('css_textarea_height') ?: 10,
'#min' => 1,
];
// Add position setting for the CSS fieldset.
$form['css_text_position'] = [
'#type' => 'select',
'#title' => t('Position of CSS Editor'),
'#description' => t('Choose whether to display the CSS editor at the top of the page or between the tabs and the "Save" button.'),
'#options' => [
'top' => t('Top (above content)'),
'bottom' => t('Bottom (between tabs and "Save" button)'),
'vertical_tab' => t('Vertical Tab'),
],
'#default_value' => $config->get('css_text_position') ?: 'top',
];
// Retrieve all content types.
$content_types = node_type_get_types();
$options = [];
// Prepare options array with machine names and labels.
foreach ($content_types as $type => $info) {
$options[$type] = $info->name;
}
// Checkbox list for selecting content types.
$form['enabled_content_types'] = [
'#type' => 'checkboxes',
'#title' => t('Enable Node CSS for Content Types'),
'#description' => t('Select the content types for which the Node CSS module should be active. If no content types are selected, Node CSS will apply to all content types.'),
'#options' => $options,
'#default_value' => $config->get('enabled_content_types') ?: [],
];
// Add a default CSS textarea for each content type, shown only if that type is selected.
foreach ($options as $type => $label) {
$form['default_css_' . $type] = [
'#type' => 'textarea',
'#title' => t('@label Default CSS', ['@label' => $label]),
'#description' => t('Enter default CSS for @label content type.', ['@label' => $label]),
'#default_value' => $config->get('default_css_' . $type) ?: $config->get('default'),
'#attributes' => ['class' => ['codemirror-css-editor']], // Add class for CodeMirror initialization.
'#states' => [
'visible' => [
':input[name="enabled_content_types[' . $type . ']"]' => ['checked' => TRUE],
],
],
];
}
// Attach CodeMirror to all textareas with the codemirror-css-editor class.
node_css_attach_codemirror($form, '.codemirror-css-editor');
// Submit button.
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Save settings'),
];
return $form;
}
/**
* Submit handler for the Node CSS settings form.
*/
function node_css_settings_submit($form, &$form_state) {
// Save selected content types, CodeMirror theme, textarea height, and display position.
$config = config('node_css.settings');
$config->set('enabled_content_types', array_filter($form_state['values']['enabled_content_types']));
$config->set('codemirror_theme', $form_state['values']['codemirror_theme']);
$config->set('css_textarea_height', $form_state['values']['css_textarea_height']);
$config->set('css_text_position', $form_state['values']['css_text_position']); // Save position setting.
// Save each default CSS value for the selected content types.
foreach ($form_state['values']['enabled_content_types'] as $type => $enabled) {
if ($enabled) {
$config->set('default_css_' . $type, $form_state['values']['default_css_' . $type]);
}
}
$config->save();
// Display a status message.
backdrop_set_message(t('The Node CSS settings have been saved.'));
}