Commit d198198d authored by kevinquillen's avatar kevinquillen Committed by kevinquillen

Issue #2800233 by kevinquillen: Refactor to remove dependency on config object

parent 1ac97476
......@@ -6,8 +6,6 @@ use Drupal\ckeditor\CKEditorPluginBase;
use Drupal\ckeditor\CKEditorPluginConfigurableInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\editor\Entity\Editor;
use Drupal\Core\Link;
use Drupal\Core\Url;
/**
* Defines the "codesnippet" plugin.
......@@ -32,29 +30,20 @@ class CodeSnippet extends CKEditorPluginBase implements CKEditorPluginConfigurab
public function getConfig(Editor $editor) {
$settings = $editor->getSettings();
$default_config = \Drupal::config('codesnippet.settings');
if (!empty($settings['plugins']['codesnippet']['highlight_style'])) {
$style = $settings['plugins']['codesnippet']['highlight_style'];
}
else {
$style = $default_config->get('style');
$style = $this->getDefaultStyle();
}
if (!empty($settings['plugins']['codesnippet']['highlight_languages'])) {
$languages = array_filter($settings['plugins']['codesnippet']['highlight_languages']);
}
else {
$languages = $default_config->get('languages');
$languages = $this->getLanguages();
}
// Before sending along to CKEditor, alpha sort and capitalize the language.
$languages = array_map(function ($language) {
return ucwords($language);
}, $languages);
asort($languages);
return [
'codeSnippet_theme' => $style,
'codeSnippet_languages' => $languages,
......@@ -79,11 +68,7 @@ class CodeSnippet extends CKEditorPluginBase implements CKEditorPluginConfigurab
public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
$settings = $editor->getSettings();
$styles = $this->getStyles();
$config = \Drupal::config('codesnippet.settings');
$default_style = $config->get('style');
$languages = $config->get('languages');
$languages = $this->getLanguages();
asort($languages);
$form['#attached']['library'][] = 'codesnippet/codesnippet.admin';
......@@ -93,7 +78,7 @@ class CodeSnippet extends CKEditorPluginBase implements CKEditorPluginConfigurab
'#title' => 'highlight.js Style',
'#description' => $this->t('Select a style to apply to all highlighted code snippets. You can preview the styles at <a href=":url">:url</a>.', [':url' => 'https://highlightjs.org/static/demo']),
'#options' => $styles,
'#default_value' => !empty($settings['plugins']['codesnippet']['highlight_style']) ? $settings['plugins']['codesnippet']['highlight_style'] : $default_style,
'#default_value' => !empty($settings['plugins']['codesnippet']['highlight_style']) ? $settings['plugins']['codesnippet']['highlight_style'] : $this->getDefaultStyle(),
];
$form['highlight_languages'] = [
......@@ -101,7 +86,7 @@ class CodeSnippet extends CKEditorPluginBase implements CKEditorPluginConfigurab
'#title' => 'Supported Languages',
'#options' => $languages,
'#description' => $this->t('Enter languages you want to have as options in the editor dialog. To add a language not in this list, please see the README.txt of this module.'),
'#default_value' => isset($settings['plugins']['codesnippet']['highlight_languages']) ? $settings['plugins']['codesnippet']['highlight_languages'] : array_map('strtolower', $languages),
'#default_value' => isset($settings['plugins']['codesnippet']['highlight_languages']) ? $settings['plugins']['codesnippet']['highlight_languages'] : $this->getDefaultLanguages(),
];
return $form;
......@@ -122,4 +107,64 @@ class CodeSnippet extends CKEditorPluginBase implements CKEditorPluginConfigurab
return $style_options;
}
private function getDefaultStyle() {
return 'arta';
}
/**
* Return an array of languages.
*
* This is used to set the list of checkboxes to be set as all TRUE when first
* configuring the plugin. Language names like C++ or C# don't quite work well
* with array_map for the checkboxes element since the value and key do not
* match up.
*
* @return array
*/
private function getDefaultLanguages() {
$languages = $this->getLanguages();
return array_combine(array_keys($languages), array_keys($languages));
}
/**
* Return an array of languages.
*
* This should be used when presenting language options to the user in a form
* element.
*
* Unlike getDefaultLanguages(), this provides human friendly names for
* languages (ex. C++ instead of cpp).
*
* @return array
*/
private function getLanguages() {
return [
'apache' => 'Apache',
'bash' => 'Bash',
'coffeescript' => 'CoffeeScript',
'cpp' => 'C++',
'cs' => 'C#',
'css' => 'CSS',
'diff' => 'Diff',
'html' => 'HTML',
'http' => 'HTTP',
'ini' => 'INI',
'java' => 'Java',
'javascript' => 'JavaScript',
'json' => 'JSON',
'makefile' => 'Makefile',
'markdown' => 'Markdown',
'nginx' => 'Nginx',
'objectivec' => 'Objective-C',
'perl' => 'Perl',
'php' => 'PHP',
'python' => 'Python',
'ruby' => 'Ruby',
'sql' => 'SQL',
'vbscript' => 'VBScript',
'xhtml' => 'XHTML',
'xml' => 'XML'
];
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment