3 namespace Drupal\ckeditor\Plugin\CKEditorPlugin;
5 use Drupal\ckeditor\CKEditorPluginBase;
6 use Drupal\ckeditor\CKEditorPluginConfigurableInterface;
7 use Drupal\ckeditor\CKEditorPluginCssInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Language\LanguageManager;
10 use Drupal\Core\Language\LanguageInterface;
11 use Drupal\editor\Entity\Editor;
14 * Defines the "language" plugin.
18 * label = @Translation("Language")
21 class Language extends CKEditorPluginBase implements CKEditorPluginConfigurableInterface, CKEditorPluginCssInterface {
26 public function isInternal() {
33 public function getFile() {
34 // This plugin is already part of Drupal core's CKEditor build.
41 public function getLibraries(Editor $editor) {
42 return ['ckeditor/drupal.ckeditor.plugins.language'];
48 public function getConfig(Editor $editor) {
50 $config = ['language_list' => 'un'];
51 $settings = $editor->getSettings();
52 if (isset($settings['plugins']['language'])) {
53 $config = $settings['plugins']['language'];
56 $predefined_languages = ($config['language_list'] === 'all') ?
57 LanguageManager::getStandardLanguageList() :
58 LanguageManager::getUnitedNationsLanguageList();
60 // Generate the language_list setting as expected by the CKEditor Language
61 // plugin, but key the values by the full language name so that we can sort
63 foreach ($predefined_languages as $langcode => $language) {
64 $english_name = $language[0];
65 $direction = empty($language[2]) ? NULL : $language[2];
66 if ($direction === LanguageInterface::DIRECTION_RTL) {
67 $language_list[$english_name] = $langcode . ':' . $english_name . ':rtl';
70 $language_list[$english_name] = $langcode . ':' . $english_name;
74 // Sort on full language name.
75 ksort($language_list);
76 $config = ['language_list' => array_values($language_list)];
83 public function getButtons() {
86 'label' => $this->t('Language'),
87 'image_alternative' => [
88 '#type' => 'inline_template',
89 '#template' => '<a href="#" class="cke-icon-only" role="button" title="' . $this->t('Language') . '" aria-label="' . $this->t('Language') . '"><span class="cke_button_icon cke_button__language_icon">' . $this->t('Language') . '</span></a>',
98 public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
100 $config = ['language_list' => 'un'];
101 $settings = $editor->getSettings();
102 if (isset($settings['plugins']['language'])) {
103 $config = $settings['plugins']['language'];
106 $predefined_languages = LanguageManager::getStandardLanguageList();
107 $form['language_list'] = [
108 '#title' => $this->t('Language list'),
109 '#title_display' => 'invisible',
112 'un' => $this->t("United Nations' official languages"),
113 'all' => $this->t('All @count languages', ['@count' => count($predefined_languages)]),
115 '#default_value' => $config['language_list'],
116 '#description' => $this->t('The list of languages to show in the language dropdown. The basic list will only show the <a href=":url">six official languages of the UN</a>. The extended list will show all @count languages that are available in Drupal.', [
117 ':url' => 'https://www.un.org/en/sections/about-un/official-languages',
118 '@count' => count($predefined_languages),
120 '#attached' => ['library' => ['ckeditor/drupal.ckeditor.language.admin']],
129 public function getCssFiles(Editor $editor) {
131 drupal_get_path('module', 'ckeditor') . '/css/plugins/language/ckeditor.language.css',