Repository navigation
Expand file tree
/
Copy pathtinymce_codehighlight.admin.inc
More file actions
108 lines (98 loc) · 3.73 KB
/
Copy pathtinymce_codehighlight.admin.inc
File metadata and controls
108 lines (98 loc) · 3.73 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
<?php
/**
* @file
* Settings form for TinyMCE Code Highlight.
*/
/**
* Settings form callback.
*/
function tinymce_codehighlight_settings_form($form, $form_state) {
$form['#config'] = 'tinymce_codehighlight.settings';
$config = config('tinymce_codehighlight.settings');
$all_themes = _tinymce_codehighlight_get_all_themes();
$active_theme = _tinymce_codehighlight_get_active_theme();
$theme_options = array();
foreach ($all_themes as $name => $theme) {
$theme_options[$name] = $theme['label'];
}
$form['theme'] = array(
'#type' => 'radios',
'#title' => t('Prism.js theme'),
'#default_value' => $active_theme['name'],
'#options' => $theme_options,
'#required' => TRUE,
);
foreach ($all_themes as $name => $theme) {
if (!empty($theme['screenshot_path'])) {
$img_url = base_path() . $theme['screenshot_path'];
$form['theme'][$name]['#description'] = '<img src="' . $img_url . '" alt="">';
}
else {
$form['theme'][$name]['#description'] = t('No screenshot provided');
}
}
$form['copybutton'] = array(
'#type' => 'checkbox',
'#title' => t('Copy button'),
'#default_value' => $config->get('copybutton'),
'#description' => t('Adds a small icon button in the top right corner to copy the code to clipboard.'),
);
$help = t('Prism.js supports a variety of programming languages. This setting has no influence on actual highlighting, only sets the dropdown options.');
$form['wrapper'] = array(
'#type' => 'fieldset',
'#title' => t('Languages'),
'#description' => $help,
);
$module_path = backdrop_get_path('module', 'tinymce_codehighlight');
$file_path = BACKDROP_ROOT . base_path() . $module_path . '/libraries/prismjs/components.json';
$data = file_get_contents($file_path);
$json = json_decode($data, TRUE);
$languages = $language_options = array();
if ($json) {
$languages = $json['languages'];
unset($languages['meta']);
foreach ($languages as $key => $info) {
$language_options[$key] = check_plain($info['title']);
}
}
else {
backdrop_set_message(t('Reading available language options failed.'), 'error');
}
// Be careful with missing or broken config.
$default_values = $config->get('languages') ? $config->get('languages') : array();
$form['wrapper']['languages'] = array(
'#type' => 'checkboxes',
'#title' => t('Options for TinyMCE codesample plugin'),
'#default_value' => $default_values,
'#options' => $language_options,
);
foreach ($languages as $key => $info) {
if (!empty($info['aliasTitles'])) {
$form['wrapper']['languages'][$key]['#description'] = implode(', ', array_values($info['aliasTitles']));
}
}
// Hidden form item to get the enabled languages with labels as an extra
// config item. Actual values will be set on form submission.
$form['#language_options'] = $language_options;
$form['#submit'][] = '_tinymce_codehighlight_settings_form_submit';
$form['enabled_with_labels'] = array(
'#type' => 'value',
'#value' => array(),
);
$form['#attached']['css'] = array($module_path . '/css/tinymce-codehighlight-admin.css');
$form['#attached']['js'] = array($module_path . '/js/tinymce-codehighlight-admin.js');
return system_settings_form($form);
}
/**
* Additional submission callback to add a config item.
*/
function _tinymce_codehighlight_settings_form_submit($form, &$form_state) {
$enabled_with_labels = array();
$selected = array_filter($form_state['values']['languages']);
// Add name => label key/value pairs, which can't be made available via
// FormAPI items.
foreach ($selected as $name) {
$enabled_with_labels[$name] = $form['#language_options'][$name];
}
$form_state['values']['enabled_with_labels'] = $enabled_with_labels;
}