class CodeSnippet in CKEditor CodeSnippet 8
Defines the "codesnippet" plugin.
Plugin annotation
@CKEditorPlugin(
id = "codesnippet",
label = @Translation("CodeSnippet"),
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\ckeditor\CKEditorPluginBase implements CKEditorPluginButtonsInterface, CKEditorPluginInterface
- class \Drupal\codesnippet\Plugin\CKEditorPlugin\CodeSnippet implements CKEditorPluginConfigurableInterface
- class \Drupal\ckeditor\CKEditorPluginBase implements CKEditorPluginButtonsInterface, CKEditorPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of CodeSnippet
3 string references to 'CodeSnippet'
- codesnippet.info.yml in ./
codesnippet.info.yml - codesnippet.info.yml
- CodeSnippet::getButtons in src/
Plugin/ CKEditorPlugin/ CodeSnippet.php - Returns the buttons that this plugin provides, along with metadata.
- codesnippet_requirements in ./
codesnippet.install - Implements hook_requirements().
File
- src/
Plugin/ CKEditorPlugin/ CodeSnippet.php, line 18
Namespace
Drupal\codesnippet\Plugin\CKEditorPluginView source
class CodeSnippet extends CKEditorPluginBase implements CKEditorPluginConfigurableInterface {
/**
* {@inheritdoc}
*/
public function getFile() {
return 'libraries/codesnippet/plugin.js';
}
/**
* {@inheritdoc}
*/
public function getConfig(Editor $editor) {
$settings = $editor
->getSettings();
if (!empty($settings['plugins']['codesnippet']['highlight_style'])) {
$style = $settings['plugins']['codesnippet']['highlight_style'];
}
else {
$style = $this
->getDefaultStyle();
}
if (!empty($settings['plugins']['codesnippet']['highlight_languages'])) {
$languages = array_filter($settings['plugins']['codesnippet']['highlight_languages']);
}
else {
$languages = $this
->getLanguages();
}
return [
'codeSnippet_theme' => $style,
'codeSnippet_languages' => $languages,
];
}
/**
* {@inheritdoc}
*/
public function getButtons() {
return [
'CodeSnippet' => [
'label' => $this
->t('CodeSnippet'),
'image' => 'libraries/codesnippet/icons/codesnippet.png',
],
];
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
$settings = $editor
->getSettings();
$styles = $this
->getStyles();
$languages = $this
->getLanguages();
natcasesort($languages);
$form['#attached']['library'][] = 'codesnippet/codesnippet.admin';
$form['highlight_style'] = [
'#type' => 'select',
'#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'] : $this
->getDefaultStyle(),
];
$form['highlight_languages'] = [
'#type' => 'checkboxes',
'#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'] : $this
->getDefaultLanguages(),
];
return $form;
}
/**
* Returns available stylesheets to use for code syntax highlighting.
*/
private function getStyles() {
$styles = preg_grep('/\\.css/', scandir(DRUPAL_ROOT . '/libraries/codesnippet/lib/highlight/styles'));
$style_options = [];
foreach ($styles as $stylesheet) {
$name = str_replace('.css', '', $stylesheet);
$style_options[$name] = $name;
}
return $style_options;
}
/**
* Return the default style if one is not set in active config.
*
* This will be * the first one in the list of styles returned from
* getStyles().
*
* @return string
* Default style.
*/
private function getDefaultStyle() {
$styles = $this
->getStyles();
return reset($styles);
}
/**
* 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
* Default programming languages.
*/
private function getDefaultLanguages() {
$languages = array_keys($this
->getLanguages());
return array_combine($languages, $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).
*
* These languages are provided as options by the module because these are the
* languages that come with HighlightJS in the CodeSnippet CKEditor plugin.
*
* To add more languages, users can easily implement hook_form_alter() and add
* to the options array.
*
* @return array
* Set of programming languages.
*/
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',
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CKEditorPluginBase:: |
public | function |
Returns a list of plugins this plugin requires. Overrides CKEditorPluginInterface:: |
1 |
CKEditorPluginBase:: |
public | function |
Returns a list of libraries this plugin requires. Overrides CKEditorPluginInterface:: |
4 |
CKEditorPluginBase:: |
public | function |
Indicates if this plugin is part of the optimized CKEditor build. Overrides CKEditorPluginInterface:: |
4 |
CodeSnippet:: |
public | function |
Returns the buttons that this plugin provides, along with metadata. Overrides CKEditorPluginButtonsInterface:: |
|
CodeSnippet:: |
public | function |
Returns the additions to CKEDITOR.config for a specific CKEditor instance. Overrides CKEditorPluginInterface:: |
|
CodeSnippet:: |
private | function | Return an array of languages. | |
CodeSnippet:: |
private | function | Return the default style if one is not set in active config. | |
CodeSnippet:: |
public | function |
Returns the Drupal root-relative file path to the plugin JavaScript file. Overrides CKEditorPluginInterface:: |
|
CodeSnippet:: |
private | function | Return an array of languages. | |
CodeSnippet:: |
private | function | Returns available stylesheets to use for code syntax highlighting. | |
CodeSnippet:: |
public | function |
Returns a settings form to configure this CKEditor plugin. Overrides CKEditorPluginConfigurableInterface:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |